Class: Skit::Serialization::Processor::Nilable
- Extended by:
- T::Sig
- Defined in:
- lib/skit/serialization/processor/nilable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #deserialize(value, path: Path.new) ⇒ Object
-
#initialize(type_spec, registry:) ⇒ Nilable
constructor
A new instance of Nilable.
- #serialize(value, path: Path.new) ⇒ Object
- #traverse(value, path: Path.new, &blk) ⇒ Object
Constructor Details
#initialize(type_spec, registry:) ⇒ Nilable
Returns a new instance of Nilable.
16 17 18 19 20 21 22 23 24 |
# File 'lib/skit/serialization/processor/nilable.rb', line 16 def initialize(type_spec, registry:) super unless self.class.nilable_type?(type_spec) raise ArgumentError, "Expected nilable type, got #{type_spec.class}" end @inner_type = T.let(extract_inner_type(type_spec), T.untyped) end |
Class Method Details
.handles?(type_spec) ⇒ Boolean
11 12 13 |
# File 'lib/skit/serialization/processor/nilable.rb', line 11 def self.handles?(type_spec) nilable_type?(type_spec) end |
.nilable_type?(type_object) ⇒ Boolean
62 63 64 65 66 67 68 69 70 |
# File 'lib/skit/serialization/processor/nilable.rb', line 62 def nilable_type?(type_object) return false unless type_object.is_a?(T::Types::Union) types = type_object.types has_nil = types.any? { |t| t.is_a?(T::Types::Simple) && t.raw_type == NilClass } non_nil_count = types.count { |t| !(t.is_a?(T::Types::Simple) && t.raw_type == NilClass) } has_nil && non_nil_count == 1 end |
Instance Method Details
#deserialize(value, path: Path.new) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/skit/serialization/processor/nilable.rb', line 35 def deserialize(value, path: Path.new) return nil if value.nil? processor = @registry.processor_for(@inner_type) processor.deserialize(value, path: path) end |
#serialize(value, path: Path.new) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/skit/serialization/processor/nilable.rb', line 27 def serialize(value, path: Path.new) return nil if value.nil? processor = @registry.processor_for(@inner_type) processor.serialize(value, path: path) end |
#traverse(value, path: Path.new, &blk) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/skit/serialization/processor/nilable.rb', line 49 def traverse(value, path: Path.new, &blk) super return if value.nil? processor = @registry.processor_for(@inner_type) processor.traverse(value, path: path, &blk) end |