class ODSExtractor::SheetFilterHandler

Public Class Methods

new(sax_handler, filter_definition) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 2
def initialize(sax_handler, filter_definition)
  @handler = sax_handler
  @sheet_name_filter_proc = build_filter_proc(filter_definition)
  @bypass = false
end

Public Instance Methods

build_filter_proc(filter_definition) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 36
def build_filter_proc(filter_definition)
  case filter_definition
  when String
    ->(sheet_name) { sheet_name == filter_definition }
  when Regexp
    ->(sheet_name) { sheet_name =~ filter_definition }
  when Array
    filter_definitions = filter_definition.map { |defn| build_filter_proc(defn) }
    ->(sheet_name) { filter_definitions.any? { |prc| prc.call(sheet_name) } }
  when Method, Proc
    filter_definition.to_proc # Return as is
  else
    "Sheet name filter must be an Array of String|Regexp|callable or a String|Regexp|callable"
  end
end
characters(string) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 26
def characters(string)
  return if @bypass
  @handler.characters(string)
end
end_element(name) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 31
def end_element(name)
  return if @bypass
  @handler.end_element(name)
end
error(string) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 22
def error(string)
  raise ODSExtractor::Error, "XML parse error: #{string}"
end
start_element(name, attributes = []) click to toggle source
# File lib/ods_extractor/sheet_filter_handler.rb, line 8
def start_element(name, attributes = [])
  if name == "table:table"
    sheet_name = attributes.to_h.fetch("table:name")
    if @sheet_name_filter_proc.call(sheet_name)
      @bypass = false
      @handler.start_element(name, attributes)
    else
      @bypass = true
    end
  end

  @handler.start_element(name, attributes) unless @bypass
end