Class: Virgola::CSVParser

Inherits:
Object
  • Object
show all
Includes:
ExtractionMethods
Defined in:
lib/virgola.rb,
lib/virgola/csv_parser.rb

Instance Method Summary collapse

Methods included from ExtractionMethods

#all, #count, #each, #in_groups_of

Constructor Details

#initialize(klass, contents) ⇒ CSVParser

Returns a new instance of CSVParser.



7
8
9
10
# File 'lib/virgola/csv_parser.rb', line 7

def initialize(klass, contents)
  @klass    = klass
  @contents = contents
end

Instance Method Details

#extract(csv) ⇒ Object



12
13
14
15
# File 'lib/virgola/csv_parser.rb', line 12

def extract(csv)
  result_set = CSV.parse(csv*"\n")
  result_set.collect { |result_set_row| map(result_set_row) }
end

#map(values) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/virgola/csv_parser.rb', line 17

def map(values)
  mapped_object = @klass.new

  @klass.attributes.each.with_index do |attr, index|
    attr.map(mapped_object, values[index])
  end
  mapped_object.run_callbacks(:map)
  mapped_object
end