jimmyz-happymapper 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History +6 -0
- data/happymapper.gemspec +2 -2
- data/lib/happymapper.rb +9 -6
- data/lib/happymapper/version.rb +1 -1
- data/spec/fixtures/family_tree.xml +2 -0
- data/spec/happymapper_spec.rb +21 -1
- metadata +2 -2
data/History
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.3.3
|
2
|
+
* 1 bug fix 2 minor enhancements
|
3
|
+
* Don't serialize nil elements of primitive type (jimmyz)
|
4
|
+
* Added the xml version/encoding header to serialized xml string on to_xml (jimmyz)
|
5
|
+
* Bug fix: Don't parse deep on non-primitive elements unless :deep => true (jimmyz)
|
6
|
+
|
1
7
|
== 0.3.2
|
2
8
|
* 1 bug fix
|
3
9
|
* to_xml_node was blowing up if a non-primitive element was nil. Fixed (jimmyz)
|
data/happymapper.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{happymapper}
|
5
|
-
s.version = "0.3.
|
5
|
+
s.version = "0.3.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["John Nunemaker"]
|
9
|
-
s.date = %q{2009-02-
|
9
|
+
s.date = %q{2009-02-21}
|
10
10
|
s.description = %q{object to xml mapping library}
|
11
11
|
s.email = %q{nunemaker@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/happymapper.rb", "README", "TODO"]
|
data/lib/happymapper.rb
CHANGED
@@ -20,31 +20,34 @@ module HappyMapper
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def to_xml
|
23
|
-
|
24
|
-
|
23
|
+
doc = LibXML::XML::Document.new
|
24
|
+
doc.root = to_xml_node
|
25
|
+
doc.to_s
|
25
26
|
end
|
26
27
|
|
27
28
|
def to_xml_node(root_node = nil)
|
28
29
|
node = XML::Node.new(self.class.tag_name)
|
29
30
|
root_node ||= node
|
31
|
+
# add namespace to node and to root_element if not already set
|
30
32
|
if self.class.namespace_url
|
31
33
|
if root_node
|
32
34
|
namespace_object = root_node.namespaces.find_by_href(self.class.namespace_url)
|
33
35
|
namespace_object ||= XML::Namespace.new root_node, self.class.namespace, self.class.namespace_url
|
34
36
|
node.namespaces.namespace = namespace_object
|
35
37
|
end
|
36
|
-
else
|
37
|
-
nil
|
38
38
|
end
|
39
|
+
# serialize elements
|
39
40
|
self.class.elements.each do |e|
|
40
41
|
if e.options[:single] == false
|
41
42
|
self.send("#{e.method_name}").each do |array_element|
|
42
43
|
node << e.to_xml_node(array_element,root_node)
|
43
44
|
end
|
44
45
|
else
|
45
|
-
|
46
|
+
element_value = self.send("#{e.method_name}")
|
47
|
+
node << e.to_xml_node(element_value,root_node) unless element_value.nil?
|
46
48
|
end
|
47
49
|
end
|
50
|
+
# serialize attributes
|
48
51
|
self.class.attributes.each do |a|
|
49
52
|
attribute_value = self.send("#{a.method_name}")
|
50
53
|
node.attributes[a.tag] = attribute_value.to_s unless attribute_value.nil?
|
@@ -152,7 +155,7 @@ module HappyMapper
|
|
152
155
|
namespace ||= DEFAULT_NS
|
153
156
|
end
|
154
157
|
|
155
|
-
xpath = root ? '/' : './/'
|
158
|
+
xpath = root ? '/' : (options[:deep]) ? './/' : './'
|
156
159
|
xpath += "#{namespace}:" if namespace
|
157
160
|
xpath += tag_name
|
158
161
|
# puts "parse: #{xpath}"
|
data/lib/happymapper/version.rb
CHANGED
@@ -16,6 +16,8 @@
|
|
16
16
|
<fsapi-v1:gender>Male</fsapi-v1:gender>
|
17
17
|
<fsapi-v1:living>false</fsapi-v1:living>
|
18
18
|
</fsapi-v1:information>
|
19
|
+
<!-- element to make sure that we're not parsing deep when not supposed to -->
|
20
|
+
<person id="KWQS-BBR" />
|
19
21
|
</person>
|
20
22
|
</persons>
|
21
23
|
</familytree>
|
data/spec/happymapper_spec.rb
CHANGED
@@ -170,7 +170,8 @@ end
|
|
170
170
|
|
171
171
|
class Radar
|
172
172
|
include HappyMapper
|
173
|
-
|
173
|
+
# places needs to be deep because it lives out in places/place
|
174
|
+
has_many :places, Place, :deep => true
|
174
175
|
end
|
175
176
|
|
176
177
|
class Post
|
@@ -550,6 +551,12 @@ describe HappyMapper do
|
|
550
551
|
track.tran_detail.cust_tran_id.should == '20090102-111321'
|
551
552
|
end
|
552
553
|
|
554
|
+
it "should not parse deep for elements unless deep option is set" do
|
555
|
+
tree = FamilySearch::FamilyTree.parse(fixture_file('family_tree.xml'))
|
556
|
+
tree.persons.person.size.should_not == 2
|
557
|
+
tree.persons.person.size.should == 1
|
558
|
+
end
|
559
|
+
|
553
560
|
it "should parse family search xml" do
|
554
561
|
tree = FamilySearch::FamilyTree.parse(fixture_file('family_tree.xml'))
|
555
562
|
tree.version.should == '1.0.20071213.942'
|
@@ -595,6 +602,12 @@ describe HappyMapper do
|
|
595
602
|
doc.should match_xpath("/address/country","Germany")
|
596
603
|
end
|
597
604
|
|
605
|
+
it "should include an xml version/encoding header" do
|
606
|
+
address = Address.new
|
607
|
+
xml = address.to_xml
|
608
|
+
xml.should =~ /<?xml version=".*" encoding=".*"?>/
|
609
|
+
end
|
610
|
+
|
598
611
|
it "should return xml with non-primitive elements from ruby objects" do
|
599
612
|
posts = Posts.parse(fixture_file('posts.xml'))
|
600
613
|
xml = posts.to_xml
|
@@ -645,6 +658,13 @@ describe HappyMapper do
|
|
645
658
|
lambda{
|
646
659
|
ft.to_xml
|
647
660
|
}.should_not raise_error
|
661
|
+
|
662
|
+
# Check for nil elements of primitive type
|
663
|
+
info = FamilySearch::Information.new
|
664
|
+
namespace = {'fsapi-v1' => 'http://api.familysearch.org/v1'}
|
665
|
+
doc = REXML::Document.new info.to_xml
|
666
|
+
doc.should have_nodes("/fsapi-v1:information/fsapi-v1:gender",0,namespace)
|
667
|
+
doc.should have_nodes("/fsapi-v1:information/fsapi-v1:living",0,namespace)
|
648
668
|
end
|
649
669
|
|
650
670
|
describe "with namespace url and prefix" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jimmyz-happymapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-21 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|