Class: CsvFiles::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, headers, limit = 10_000) ⇒ Generator



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/csv_files.rb', line 8

def initialize(filename, headers, limit = 10_000)
  raise "Filename must be present" if filename.nil? || filename.to_s == ""
  raise "Headers must be present" if headers.nil?
  raise "Headers must be an array" if !headers.is_a?(Array) || headers.size.zero?
  raise "Limit must be a number" if !limit.is_a?(Numeric)

  self.filename = formatted_filename(filename.to_s)
  self.limit = limit.round
  self.headers = headers
  self.items = Array.new()
  self.count = 0
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



6
7
8
# File 'lib/csv_files.rb', line 6

def count
  @count
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/csv_files.rb', line 6

def filename
  @filename
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/csv_files.rb', line 6

def headers
  @headers
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/csv_files.rb', line 6

def items
  @items
end

#limitObject

Returns the value of attribute limit.



6
7
8
# File 'lib/csv_files.rb', line 6

def limit
  @limit
end

#store_fnObject

Returns the value of attribute store_fn.



6
7
8
# File 'lib/csv_files.rb', line 6

def store_fn
  @store_fn
end

Instance Method Details

#<<(row) ⇒ Object



21
22
23
24
25
# File 'lib/csv_files.rb', line 21

def <<(row)
  @items << row
  store if limit_reached?
  row
end

#storeObject



27
28
29
30
31
32
33
34
35
# File 'lib/csv_files.rb', line 27

def store
  csv = CSV.generate do |r|
    r << @headers
    @items.each { |i| r << i }
  end
  store_fn.call(generated_filename, csv)
  @items.clear
  @count += 1
end