Class: ODSExtractor::SheetFilterHandler
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- ODSExtractor::SheetFilterHandler
- Defined in:
- lib/ods_extractor/sheet_filter_handler.rb
Instance Method Summary collapse
- #build_filter_proc(filter_definition) ⇒ Object
- #characters(string) ⇒ Object
- #end_element(name) ⇒ Object
- #error(string) ⇒ Object
-
#initialize(sax_handler, filter_definition) ⇒ SheetFilterHandler
constructor
A new instance of SheetFilterHandler.
- #start_element(name, attributes = []) ⇒ Object
Constructor Details
#initialize(sax_handler, filter_definition) ⇒ SheetFilterHandler
Returns a new instance of SheetFilterHandler.
2 3 4 5 6 |
# 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 |
Instance Method Details
#build_filter_proc(filter_definition) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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) ⇒ Object
26 27 28 29 |
# File 'lib/ods_extractor/sheet_filter_handler.rb', line 26 def characters(string) return if @bypass @handler.characters(string) end |
#end_element(name) ⇒ Object
31 32 33 34 |
# File 'lib/ods_extractor/sheet_filter_handler.rb', line 31 def end_element(name) return if @bypass @handler.end_element(name) end |
#error(string) ⇒ Object
22 23 24 |
# 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 = []) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 |