Module: Eatr::ParseValue

Included in:
Csv::Document, Xml::Document
Defined in:
lib/eatr/parse_value.rb

Instance Method Summary collapse

Instance Method Details

#parse_value(field, text) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eatr/parse_value.rb', line 3

def parse_value(field, text)
  case field.type
  when 'integer' then text.to_i
  when 'float' then text.to_f
  when 'timestamp'
    if field.strptime
      DateTime.strptime(text, field.strptime)
    else
      DateTime.parse(text)
    end
  when 'boolean' then YAML.load(text)
  else
    if field.max_length
      text[0...field.max_length]
    elsif field.length
      text[0...field.length]
    else
      text
    end
  end
end