Class: DataForge::File::CSV::CSVRecordFileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/data_forge/file/csv/csv_record_file_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ CSVRecordFileReader

Returns a new instance of CSVRecordFileReader.



10
11
12
13
14
# File 'lib/data_forge/file/csv/csv_record_file_reader.rb', line 10

def initialize(definition)
  @definition = definition
  @name = definition.name
  @fields = definition.field_names
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



6
7
8
# File 'lib/data_forge/file/csv/csv_record_file_reader.rb', line 6

def definition
  @definition
end

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/data_forge/file/csv/csv_record_file_reader.rb', line 6

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/data_forge/file/csv/csv_record_file_reader.rb', line 6

def name
  @name
end

Instance Method Details

#each_record(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/data_forge/file/csv/csv_record_file_reader.rb', line 18

def each_record(&block)
  ::CSV.open definition.file_name, csv_options do |csv_file|
    csv_file.shift if definition.has_header_row

    until (row = csv_file.shift).nil?
      block.call Hash[definition.field_names.zip row]
    end
  end
end