lib/sax-machine/sax_handler.rb in UnderpantsGnome-sax-machine-0.0.13 vs lib/sax-machine/sax_handler.rb in UnderpantsGnome-sax-machine-0.0.14

- old
+ new

@@ -5,14 +5,17 @@ attr_reader :object def initialize(object) @object = object @parsed_configs = {} + @parsed_complex_configs = {} end def characters(string) - if parsing_collection? + if parsing_complex? + @complex_handler.characters(string) + elsif parsing_collection? @collection_handler.characters(string) elsif @element_config @value << string end end @@ -23,13 +26,20 @@ def start_element(name, attrs = []) @name = name @attrs = attrs - if parsing_collection? + if parsing_complex? + @complex_handler.start_element(@name, @attrs) + + elsif parsing_collection? @collection_handler.start_element(@name, @attrs) + elsif @complex_config = sax_config.complex_config(@name) + @complex_handler = @complex_config.handler + @complex_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? @@ -40,11 +50,19 @@ set_element_config_for_element_value end end def end_element(name) - if parsing_collection? && @collection_config.name == name + if parsing_complex? && @complex_config.name == name && !parsed_complex_config? + complex_mark_as_parsed + @object.send(@complex_config.setter, @complex_handler.object) + reset_current_complex + + elsif parsing_complex? && !parsed_complex_config? + @complex_handler.end_element(name) + + elsif 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) @@ -59,10 +77,14 @@ def characaters_captured? !@value.nil? && !@value.empty? end + def parsing_complex? + !@complex_handler.nil? + end + def parsing_collection? !@collection_handler.nil? end def parse_collection_instance_attributes @@ -95,22 +117,35 @@ def parsed_config?(element_config=nil) element_config ||= @element_config @parsed_configs[element_config] end + def complex_mark_as_parsed + @parsed_complex_configs[@complex_config] = true + end + + def parsed_complex_config? + @parsed_complex_configs[@complex_config] + end + def reset_current_collection @collection_handler = nil @collection_config = nil end + def reset_current_complex + @complex_handler = nil + @complex_config = nil + end + def reset_current_tag - @name = nil - @attrs = nil - @value = nil + @name = nil + @attrs = nil + @value = nil @element_config = nil end def sax_config @object.class.sax_config end end -end \ No newline at end of file +end