Class: MovableErb::CSV

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



33
34
35
# File 'lib/movable_erb.rb', line 33

def filename
  @filename
end

#hashesObject

Returns the value of attribute hashes.



33
34
35
# File 'lib/movable_erb.rb', line 33

def hashes
  @hashes
end

Class Method Details

.setup {|csv| ... } ⇒ Object

Yields:



41
42
43
44
45
# File 'lib/movable_erb.rb', line 41

def self.setup
  csv = self.new
  yield csv
  csv.parse!
end

Instance Method Details

#parse!Object



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

def parse!
  @hashes = self.to_hashes
  self
end

#setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
# File 'lib/movable_erb.rb', line 35

def setup(&block)
  yield self
  parse! if @filename
  self
end

#to_hashesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/movable_erb.rb', line 52

def to_hashes
  array_of_arrays = FasterCSV.read(filename)
  headers = array_of_arrays.shift
  headers.each { |h| h.downcase! && h.gsub!(/\s/,"_") } if headers
  hashes = Array.new(array_of_arrays.length) { Hash.new }
  array_of_arrays.each_with_index do |row,i|
    headers.each_with_index do |header, j|
      unless row[j].nil?
        hashes[i][header] = [] if hashes[i][header].nil?
        hashes[i][header] << row[j]
      end
    end
  end
  hashes
end