module ParamsReady::Marshaller::ArrayMarshallers::HashMarshaller

Public Class Methods

canonicalize(definition, hash, context, validator) click to toggle source
# 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

    count = Integer(count)
    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) click to toggle source
# File lib/params_ready/marshaller/array_marshallers.rb, line 72
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