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: {}) ⇒ Sheet

Returns a new instance of Sheet.



17
18
19
20
21
22
23
24
25
# File 'lib/see_as_vee/sheet.rb', line 17

def initialize whatever, formatters: {}, checkers: {}
  @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.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



35
36
37
# File 'lib/see_as_vee/sheet.rb', line 35

def [] index, key = nil
  key.nil? ? values[index] : values[index][header_index(key)]
end

#eachObject



39
40
41
42
43
44
45
46
47
# File 'lib/see_as_vee/sheet.rb', line 39

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



31
32
33
# File 'lib/see_as_vee/sheet.rb', line 31

def headers symbolic = false
  symbolic ? @rows.first.map { |s| str_to_sym s } : @rows.first
end

#mapObject



49
50
51
52
53
54
55
# File 'lib/see_as_vee/sheet.rb', line 49

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



57
58
59
# File 'lib/see_as_vee/sheet.rb', line 57

def produce csv: true, xlsx: nil, **params
  [csv && produce_csv(**params), xlsx && produce_xlsx(**params)]
end

#valuesObject



27
28
29
# File 'lib/see_as_vee/sheet.rb', line 27

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