Class: Typedcsv::Headers

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Headers

Returns a new instance of Headers.



48
49
50
# File 'lib/typedcsv.rb', line 48

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



47
48
49
# File 'lib/typedcsv.rb', line 47

def raw
  @raw
end

Instance Method Details

#parse_array(row) ⇒ Object



61
62
63
64
65
# File 'lib/typedcsv.rb', line 61

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

#parse_hash(row) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/typedcsv.rb', line 66

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/typedcsv.rb', line 51

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