Class: Skit::Serialization::Processor::Array

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/skit/serialization/processor/array.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_spec, registry:) ⇒ Array



16
17
18
19
20
21
22
23
# File 'lib/skit/serialization/processor/array.rb', line 16

def initialize(type_spec, registry:)
  super
  unless type_spec.is_a?(T::Types::TypedArray)
    raise ArgumentError, "Expected T::Types::TypedArray, got #{type_spec.class}"
  end

  @element_type = T.let(type_spec.type, T.untyped)
end

Class Method Details

.handles?(type_spec) ⇒ Boolean



11
12
13
# File 'lib/skit/serialization/processor/array.rb', line 11

def self.handles?(type_spec)
  type_spec.is_a?(T::Types::TypedArray)
end

Instance Method Details

#deserialize(value, path: Path.new) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
# File 'lib/skit/serialization/processor/array.rb', line 36

def deserialize(value, path: Path.new)
  raise DeserializeError.new("Expected Array, got #{value.class}", path: path) unless value.is_a?(::Array)

  value.each_with_index.map do |item, index|
    processor = @registry.processor_for(@element_type)
    processor.deserialize(item, path: path.append(index))
  end
end

#serialize(value, path: Path.new) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
# File 'lib/skit/serialization/processor/array.rb', line 26

def serialize(value, path: Path.new)
  raise SerializeError.new("Expected Array, got #{value.class}", path: path) unless value.is_a?(::Array)

  value.each_with_index.map do |item, index|
    processor = @registry.processor_for(@element_type)
    processor.serialize(item, path: path.append(index))
  end
end

#traverse(value, path: Path.new, &blk) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/skit/serialization/processor/array.rb', line 52

def traverse(value, path: Path.new, &blk)
  super

  return unless value.is_a?(::Array)

  value.each_with_index do |item, index|
    processor = @registry.processor_for(@element_type)
    processor.traverse(item, path: path.append(index), &blk)
  end
end