Module: HstoreAccessor::Serialization

Included in:
HstoreAccessor
Defined in:
lib/hstore_accessor/serialization.rb

Constant Summary collapse

InvalidDataTypeError =
Class.new(StandardError)
VALID_TYPES =
[
  :boolean,
  :date,
  :datetime,
  :decimal,
  :float,
  :integer,
  :string
]
DEFAULT_SERIALIZER =
->(value) { value.to_s }
DEFAULT_DESERIALIZER =
DEFAULT_SERIALIZER
SERIALIZERS =
{
  boolean: -> value { (value.to_s == "true").to_s },
  date: -> value { value && value.to_s },
  datetime: -> value { value && value.to_i }
}
DESERIALIZERS =
{
  boolean: -> value { TypeHelpers.cast(:boolean, value) },
  date: -> value { value && Date.parse(value) },
  decimal: -> value { value && BigDecimal.new(value) },
  float: -> value { value && value.to_f },
  integer: -> value { value && value.to_i },
  datetime: -> value { value && Time.at(value.to_i).in_time_zone }
}

Class Method Summary collapse

Class Method Details

.deserialize(type, value, deserializer = ) ⇒ Object



42
43
44
45
46
# File 'lib/hstore_accessor/serialization.rb', line 42

def deserialize(type, value, deserializer=DESERIALIZERS[type])
  return nil if value.nil?

  deserializer.call(value)
end

.serialize(type, value, serializer = ) ⇒ Object



36
37
38
39
40
# File 'lib/hstore_accessor/serialization.rb', line 36

def serialize(type, value, serializer=SERIALIZERS[type])
  return nil if value.nil?

  serializer.call(value)
end