Class: SeeAsVee::Sheet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#checkersObject (readonly)

Returns the value of attribute checkers.



15
16
17
# File 'lib/see_as_vee/sheet.rb', line 15

def checkers
  @checkers
end

#formattersObject (readonly)

Returns the value of attribute formatters.



15
16
17
# File 'lib/see_as_vee/sheet.rb', line 15

def formatters
  @formatters
end

#rowsObject (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

#eachObject



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.with_index { |s, ind| str_to_sym(s || "col #{ind}") } : @rows.first
end

#mapObject



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

#valuesObject



29
30
31
# File 'lib/see_as_vee/sheet.rb', line 29

def values
  @rows[1..-1]
end