Class: PGTrunk::Serializers::ArrayOfHashesSerializer

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/pg_trunk/core/serializers/array_of_hashes_serializer.rb

Overview

Cast the attribute value as an array of strings. It knows how to cast arrays returned by PostgreSQL as a string like 'USD,EUR,GBP' into ['USD', 'EUR', 'GBP'].

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/pg_trunk/core/serializers/array_of_hashes_serializer.rb', line 10

def cast(value)
  case value
  when ::String then JSON.parse(value).map(&:symbolize_keys)
  when ::NilClass then []
  when ::Array then value.map(&:to_h)
  else [value.to_h]
  end
end

#serialize(value) ⇒ Object



19
20
21
# File 'lib/pg_trunk/core/serializers/array_of_hashes_serializer.rb', line 19

def serialize(value)
  Array.wrap(value).map { |item| item.to_h.symbolize_keys }
end