Class: MovableErb::CSV
- Inherits:
-
Object
- Object
- MovableErb::CSV
- Defined in:
- lib/movable_erb.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#hashes ⇒ Object
Returns the value of attribute hashes.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
33 34 35 |
# File 'lib/movable_erb.rb', line 33 def filename @filename end |
#hashes ⇒ Object
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
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
35 36 37 38 39 |
# File 'lib/movable_erb.rb', line 35 def setup(&block) yield self parse! if @filename self end |
#to_hashes ⇒ Object
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 |