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, formatter = nil) ⇒ Context

Returns a new instance of Context.



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

def initialize(predicates, formatter = nil)
  @predicates = predicates
  @formatter = formatter || SimpleFormatter.new
end

Instance Method Details

#book_for(file) ⇒ Object



13
14
15
# File 'lib/xlgrep/context.rb', line 13

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

#execute(files) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xlgrep/context.rb', line 19

def execute(files)
  files.each do |f|
    book = book_for(f)
    book.sheets.each do |sheet|
      print_status("loading #{f} #{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
              print_status("")
              @formatter.process({
                file: f, sheet: sheet,
                x: x, y: r,
                data: cell,
                msg: msg
              })
              print_status("loading #{f} #{sheet}")
            end
          end
        end
      end
    end
    # print_status(" loaded #{f}")
    # puts
  end
  print_status("")
  self
end


53
54
55
56
57
58
59
# File 'lib/xlgrep/context.rb', line 53

def print_status(s)
  return unless $stdout.tty?
  cols, rows = HighLine::SystemExtensions.terminal_size # [columns, lines]
  slen = cols - s.bytesize
  slen = 0 if slen < 0
  $stdout.print("\r" + s + (" " * slen))
end