Class: Typedcsv

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

Defined Under Namespace

Classes: Headers

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &blk) ⇒ Typedcsv

Returns a new instance of Typedcsv.



20
21
22
23
# File 'lib/typedcsv.rb', line 20

def initialize(*args, &blk)
  @args = args
  @blk = blk
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



17
18
19
# File 'lib/typedcsv.rb', line 17

def args
  @args
end

#blkObject (readonly)

Returns the value of attribute blk.



18
19
20
# File 'lib/typedcsv.rb', line 18

def blk
  @blk
end

Class Method Details

.foreach(*args, &blk) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/typedcsv.rb', line 8

def Typedcsv.foreach(*args, &blk)
  typedcsv = new(*args, &blk)
  if args.last.is_a?(Hash) and args.last[:headers]
    typedcsv.foreach_hash
  else
    typedcsv.foreach_array
  end
end

Instance Method Details

#foreach_arrayObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/typedcsv.rb', line 35

def foreach_array
  headers = nil
  CSV.foreach(*args) do |row|
    unless headers
      headers = Headers.new(row)
      next
    end
    blk.call headers.parse_array(row)
  end
end

#foreach_hashObject



25
26
27
28
29
30
31
32
33
# File 'lib/typedcsv.rb', line 25

def foreach_hash
  headers = nil
  CSV.foreach(*args) do |row|
    unless headers
      headers = Headers.new(row.headers)
    end
    blk.call headers.parse_hash(row)
  end
end