Module: Organ::Coercer

Included in:
Form
Defined in:
lib/organ/coercer.rb

Instance Method Summary collapse

Instance Method Details

#coerce_array(value, options = {}) ⇒ Array

Coerce the value into an Array.

Parameters:

  • value (Object)

    The value to be coerced.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :element_type (Symbol) — default: nil

    The type of the value to coerce each element of the array. No coercion done if type is nil.

Returns:

  • (Array)

    The coerced Array.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/organ/coercer.rb', line 47

def coerce_array(value, options = {})
  element_type = options[:element_type]
  value = value.values if value.kind_of?(Hash)
  if value.kind_of?(Array)
    if element_type
      value.map do |array_element|
        send("coerce_#{element_type}", array_element)
      end
    else
      value
    end
  else
    []
  end
end

#coerce_boolean(value, options = {}) ⇒ Boolean

Corce the value into true or false.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


31
32
33
# File 'lib/organ/coercer.rb', line 31

def coerce_boolean(value, options = {})
  !!value
end

#coerce_date(value, options = {}) ⇒ Date?

Coerce the value into a Date.

Parameters:

  • value (Object)

    The value to be coerced.

Returns:

  • (Date, nil)

    A Date if the value can be coerced or nil otherwise.



128
129
130
131
132
133
134
135
# File 'lib/organ/coercer.rb', line 128

def coerce_date(value, options = {})
  value = coerce_string(value)
  begin
    Date.strptime(value, "%Y-%m-%d")
  rescue ArgumentError
    nil
  end
end

#coerce_float(value, options = {}) ⇒ Float?

Coerce the value into a Float.

Parameters:

  • value (Object)

Returns:

  • (Float, nil)


70
71
72
# File 'lib/organ/coercer.rb', line 70

def coerce_float(value, options = {})
  Float(value) rescue nil
end

#coerce_hash(value, options = {}) ⇒ Hash

Coerce the value into a Hash.

Parameters:

  • value (Object)

    The value to be coerced.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :key_type (Symbol) — default: nil

    The type of the hash keys to coerce, no coersion done if type is nil.

Returns:

  • (Hash)

    The coerced Hash.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/organ/coercer.rb', line 87

def coerce_hash(value, options = {})
  key_type = options[:key_type]
  value_type = options[:value_type]
  if value.kind_of?(Hash)
    value.each_with_object({}) do |(key, value), coerced_hash|
      key = send("coerce_#{key_type}", key) if key_type
      value = send("coerce_#{value_type}", value) if value_type
      coerced_hash[key] = value
    end
  else
    {}
  end
end

#coerce_integer(value, options = {}) ⇒ Integer?

Coerce the value into an Integer.

Parameters:

  • value (Object)

    The value to be coerced.

Returns:

  • (Integer, nil)

    An Integer if the value can be coerced or nil otherwise.



110
111
112
113
114
115
116
117
# File 'lib/organ/coercer.rb', line 110

def coerce_integer(value, options = {})
  value = value.to_s
  if value.match(/\A0|[1-9]\d*\z/)
    value.to_i
  else
    nil
  end
end

#coerce_string(value, options = {}) ⇒ String?

Coerce the value into a String or nil if no value given.

Parameters:

  • value (Object)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :trim (Boolan) — default: false

    If true, it strips the preceding/trailing whitespaces and newlines. It also replaces multiple consecutive spaces into one.

Returns:

  • (String, nil)


16
17
18
19
20
21
22
# File 'lib/organ/coercer.rb', line 16

def coerce_string(value, options = {})
  value = value ? value.to_s : nil
  if value && options[:trim]
    value = value.strip.gsub(/\s{2,}/, " ")
  end
  value
end