Class: Typedcsv::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/typedcsv.rb

Constant Summary collapse

TRUE =
'true'
FALSE =
'false'
EMPTY_STRING =
''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Headers

Returns a new instance of Headers.



51
52
53
# File 'lib/typedcsv.rb', line 51

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



50
51
52
# File 'lib/typedcsv.rb', line 50

def raw
  @raw
end

Instance Method Details

#parse_array(row) ⇒ Object



64
65
66
67
68
# File 'lib/typedcsv.rb', line 64

def parse_array(row)
  types.map do |k, type, _, i|
    convert type, row[i]
  end
end

#parse_hash(row) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/typedcsv.rb', line 69

def parse_hash(row)
  types.inject({}) do |memo, (k, type, orig_k, _)|
    v = row.fetch orig_k
    memo[k] = convert(type, v)
    memo
  end
end

#typesObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/typedcsv.rb', line 54

def types
  @types ||= raw.each_with_index.map do |raw_k, i|
    k, type = raw_k.split(':', 2)
    if type
      [k, type, "#{k}:#{type}", i]
    else
      [k, 'text', k, i]
    end
  end
end