Class: CsvFiles::Generator
- Inherits:
-
Object
- Object
- CsvFiles::Generator
- Defined in:
- lib/csv_files.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#items ⇒ Object
Returns the value of attribute items.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#store_fn ⇒ Object
Returns the value of attribute store_fn.
Instance Method Summary collapse
- #<<(row) ⇒ Object
-
#initialize(filename, headers, limit = 10_000) ⇒ Generator
constructor
A new instance of Generator.
- #store ⇒ Object
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
#count ⇒ Object
Returns the value of attribute count.
6 7 8 |
# File 'lib/csv_files.rb', line 6 def count @count end |
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/csv_files.rb', line 6 def filename @filename end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/csv_files.rb', line 6 def headers @headers end |
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/csv_files.rb', line 6 def items @items end |
#limit ⇒ Object
Returns the value of attribute limit.
6 7 8 |
# File 'lib/csv_files.rb', line 6 def limit @limit end |
#store_fn ⇒ Object
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 |
#store ⇒ Object
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 |