Module: SimpleEtl::Source::FieldCaster

Extended by:
FieldCaster
Included in:
FieldCaster
Defined in:
lib/simple_etl/source/field_caster.rb

Instance Method Summary collapse

Instance Method Details

#parse_boolean(o) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simple_etl/source/field_caster.rb', line 12

def parse_boolean o
  if o.nil? || o =~ /^\s*$/
    nil
  else
    if %w(true 1).include? o.strip
      true
    elsif %w(false 0).include? o.strip
      false
    else
      raise(CastError.new "Cannot cast '#{o}' to 'boolean'")
    end
  end
end

#parse_float(o) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_etl/source/field_caster.rb', line 38

def parse_float o
  if o.nil? || o.to_s =~ /^\s*$/
    nil
  else
    if o.to_s =~ /^\s*\d*([\.\,]\d+)?\s*$/
      o.to_s.gsub(/\,/, '.').to_f
    else
      raise(CastError.new "Cannot cast '#{o}' to 'float'")
    end
  end
end

#parse_integer(o) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_etl/source/field_caster.rb', line 26

def parse_integer o
  if o.nil? || o.to_s =~ /^\s*$/
    nil
  else
    if o.to_s =~ /^\s*\d+\s*$/
      o.to_i
    else
      raise(CastError.new "Cannot cast '#{o}' to 'integer'")
    end
  end
end

#parse_object(o) ⇒ Object



6
# File 'lib/simple_etl/source/field_caster.rb', line 6

def parse_object o; o; end

#parse_string(o) ⇒ Object



8
9
10
# File 'lib/simple_etl/source/field_caster.rb', line 8

def parse_string o
  o && o.strip
end