Module: ParamsReady::Marshaller::ArrayMarshallers::StructMarshaller

Extended by:
AbstractMarshaller
Defined in:
lib/params_ready/marshaller/array_marshallers.rb

Class Method Summary collapse

Methods included from AbstractMarshaller

marshal

Class Method Details

.canonicalize(definition, hash, context, validator) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 53

def self.canonicalize(definition, hash, context, validator)
  if definition.compact?
    ArrayMarshaller.canonicalize(definition, hash.values, context, validator)
  else
    count_key = :cnt
    found, count = Helpers::FindInHash.find_in_hash hash, count_key
    raise ParamsReadyError, "Count not found" unless found

    base = 10 if count.is_a? String
    count = Integer(count, base)
    array = (0...count).map do |index|
      found, value = Helpers::FindInHash.find_in_hash hash, index
      element = definition.prototype.create
      element.set_from_input(value, context, validator) if found
      element
    end
    [array, validator]
  end
end

.do_marshal(array, _, compact) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/params_ready/marshaller/array_marshallers.rb', line 73

def self.do_marshal(array, _, compact)
  return array if compact

  result = array.each_with_index.reduce({}) do |result, (element, index)|
    index = index.to_s
    result[index] = element
    result
  end

  result['cnt'] = array.length.to_s
  result
end