Module: ArrayHasher

Defined in:
lib/array_hasher.rb,
lib/array_hasher/version.rb,
lib/array_hasher/formatter.rb

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.csv_each(path, ext_types = {}, &block) ⇒ Object



26
27
28
29
30
31
# File 'lib/array_hasher.rb', line 26

def csv_each(path, ext_types = {}, &block)
  csv = CSV.open(path)
  formatter = new_formatter(parse_format(csv.gets))
  formatter.types.merge!(ext_types)
  csv.each { |line| block.call(formatter.parse(line)) }
end

.new_formatter(cols) ⇒ Object



10
11
12
# File 'lib/array_hasher.rb', line 10

def new_formatter(cols)
  Formatter.new(cols)
end

.parse_format(definition) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/array_hasher.rb', line 14

def parse_format(definition)
  definition.map do |val|
    name, type, opts = val.to_s.split(':', 3)

    [
      (name && name.length > 0) ? name.to_sym : nil,
      (type && type.length > 0) ? type.to_sym : nil,
      (opts && opts =~ /\A\{.*\}\z/) ? JSON.parse(opts) : {}
    ]
  end
end