Module: PropertySets::Casting

Defined in:
lib/property_sets/casting.rb

Constant Summary collapse

FALSE_VALUES =
["false", "0", "", "off", "n"]

Class Method Summary collapse

Class Method Details

.deserialize(value) ⇒ Object



48
49
50
51
# File 'lib/property_sets/casting.rb', line 48

def deserialize(value)
  return nil if value.nil? || value == "null"
  JSON.parse(value)
end

.read(type, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/property_sets/casting.rb', line 8

def read(type, value)
  return nil if value.nil?

  case type
  when :string
    value
  when :datetime
    Time.parse(value).in_time_zone
  when :float
    value.to_f
  when :integer
    value.to_i
  when :boolean
    !false?(value)
  when :serialized
    # deserialization happens in the model
    value
  end
end

.write(type, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/property_sets/casting.rb', line 28

def write(type, value)
  return nil if value.nil?

  case type
  when :datetime
    if value.is_a?(String)
      value
    else
      value.in_time_zone("UTC").to_s
    end
  when :serialized
    # write the object directly.
    value
  when :boolean
    false?(value) ? "0" : "1"
  else
    value.to_s
  end
end