lib/happymapper/item.rb in happymapper-0.1.2 vs lib/happymapper/item.rb in happymapper-0.1.3

- old
+ new

@@ -17,13 +17,15 @@ def name=(new_name) @name = new_name.to_s end - def from_xml_node(node, namespace=nil) + def from_xml_node(node, namespace = nil) if primitive? - typecast(value_from_xml_node(node, namespace)) + value_from_xml_node(node, namespace) do |value_before_type_cast| + typecast(value_before_type_cast) + end else use_default_namespace = !namespace.nil? type.parse(node, options.merge(:use_default_namespace => use_default_namespace)) end end @@ -75,12 +77,29 @@ node.register_default_namespace(namespace.chop) if namespace if element? depth = options[:deep] ? './/' : '' result = node.find_first("#{depth}#{namespace}#{tag}") - result ? result.content : nil + if result + value = yield(result.content) + if options[:attributes].is_a?(Hash) + result.attributes.each do |xml_attribute| + if attribute_options = options[:attributes][xml_attribute.name.to_sym] + attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result) + result.instance_eval <<-EOV + def value.#{xml_attribute.name} + #{attribute_value.inspect} + end + EOV + end + end + end + value + else + nil + end else - node[tag] + yield(node[tag]) end end end end \ No newline at end of file