Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added collections for primitive values. included example class and xml

  • Loading branch information...
commit c5c96d0cdf1f8ec83139377bf29589e3508447a7 1 parent c4ac573
Michael Macasek authored bkeepers committed
View
15 examples/multi_street_address.rb
@@ -0,0 +1,15 @@
+dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
+require File.join(dir, 'happymapper')
+
+file_contents = File.read(dir + '/../spec/fixtures/address_multi_street.xml')
+
+class MultiStreetAddress
+ include HappyMapper
+
+ # allow primitive type to be collection
+ has_many :street_address, String, :tag => "streetaddress"
+ element :city, String
+ element :state_or_providence, String, :tag => "stateOfProvidence"
+ element :zip, String
+ element :country, String
+end
View
16 lib/happymapper/item.rb
@@ -137,10 +137,22 @@ def find(node, namespace, &block)
end
if element?
- result = node.find_first(xpath(namespace), namespace)
+ if(options[:single].nil? || options[:single])
+ result = node.find_first(xpath(namespace), namespace)
+ else
+ result = node.find(xpath(namespace))
+ end
# puts "vfxn: #{xpath} #{result.inspect}"
if result
- value = yield(result)
+ if(options[:single].nil? || options[:single])
+ value = yield(result)
+ else
+ value = []
+
+ result.each do |res|
+ value << yield(res)
+ end
+ end
if options[:attributes].is_a?(Hash)
result.attributes.each do |xml_attribute|
if attribute_options = options[:attributes][xml_attribute.name.to_sym]
View
9 spec/fixtures/multi_street_address.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<address>
+ <streetaddress>123 Smith Dr</streetaddress>
+ <streetaddress>Apt 31</streetaddress>
+ <city>Anytown</city>
+ <stateOrProvince>ST<stateOrProvince>
+ <zip>12345</zip>
+ <country>USA</country>
+</address>
Please sign in to comment.
Something went wrong with that request. Please try again.