Class: Drudgery::Extractors::CSVExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/drudgery/extractors/csv_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, options = {}) ⇒ CSVExtractor

Returns a new instance of CSVExtractor.



6
7
8
9
10
11
# File 'lib/drudgery/extractors/csv_extractor.rb', line 6

def initialize(filepath, options={})
  @filepath = filepath
  @options = { :headers => true }.merge(options)

  @name = "csv:#{File.basename(@filepath)}"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/drudgery/extractors/csv_extractor.rb', line 4

def name
  @name
end

Instance Method Details

#col_sepObject



13
14
15
# File 'lib/drudgery/extractors/csv_extractor.rb', line 13

def col_sep
  @options[:col_sep]
end

#col_sep=(char) ⇒ Object



17
18
19
# File 'lib/drudgery/extractors/csv_extractor.rb', line 17

def col_sep=(char)
  @options[:col_sep] = char
end

#extractObject



21
22
23
24
25
26
27
28
29
# File 'lib/drudgery/extractors/csv_extractor.rb', line 21

def extract
  index = 0

  CSV.foreach(@filepath, @options) do |row|
    yield [row.to_hash, index]

    index += 1
  end
end

#record_countObject



31
32
33
# File 'lib/drudgery/extractors/csv_extractor.rb', line 31

def record_count
  @record_count ||= calculate_record_count
end