Module: CustomFielder::DeserializationHelper

Included in:
Field, Value
Defined in:
app/helpers/custom_fielder/deserialization_helper.rb

Constant Summary collapse

FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']

Instance Method Summary collapse

Instance Method Details

#array(value) ⇒ Array

Returns:

  • (Array)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 21

def array(value)
  value.delete('[]"').split(', ').map do |val|
    case val
    when /^\d+$/
      val.to_i
    when /^\d+\.\d+$/
      val.to_f
    when /^\d{4}-\d{2}-\d{2}$/
      date(val)
    when /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2}$/
      date_time(val)
    when 'true'
      true
    when 'false'
      false
    when /".+"/
      val.delete('"')
    else
      val
    end
  end
end

#boolean(value) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 75

def boolean(value)
  FALSE_VALUES.include?(value) ? false : true
end

#date(value) ⇒ Date

Returns:

  • (Date)


61
62
63
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 61

def date(value)
  Date.parse(value)
end

#date_time(value) ⇒ DateTime

Returns:

  • (DateTime)


68
69
70
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 68

def date_time(value)
  DateTime.parse(value)
end

#deserialize(type, value) ⇒ Array|Integer|Float|Date|DateTime|Boolean|String

Runs the the deserialization function for the passed type

Parameters:

  • type (String)

    the type to deserialize to

  • value (String)

    the value to be deserialized

Returns:

  • (Array|Integer|Float|Date|DateTime|Boolean|String)


14
15
16
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 14

def deserialize(type, value)
  send("#{type.underscore}", value)
end

#fixnum(value) ⇒ Integer

Returns:

  • (Integer)


47
48
49
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 47

def fixnum(value)
  value.to_i
end

#float(value) ⇒ Float

Returns:

  • (Float)


54
55
56
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 54

def float(value)
  value.to_f
end

#string(value) ⇒ String

Returns:

  • (String)


82
83
84
# File 'app/helpers/custom_fielder/deserialization_helper.rb', line 82

def string(value)
  value
end