Class: Xlgrep::Context
- Inherits:
-
Object
- Object
- Xlgrep::Context
- Defined in:
- lib/xlgrep/context.rb
Constant Summary collapse
- BASE_CHAR_ORDER =
'A'.ord
Instance Method Summary collapse
- #book_for(file) ⇒ Object
- #execute(files) ⇒ Object
-
#initialize(predicates) ⇒ Context
constructor
A new instance of Context.
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 12 13 14 15 16 17 18 19 20 |
# File 'lib/xlgrep/context.rb', line 9 def book_for(file) klass, = case ext = File.extname(file) when ".ods" then Roo::OpenOffice when ".xls" then Roo::Excel when ".xlsx" then Roo::Excelx when ".csv" then Roo::CSV when ".tsv" then [Roo::CSV, {csv_options: {col_sep: "\t"}}] else raise ArgumentError, "Unsupported file extension: #{ext.inspect}" end klass.new(file, ) end |
#execute(files) ⇒ Object
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 |
# File 'lib/xlgrep/context.rb', line 24 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 |