Class: Xlgrep::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/xlgrep/context.rb

Constant Summary collapse

BASE_CHAR_ORDER =
'A'.ord

Instance Method Summary collapse

Constructor Details

#initialize(predicates) ⇒ Context

Returns a new instance of Context.



5
6
7
# File 'lib/xlgrep/context.rb', line 5

def initialize(predicates)
  @predicates = predicates
end

Instance Method Details

#book_for(file) ⇒ Object



9
10
11
# File 'lib/xlgrep/context.rb', line 9

def book_for(file)
  Roo::Spreadsheet.open(file)
end

#execute(files) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xlgrep/context.rb', line 15

def execute(files)
  result = []
  files.each do |f|
    book = book_for(f)
    book.sheets.each do |sheet|
      book.default_sheet = sheet
      (book.first_row..book.last_row).each do |r|
        cells = book.row(r)
        cells.each.with_index do |cell, idx|
          @predicates.each do |pred|
            pred.match(cell) do |msg|
              a, b = idx.divmod(26) # ('A'..'Z').length => 26
              x = (a > 0 ? (BASE_CHAR_ORDER + a).chr : "") + (BASE_CHAR_ORDER + b).chr
              result << {
                file: f, sheet: sheet,
                x: x, y: r,
                data: cell,
                msg: msg
              }
            end
          end
        end
      end
    end
  end
  result
end