Class: SAXMachine::SAXHandler
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- SAXMachine::SAXHandler
- Defined in:
- lib/sax-machine/sax_handler.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #characaters_captured? ⇒ Boolean
- #characters(string) ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize(object) ⇒ SAXHandler
constructor
A new instance of SAXHandler.
- #mark_as_parsed ⇒ Object
- #parse_element_attribute ⇒ Object
- #parsed_config? ⇒ Boolean
- #parsing_collection? ⇒ Boolean
- #reset_current_collection ⇒ Object
- #reset_current_tag ⇒ Object
- #sax_config ⇒ Object
- #start_element(name, attrs = []) ⇒ Object
Constructor Details
#initialize(object) ⇒ SAXHandler
Returns a new instance of SAXHandler.
7 8 9 10 |
# File 'lib/sax-machine/sax_handler.rb', line 7 def initialize(object) @object = object @parsed_configs = {} end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/sax-machine/sax_handler.rb', line 5 def object @object end |
Instance Method Details
#characaters_captured? ⇒ Boolean
54 55 56 |
# File 'lib/sax-machine/sax_handler.rb', line 54 def characaters_captured? !@value.nil? end |
#characters(string) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/sax-machine/sax_handler.rb', line 12 def characters(string) if parsing_collection? @collection_handler.characters(string) elsif @element_config @value = string end end |
#end_element(name) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sax-machine/sax_handler.rb', line 38 def end_element(name) if parsing_collection? && @collection_config.name == name @object.send(@collection_config.accessor) << @collection_handler.object reset_current_collection elsif parsing_collection? @collection_handler.end_element(name) elsif characaters_captured? && !parsed_config? mark_as_parsed @object.send(@element_config.setter, @value) end reset_current_tag end |
#mark_as_parsed ⇒ Object
71 72 73 |
# File 'lib/sax-machine/sax_handler.rb', line 71 def mark_as_parsed @parsed_configs[@element_config] = true end |
#parse_element_attribute ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/sax-machine/sax_handler.rb', line 62 def parse_element_attribute unless parsed_config? mark_as_parsed @object.send(@element_config.setter, @element_config.value_from_attrs(@attrs)) end @element_config = nil end |
#parsed_config? ⇒ Boolean
75 76 77 |
# File 'lib/sax-machine/sax_handler.rb', line 75 def parsed_config? @parsed_configs[@element_config] end |
#parsing_collection? ⇒ Boolean
58 59 60 |
# File 'lib/sax-machine/sax_handler.rb', line 58 def parsing_collection? !@collection_handler.nil? end |
#reset_current_collection ⇒ Object
79 80 81 82 |
# File 'lib/sax-machine/sax_handler.rb', line 79 def reset_current_collection @collection_handler = nil @collection_config = nil end |
#reset_current_tag ⇒ Object
84 85 86 87 88 89 |
# File 'lib/sax-machine/sax_handler.rb', line 84 def reset_current_tag @name = nil @attrs = nil @value = nil @element_config = nil end |
#sax_config ⇒ Object
91 92 93 |
# File 'lib/sax-machine/sax_handler.rb', line 91 def sax_config @object.class.sax_config end |
#start_element(name, attrs = []) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sax-machine/sax_handler.rb', line 20 def start_element(name, attrs = []) @name = name @attrs = attrs if parsing_collection? @collection_handler.start_element(@name, @attrs) elsif @collection_config = sax_config.collection_config(@name) @collection_handler = @collection_config.handler elsif @element_config = sax_config.element_config_for_attribute(@name, @attrs) parse_element_attribute else @element_config = sax_config.element_config_for_tag(@name, @attrs) end end |