Class: SeeAsVee::Sheet
- Inherits:
-
Object
- Object
- SeeAsVee::Sheet
- Defined in:
- lib/see_as_vee/sheet.rb
Constant Summary collapse
- CELL_ERROR_MARKER =
'⚑ '.freeze
- CELL_ERROR_STYLE =
{ bg_color: 'FF880000', fg_color: 'FFFFFFFF', sz: 14, border: { style: :thin, color: 'FFFF0000' } }.freeze
- WORK_SHEET_NAME =
'Processing errors shown in red'.freeze
- LEAVE_ERROR_MARKER =
true
Instance Attribute Summary collapse
-
#checkers ⇒ Object
readonly
Returns the value of attribute checkers.
-
#formatters ⇒ Object
readonly
Returns the value of attribute formatters.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #[](index, key = nil) ⇒ Object
- #each ⇒ Object
- #headers(symbolic = false) ⇒ Object
-
#initialize(whatever, formatters: {}, checkers: {}, skip_blank_rows: false) ⇒ Sheet
constructor
A new instance of Sheet.
- #map ⇒ Object
- #produce(csv: true, xlsx: nil, **params) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(whatever, formatters: {}, checkers: {}, skip_blank_rows: false) ⇒ Sheet
Returns a new instance of Sheet.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/see_as_vee/sheet.rb', line 17 def initialize whatever, formatters: {}, checkers: {}, skip_blank_rows: false @formatters = formatters.map { |k, v| [str_to_sym(k), v] }.to_h @checkers = checkers.map { |k, v| [str_to_sym(k), v] }.to_h @rows = whatever.is_a?(Array) ? whatever : Helpers.harvest_csv(whatever) @rows = @rows.map do |row| row unless skip_blank_rows && row.compact.empty? end.compact.map.with_index do |row, idx| idx.zero? ? row : plough_row(row) end end |
Instance Attribute Details
#checkers ⇒ Object (readonly)
Returns the value of attribute checkers.
15 16 17 |
# File 'lib/see_as_vee/sheet.rb', line 15 def checkers @checkers end |
#formatters ⇒ Object (readonly)
Returns the value of attribute formatters.
15 16 17 |
# File 'lib/see_as_vee/sheet.rb', line 15 def formatters @formatters end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
15 16 17 |
# File 'lib/see_as_vee/sheet.rb', line 15 def rows @rows end |
Instance Method Details
#[](index, key = nil) ⇒ Object
37 38 39 |
# File 'lib/see_as_vee/sheet.rb', line 37 def [] index, key = nil key.nil? ? values[index] : values[index][header_index(key)] end |
#each ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/see_as_vee/sheet.rb', line 41 def each return enum_for unless block_given? values.each_with_index do |row, idx| result = headers.zip(row).to_h errors = result.select { |_, v| malformed?(v) } yield idx, errors, result end end |
#headers(symbolic = false) ⇒ Object
33 34 35 |
# File 'lib/see_as_vee/sheet.rb', line 33 def headers symbolic = false symbolic ? @rows.first.map { |s| str_to_sym s } : @rows.first end |
#map ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/see_as_vee/sheet.rb', line 51 def map return enum_for unless block_given? values.map do |row| yield headers(true).zip(row).to_h end end |
#produce(csv: true, xlsx: nil, **params) ⇒ Object
59 60 61 |
# File 'lib/see_as_vee/sheet.rb', line 59 def produce csv: true, xlsx: nil, **params [csv && produce_csv(**params), xlsx && produce_xlsx(**params)] end |
#values ⇒ Object
29 30 31 |
# File 'lib/see_as_vee/sheet.rb', line 29 def values @rows[1..-1] end |