Method: Serializer::Serializable#array_to_hash

Defined in:
lib/serializer/serializable.rb

#array_to_hash(array, summary = false) ⇒ OrderedHash

Returns Generate a hash from an array of objects. If the array member has a field tagged as a key, that field will be used as the hash.key. Otherwise the index position of the array member will be used as the key.

Parameters:

  • array (Array)

    The array to be converted to a hash

Returns:

  • (OrderedHash)

    Generate a hash from an array of objects. If the array member has a field tagged as a key, that field will be used as the hash.key. Otherwise the index position of the array member will be used as the key



83
84
85
86
87
88
89
90
91
# File 'lib/serializer/serializable.rb', line 83

def array_to_hash(array,summary=false)
  item_hash = OrderedHash.new
  array.each_index do |index|
    item = array[index]
    ikey = (item.respond_to?(:key) && item.key) ?  item.key : index
    item_hash[ikey] =  item.respond_to?(:to_hash) ? item.to_hash(summary) : item
  end
  item_hash
end