Class: HappyMapper::Element
Instance Attribute Summary
Attributes inherited from Item
#name, #namespace, #options, #tag, #type
Instance Method Summary collapse
- #find(node, namespace, xpath_options) ⇒ Object
- #handle_attributes_option(result, value, xpath_options) ⇒ Object
Methods inherited from Item
#constant, #from_xml_node, #initialize, #method_name, #typecast, #xpath
Constructor Details
This class inherits a constructor from HappyMapper::Item
Instance Method Details
#find(node, namespace, xpath_options) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/happymapper/element.rb', line 3 def find(node, namespace, ) if self.namespace # from the class definition namespace = self.namespace elsif [:namespace] namespace = [:namespace] end if [:single] if [:xpath] result = node.xpath([:xpath], ) else result = node.xpath(xpath(namespace), ) end if result value = yield(result.first) handle_attributes_option(result, value, ) value end else target_path = [:xpath] ? [:xpath] : xpath(namespace) node.xpath(target_path, ).collect do |result| value = yield(result) handle_attributes_option(result, value, ) value end end end |
#handle_attributes_option(result, value, xpath_options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/happymapper/element.rb', line 33 def handle_attributes_option(result, value, ) if [:attributes].is_a?(Hash) result = result.first unless result.respond_to?(:attribute_nodes) return unless result.respond_to?(:attribute_nodes) result.attribute_nodes.each do |xml_attribute| if = [:attributes][xml_attribute.name.to_sym] attribute_value = Attribute.new(xml_attribute.name.to_sym, *).from_xml_node(result, namespace, ) result.instance_eval <<-EOV def value.#{xml_attribute.name.gsub(/\-/, '_')} #{attribute_value.inspect} end EOV end # if attributes_options end # attribute_nodes.each end # if options[:attributes] end |