Class: EasySax::Parser
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- EasySax::Parser
- Defined in:
- lib/easy_sax/parser.rb
Instance Attribute Summary collapse
-
#array_elements ⇒ Object
readonly
Returns the value of attribute array_elements.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#element_stack ⇒ Object
readonly
Returns the value of attribute element_stack.
-
#element_text ⇒ Object
readonly
Returns the value of attribute element_text.
-
#ignorable_elements ⇒ Object
readonly
Returns the value of attribute ignorable_elements.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#target_element ⇒ Object
readonly
Returns the value of attribute target_element.
Instance Method Summary collapse
- #cdata_block(string) ⇒ Object
- #characters(string) ⇒ Object
- #end_element(name) ⇒ Object
- #error(string) ⇒ Object
-
#initialize(io) ⇒ Parser
constructor
A new instance of Parser.
- #parse_each(target_element, ignore: [], arrays: [], &block) ⇒ Object
- #start_element(name, attrs = []) ⇒ Object
Constructor Details
#initialize(io) ⇒ Parser
Returns a new instance of Parser.
13 14 15 |
# File 'lib/easy_sax/parser.rb', line 13 def initialize(io) @io = io end |
Instance Attribute Details
#array_elements ⇒ Object (readonly)
Returns the value of attribute array_elements.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def array_elements @array_elements end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def callback @callback end |
#element_stack ⇒ Object (readonly)
Returns the value of attribute element_stack.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def element_stack @element_stack end |
#element_text ⇒ Object (readonly)
Returns the value of attribute element_text.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def element_text @element_text end |
#ignorable_elements ⇒ Object (readonly)
Returns the value of attribute ignorable_elements.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def ignorable_elements @ignorable_elements end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def io @io end |
#target_element ⇒ Object (readonly)
Returns the value of attribute target_element.
5 6 7 |
# File 'lib/easy_sax/parser.rb', line 5 def target_element @target_element end |
Instance Method Details
#cdata_block(string) ⇒ Object
43 44 45 |
# File 'lib/easy_sax/parser.rb', line 43 def cdata_block(string) characters(string) end |
#characters(string) ⇒ Object
39 40 41 |
# File 'lib/easy_sax/parser.rb', line 39 def characters(string) @element_text << string if element_text end |
#end_element(name) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/easy_sax/parser.rb', line 47 def end_element(name) return if ignorable_elements.include?(name) element = element_stack.pop return if element.kind_of?(Array) element.text = element_text.strip if element_text.present? callback.call element, element_stack.first if name == target_element end |
#error(string) ⇒ Object
57 58 59 |
# File 'lib/easy_sax/parser.rb', line 57 def error(string) raise EasySax::ParseError.new(string) end |
#parse_each(target_element, ignore: [], arrays: [], &block) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/easy_sax/parser.rb', line 17 def parse_each(target_element, ignore: [], arrays: [], &block) validate_array(:arrays, arrays) @target_element = target_element.to_s @ignorable_elements = validate_array(:ignore, ignore) @array_elements = validate_array(:arrays, arrays) @element_stack = [] @callback = block Nokogiri::XML::SAX::Parser.new(self).parse(io) end |
#start_element(name, attrs = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/easy_sax/parser.rb', line 27 def start_element(name, attrs = []) return if ignorable_elements.include?(name) @element_text = '' parent = element_stack.last if parent.nil? element_stack << EasySax::SimpleElement.new(name, attrs.to_h) else add_child(parent, name, attrs) end end |