module ArrayHasher

Constants

VERSION

Public Class Methods

csv_each(path, ext_types = {}, &block) click to toggle source
# File lib/array_hasher.rb, line 28
def csv_each(path, ext_types = {}, &block)
  csv = CSV.open(path)
  formatter = new_formatter(parse_format(csv.gets))
  formatter.types.merge!(ext_types)
  if block
    csv.each { |line| block.call(formatter.parse(line)) }
  else
    Enumerator.new { |y| csv.each { |line| y << formatter.parse(line) } }
  end
end
new_formatter(cols) click to toggle source
# File lib/array_hasher.rb, line 12
def new_formatter(cols)
  Formatter.new(cols)
end
parse_format(definition) click to toggle source
# File lib/array_hasher.rb, line 16
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