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
44 45 46 |
# File 'lib/see_as_vee/sheet.rb', line 44 def [] index, key = nil key.nil? ? values[index] : values[index][header_index(key)] end |
#each ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/see_as_vee/sheet.rb', line 48 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 36 37 38 39 40 41 42 |
# File 'lib/see_as_vee/sheet.rb', line 33 def headers symbolic = false headers = @rows.first unless headers.uniq.length == headers.length groups = headers.group_by { |h| h }.select { |_, group| group.size > 1 } headers = headers.map.with_index { |e, idx| groups[e].nil? ? e : "#{e} #{idx}" } end headers = headers.map.with_index { |s, ind| str_to_sym(s || "col #{ind}") } if symbolic headers end |
#map ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/see_as_vee/sheet.rb', line 58 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
66 67 68 |
# File 'lib/see_as_vee/sheet.rb', line 66 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 |