Class: LargeCsvReader
- Inherits:
-
Object
- Object
- LargeCsvReader
- Defined in:
- lib/large_csv_reader.rb
Instance Attribute Summary collapse
-
#books_in_stock ⇒ Object
Returns the value of attribute books_in_stock.
Instance Method Summary collapse
- #append_book_to_csv(filename, rows = 1000000) ⇒ Object
- #append_to_csv(filename, rows = 1000000, rowStructure) ⇒ Object
- #book_generator ⇒ Object
- #generate_csv(filename, column_names) ⇒ Object
-
#initialize ⇒ LargeCsvReader
constructor
A new instance of LargeCsvReader.
- #massive_book_csv_builder(filename, column_names, rowMultiplicator = "1") ⇒ Object
- #massive_csv_builder(filename, column_names, rowStructure, rowMultiplicator = "1") ⇒ Object
- #massive_number_of_each_isbn(csv_file_name) ⇒ Object
- #massive_read_in_csv_data(csv_file_name) ⇒ Object
- #massive_total_value_in_stock(csv_file_name) ⇒ Object
- #number_of_each_isbn(counter_hash, bookInfo) ⇒ Object
- #row_generator(structure) ⇒ Object
Constructor Details
#initialize ⇒ LargeCsvReader
7 8 9 |
# File 'lib/large_csv_reader.rb', line 7 def initialize @books_in_stock = [] end |
Instance Attribute Details
#books_in_stock ⇒ Object
Returns the value of attribute books_in_stock.
6 7 8 |
# File 'lib/large_csv_reader.rb', line 6 def books_in_stock @books_in_stock end |
Instance Method Details
#append_book_to_csv(filename, rows = 1000000) ⇒ Object
33 34 35 36 37 |
# File 'lib/large_csv_reader.rb', line 33 def append_book_to_csv(filename,rows=1000000) CSV.open(filename, "ab") do |csv| self.book_generator.lazy.select {|book| csv << [Date.new(2001,2,3), book.isbn, book.price] }.first(rows) end end |
#append_to_csv(filename, rows = 1000000, rowStructure) ⇒ Object
39 40 41 42 43 |
# File 'lib/large_csv_reader.rb', line 39 def append_to_csv(filename,rows=1000000,rowStructure) CSV.open(filename, "ab") do |csv| self.row_generator(rowStructure).lazy.select {|row| csv << row }.first(rows) end end |
#book_generator ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/large_csv_reader.rb', line 69 def book_generator Enumerator.new do |caller| testBook = BookInStock.new("978-1-9343561-0-4", 20.05) loop do caller.yield testBook end end end |
#generate_csv(filename, column_names) ⇒ Object
27 28 29 30 31 |
# File 'lib/large_csv_reader.rb', line 27 def generate_csv(filename,column_names) CSV.open(filename, "ab") do |csv| csv << column_names end end |
#massive_book_csv_builder(filename, column_names, rowMultiplicator = "1") ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/large_csv_reader.rb', line 19 def massive_book_csv_builder(filename,column_names,rowMultiplicator="1") generate_csv(filename,column_names) millions = rowMultiplicator.to_i millions.times do append_book_to_csv(filename) end end |
#massive_csv_builder(filename, column_names, rowStructure, rowMultiplicator = "1") ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/large_csv_reader.rb', line 11 def massive_csv_builder(filename,column_names,rowStructure,rowMultiplicator="1") generate_csv(filename,column_names) millions = rowMultiplicator.to_i millions.times do append_to_csv(filename, rowStructure) end end |
#massive_number_of_each_isbn(csv_file_name) ⇒ Object
53 54 55 56 57 |
# File 'lib/large_csv_reader.rb', line 53 def massive_number_of_each_isbn(csv_file_name) counter_hash = {} CSV.foreach(csv_file_name, headers: true).each_slice(1).each {|row| number_of_each_isbn(counter_hash, row) } return counter_hash end |
#massive_read_in_csv_data(csv_file_name) ⇒ Object
45 46 47 |
# File 'lib/large_csv_reader.rb', line 45 def massive_read_in_csv_data(csv_file_name) CSV.foreach(csv_file_name, headers: true).each_slice(1).each {|row| @books_in_stock << BookInStock.new(row[0][1], row[0][2])} end |
#massive_total_value_in_stock(csv_file_name) ⇒ Object
49 50 51 |
# File 'lib/large_csv_reader.rb', line 49 def massive_total_value_in_stock(csv_file_name) CSV.foreach(csv_file_name, headers: true).each_slice(1).each.inject(0) {|sum, row| sum + row[0][2].to_f } end |
#number_of_each_isbn(counter_hash, bookInfo) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/large_csv_reader.rb', line 59 def number_of_each_isbn(counter_hash, bookInfo) isbn = bookInfo[0][1] if counter_hash.has_key?(isbn) counter_hash[isbn] = counter_hash[isbn]+1 else counter_hash[isbn] = 1 end counter_hash end |
#row_generator(structure) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/large_csv_reader.rb', line 78 def row_generator(structure) Enumerator.new do |caller| row = structure loop do caller.yield row end end end |