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
- #cdata_block(string) ⇒ Object
- #characaters_captured? ⇒ Boolean
- #characters(string) ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize(object) ⇒ SAXHandler
constructor
A new instance of SAXHandler.
- #mark_as_parsed(element_config = nil) ⇒ Object
- #parse_collection_instance_attributes ⇒ Object
- #parse_element_attributes(element_configs) ⇒ Object
- #parsed_config?(element_config = nil) ⇒ Boolean
- #parsing_collection? ⇒ Boolean
- #reset_current_collection ⇒ Object
- #reset_current_tag ⇒ Object
- #sax_config ⇒ Object
- #set_element_config_for_element_value ⇒ 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
#cdata_block(string) ⇒ Object
20 21 22 |
# File 'lib/sax-machine/sax_handler.rb', line 20 def cdata_block(string) characters(string) end |
#characaters_captured? ⇒ Boolean
60 61 62 |
# File 'lib/sax-machine/sax_handler.rb', line 60 def characaters_captured? !@value.nil? && !@value.empty? 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sax-machine/sax_handler.rb', line 44 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(element_config = nil) ⇒ Object
90 91 92 93 |
# File 'lib/sax-machine/sax_handler.rb', line 90 def mark_as_parsed(element_config=nil) element_config ||= @element_config @parsed_configs[element_config] = true unless element_config.collection? end |
#parse_collection_instance_attributes ⇒ Object
68 69 70 71 72 73 |
# File 'lib/sax-machine/sax_handler.rb', line 68 def parse_collection_instance_attributes instance = @collection_handler.object @attrs.each_with_index do |attr_name,index| instance.send("#{attr_name}=", @attrs[index + 1]) if index % 2 == 0 && instance.methods.include?("#{attr_name}=") end end |
#parse_element_attributes(element_configs) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/sax-machine/sax_handler.rb', line 75 def parse_element_attributes(element_configs) element_configs.each do |ec| unless parsed_config?(ec) @object.send(ec.setter, ec.value_from_attrs(@attrs)) mark_as_parsed(ec) end end @element_config = nil end |
#parsed_config?(element_config = nil) ⇒ Boolean
95 96 97 98 |
# File 'lib/sax-machine/sax_handler.rb', line 95 def parsed_config?(element_config=nil) element_config ||= @element_config @parsed_configs[element_config] end |
#parsing_collection? ⇒ Boolean
64 65 66 |
# File 'lib/sax-machine/sax_handler.rb', line 64 def parsing_collection? !@collection_handler.nil? end |
#reset_current_collection ⇒ Object
100 101 102 103 |
# File 'lib/sax-machine/sax_handler.rb', line 100 def reset_current_collection @collection_handler = nil @collection_config = nil end |
#reset_current_tag ⇒ Object
105 106 107 108 109 110 |
# File 'lib/sax-machine/sax_handler.rb', line 105 def reset_current_tag @name = nil @attrs = nil @value = nil @element_config = nil end |
#sax_config ⇒ Object
112 113 114 |
# File 'lib/sax-machine/sax_handler.rb', line 112 def sax_config @object.class.sax_config end |
#set_element_config_for_element_value ⇒ Object
85 86 87 88 |
# File 'lib/sax-machine/sax_handler.rb', line 85 def set_element_config_for_element_value @value = "" @element_config = sax_config.element_config_for_tag(@name, @attrs) end |
#start_element(name, attrs = []) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sax-machine/sax_handler.rb', line 24 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 @collection_handler.start_element(@name, @attrs) elsif (element_configs = sax_config.element_configs_for_attribute(@name, @attrs)).any? parse_element_attributes(element_configs) set_element_config_for_element_value else set_element_config_for_element_value end end |