Class: SimpleXlsxReader::Document::RowsProxy
- Inherits:
-
Object
- Object
- SimpleXlsxReader::Document::RowsProxy
- Includes:
- Enumerable
- Defined in:
- lib/simple_xlsx_reader/document.rb
Overview
Waits until we call #each with a block to parse the rows
Instance Attribute Summary collapse
-
#load_errors ⇒ Object
readonly
Returns the value of attribute load_errors.
-
#slurped ⇒ Object
readonly
Returns the value of attribute slurped.
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#each(headers: false, &block) ⇒ Object
By default, #each streams the rows to the provided block, either as arrays, or as header => cell value pairs if provided a
headers:argument. -
#initialize(sheet_parser:) ⇒ RowsProxy
constructor
A new instance of RowsProxy.
- #shift(*args) ⇒ Object
-
#slurp ⇒ Object
Mostly for legacy support, I’m not aware of a use case for doing this when you don’t have to.
- #slurped! ⇒ Object
- #slurped? ⇒ Boolean
Constructor Details
#initialize(sheet_parser:) ⇒ RowsProxy
Returns a new instance of RowsProxy.
60 61 62 63 64 |
# File 'lib/simple_xlsx_reader/document.rb', line 60 def initialize(sheet_parser:) @sheet_parser = sheet_parser @slurped = nil @load_errors = {} end |
Instance Attribute Details
#load_errors ⇒ Object (readonly)
Returns the value of attribute load_errors.
58 59 60 |
# File 'lib/simple_xlsx_reader/document.rb', line 58 def load_errors @load_errors end |
#slurped ⇒ Object (readonly)
Returns the value of attribute slurped.
58 59 60 |
# File 'lib/simple_xlsx_reader/document.rb', line 58 def slurped @slurped end |
Instance Method Details
#[](*args) ⇒ Object
126 127 128 129 130 |
# File 'lib/simple_xlsx_reader/document.rb', line 126 def [](*args) check_slurped slurped[*args] end |
#each(headers: false, &block) ⇒ Object
By default, #each streams the rows to the provided block, either as arrays, or as header => cell value pairs if provided a headers: argument.
headers can be:
-
true- simply takes the first row as the header row -
block - calls the block with successive rows until the block returns true, which it then uses that row for the headers. All data prior to finding the headers is ignored.
-
hash - transforms the header row by replacing cells with keys matched by value, ex. ‘/ID|Identity/, name: /Name/i, date: ’Date’‘ would potentially yield the row `5, name: ’Jane’, date: [Date object]‘ instead of the headers from the sheet. It would also search for the row that matches at least one header, in case the header row isn’t the first.
If rows have been slurped, #each will iterate the slurped rows instead.
Note, calls to this after slurping will raise if given the headers: argument, as that’s handled by the sheet parser. If this is important to someone, speak up and we could potentially support it.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simple_xlsx_reader/document.rb', line 88 def each(headers: false, &block) if slurped? raise '#each does not support headers with slurped rows' if headers slurped.each(&block) elsif block_given? # It's possible to slurp while yielding to the block, which would # null out @sheet_parser, so let's just keep track of it here too sheet_parser = @sheet_parser @sheet_parser.parse(headers: headers, &block).tap do @load_errors = sheet_parser.load_errors end else to_enum(:each, headers: headers) end end |
#shift(*args) ⇒ Object
132 133 134 135 136 |
# File 'lib/simple_xlsx_reader/document.rb', line 132 def shift(*args) check_slurped slurped.shift(*args) end |
#slurp ⇒ Object
Mostly for legacy support, I’m not aware of a use case for doing this when you don’t have to.
Note that #each will use slurped results if available, and since we’re leveraging Enumerable, all the other Enumerable methods will too.
110 111 112 113 114 |
# File 'lib/simple_xlsx_reader/document.rb', line 110 def slurp # possibly release sheet parser from memory on next GC run; # untested, but it can hold a lot of stuff, so worth a try @slurped ||= to_a.tap { @sheet_parser = nil } end |
#slurped! ⇒ Object
120 121 122 123 124 |
# File 'lib/simple_xlsx_reader/document.rb', line 120 def slurped! check_slurped slurped end |
#slurped? ⇒ Boolean
116 117 118 |
# File 'lib/simple_xlsx_reader/document.rb', line 116 def slurped? !!@slurped end |