Class: BatchFactory::Parser
- Inherits:
-
Object
- Object
- BatchFactory::Parser
- Defined in:
- lib/batch_factory/parser.rb
Instance Attribute Summary collapse
-
#column_bounds ⇒ Object
Returns the value of attribute column_bounds.
-
#heading_keys ⇒ Object
Returns the value of attribute heading_keys.
-
#row_hashes ⇒ Object
Returns the value of attribute row_hashes.
-
#user_heading_keys ⇒ Object
Returns the value of attribute user_heading_keys.
-
#worksheet ⇒ Object
Returns the value of attribute worksheet.
Instance Method Summary collapse
- #hashed_worksheet ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #open(file_location, sheet_number = 0, user_keys = nil) ⇒ Object
- #parse! ⇒ Object
- #parse_column_bounds ⇒ Object
- #parse_data_rows ⇒ Object
- #parse_heading_keys ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 |
# File 'lib/batch_factory/parser.rb', line 5 def initialize @heading_keys = [] @column_bounds = [] @row_hashes = [] end |
Instance Attribute Details
#column_bounds ⇒ Object
Returns the value of attribute column_bounds.
3 4 5 |
# File 'lib/batch_factory/parser.rb', line 3 def column_bounds @column_bounds end |
#heading_keys ⇒ Object
Returns the value of attribute heading_keys.
3 4 5 |
# File 'lib/batch_factory/parser.rb', line 3 def heading_keys @heading_keys end |
#row_hashes ⇒ Object
Returns the value of attribute row_hashes.
3 4 5 |
# File 'lib/batch_factory/parser.rb', line 3 def row_hashes @row_hashes end |
#user_heading_keys ⇒ Object
Returns the value of attribute user_heading_keys.
3 4 5 |
# File 'lib/batch_factory/parser.rb', line 3 def user_heading_keys @user_heading_keys end |
#worksheet ⇒ Object
Returns the value of attribute worksheet.
3 4 5 |
# File 'lib/batch_factory/parser.rb', line 3 def worksheet @worksheet end |
Instance Method Details
#hashed_worksheet ⇒ Object
56 57 58 59 60 61 |
# File 'lib/batch_factory/parser.rb', line 56 def hashed_worksheet @hashed_worksheet ||= HashedWorksheet.new( @heading_keys, @row_hashes ) end |
#open(file_location, sheet_number = 0, user_keys = nil) ⇒ Object
11 12 13 14 |
# File 'lib/batch_factory/parser.rb', line 11 def open(file_location, sheet_number = 0, user_keys = nil) @worksheet = SpreadsheetDocument.new(file_location, sheet_number) @user_heading_keys = user_keys end |
#parse! ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/batch_factory/parser.rb', line 16 def parse! @hashed_worksheet = nil parse_column_bounds parse_heading_keys parse_data_rows end |
#parse_column_bounds ⇒ Object
30 31 32 |
# File 'lib/batch_factory/parser.rb', line 30 def parse_column_bounds @column_bounds = (@worksheet.first_column-1)..(@worksheet.last_column-1) end |
#parse_data_rows ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/batch_factory/parser.rb', line 34 def parse_data_rows @row_hashes = [] first_row_offset = user_heading_keys ? 0 : 1 rows_range = ((worksheet.first_row+first_row_offset)..worksheet.last_row) rows_range.each do |idx| row = worksheet.row(idx) hash = HashWithIndifferentAccess.new for cell_index in @column_bounds if key = @heading_keys[cell_index] and value = row[cell_index] hash[key] = value if value.present? end end @row_hashes << hash unless hash.empty? end end |
#parse_heading_keys ⇒ Object
24 25 26 27 28 |
# File 'lib/batch_factory/parser.rb', line 24 def parse_heading_keys @heading_keys = user_heading_keys || worksheet.row(worksheet.first_row).map do |key| key.blank? ? nil : key.strip end end |