xmlmapper 0.5.9 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -4
- data/README.md +35 -35
- data/lib/xmlmapper.rb +776 -1
- data/lib/{happymapper → xmlmapper}/anonymous_mapper.rb +23 -23
- data/lib/{happymapper → xmlmapper}/attribute.rb +1 -1
- data/lib/{happymapper → xmlmapper}/element.rb +1 -1
- data/lib/{happymapper → xmlmapper}/item.rb +1 -1
- data/lib/{happymapper → xmlmapper}/supported_types.rb +2 -2
- data/lib/{happymapper → xmlmapper}/text_node.rb +1 -1
- data/lib/xmlmapper/version.rb +3 -0
- data/spec/attribute_default_value_spec.rb +1 -1
- data/spec/attributes_spec.rb +2 -2
- data/spec/fixtures/unformatted_address.xml +1 -0
- data/spec/has_many_empty_array_spec.rb +2 -2
- data/spec/ignay_spec.rb +5 -5
- data/spec/inheritance_spec.rb +6 -6
- data/spec/mixed_namespaces_spec.rb +2 -2
- data/spec/parse_with_object_to_update_spec.rb +4 -4
- data/spec/spec_helper.rb +1 -1
- data/spec/to_xml_spec.rb +5 -5
- data/spec/to_xml_with_namespaces_spec.rb +6 -6
- data/spec/wilcard_tag_name_spec.rb +8 -8
- data/spec/wrap_spec.rb +5 -5
- data/spec/{happymapper → xmlmapper}/attribute_spec.rb +1 -1
- data/spec/{happymapper → xmlmapper}/element_spec.rb +2 -2
- data/spec/{happymapper → xmlmapper}/item_spec.rb +16 -16
- data/spec/xmlmapper/text_node_spec.rb +9 -0
- data/spec/{happymapper_parse_spec.rb → xmlmapper_parse_spec.rb} +3 -3
- data/spec/{happymapper_spec.rb → xmlmapper_spec.rb} +87 -66
- data/spec/xpath_spec.rb +5 -5
- metadata +22 -21
- data/lib/happymapper.rb +0 -776
- data/lib/happymapper/version.rb +0 -3
- data/spec/happymapper/text_node_spec.rb +0 -9
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module XmlMapper
|
2
2
|
module AnonymousMapper
|
3
3
|
|
4
4
|
def parse(xml_content)
|
@@ -7,13 +7,13 @@ module HappyMapper
|
|
7
7
|
# to handle which includes the text, xml document, node, fragment, etc.
|
8
8
|
xml = Nokogiri::XML(xml_content)
|
9
9
|
|
10
|
-
|
10
|
+
xmlmapper_class = create_xmlmapper_class_with_element(xml.root)
|
11
11
|
|
12
12
|
# With all the elements and attributes defined on the class it is time
|
13
|
-
# for the class to actually use the normal
|
13
|
+
# for the class to actually use the normal XmlMapper powers to parse
|
14
14
|
# the content. At this point this code is utilizing all of the existing
|
15
15
|
# code implemented for parsing.
|
16
|
-
|
16
|
+
xmlmapper_class.parse(xml_content, :single => true)
|
17
17
|
|
18
18
|
end
|
19
19
|
|
@@ -38,48 +38,48 @@ module HappyMapper
|
|
38
38
|
# going to match the content it will be able to parse so the tag
|
39
39
|
# value is set to the one provided.
|
40
40
|
#
|
41
|
-
def
|
42
|
-
|
43
|
-
|
44
|
-
include
|
41
|
+
def create_xmlmapper_class_with_tag(tag_name)
|
42
|
+
xmlmapper_class = Class.new
|
43
|
+
xmlmapper_class.class_eval do
|
44
|
+
include XmlMapper
|
45
45
|
tag tag_name
|
46
46
|
end
|
47
|
-
|
47
|
+
xmlmapper_class
|
48
48
|
end
|
49
49
|
|
50
50
|
#
|
51
|
-
# Used internally to create and define the necessary
|
51
|
+
# Used internally to create and define the necessary xmlmapper
|
52
52
|
# elements.
|
53
53
|
#
|
54
|
-
def
|
55
|
-
|
54
|
+
def create_xmlmapper_class_with_element(element)
|
55
|
+
xmlmapper_class = create_xmlmapper_class_with_tag(element.name)
|
56
56
|
|
57
|
-
|
57
|
+
xmlmapper_class.namespace element.namespace.prefix if element.namespace
|
58
58
|
|
59
59
|
element.namespaces.each do |prefix,namespace|
|
60
|
-
|
60
|
+
xmlmapper_class.register_namespace prefix, namespace
|
61
61
|
end
|
62
62
|
|
63
63
|
element.attributes.each do |name,attribute|
|
64
|
-
define_attribute_on_class(
|
64
|
+
define_attribute_on_class(xmlmapper_class,attribute)
|
65
65
|
end
|
66
66
|
|
67
67
|
element.children.each do |element|
|
68
|
-
define_element_on_class(
|
68
|
+
define_element_on_class(xmlmapper_class,element)
|
69
69
|
end
|
70
70
|
|
71
|
-
|
71
|
+
xmlmapper_class
|
72
72
|
end
|
73
73
|
|
74
74
|
|
75
75
|
#
|
76
|
-
# Define a
|
76
|
+
# Define a XmlMapper element on the provided class based on
|
77
77
|
# the element provided.
|
78
78
|
#
|
79
79
|
def define_element_on_class(class_instance,element)
|
80
80
|
|
81
81
|
# When a text element has been provided create the necessary
|
82
|
-
#
|
82
|
+
# XmlMapper content attribute if the text happens to content
|
83
83
|
# some content.
|
84
84
|
|
85
85
|
if element.text? and element.content.strip != ""
|
@@ -87,11 +87,11 @@ module HappyMapper
|
|
87
87
|
end
|
88
88
|
|
89
89
|
# When the element has children elements, that are not text
|
90
|
-
# elements, then we want to recursively define a new
|
90
|
+
# elements, then we want to recursively define a new XmlMapper
|
91
91
|
# class that will have elements and attributes.
|
92
92
|
|
93
93
|
element_type = if !element.elements.reject {|e| e.text? }.empty? or !element.attributes.empty?
|
94
|
-
|
94
|
+
create_xmlmapper_class_with_element(element)
|
95
95
|
else
|
96
96
|
String
|
97
97
|
end
|
@@ -102,7 +102,7 @@ module HappyMapper
|
|
102
102
|
end
|
103
103
|
|
104
104
|
#
|
105
|
-
# Define a
|
105
|
+
# Define a XmlMapper attribute on the provided class based on
|
106
106
|
# the attribute provided.
|
107
107
|
#
|
108
108
|
def define_attribute_on_class(class_instance,attribute)
|
@@ -111,4 +111,4 @@ module HappyMapper
|
|
111
111
|
|
112
112
|
end
|
113
113
|
|
114
|
-
end
|
114
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module XmlMapper
|
2
2
|
module SupportedTypes
|
3
3
|
extend self
|
4
4
|
|
@@ -41,7 +41,7 @@ module HappyMapper
|
|
41
41
|
#
|
42
42
|
# @example Registering a DateTime parser
|
43
43
|
#
|
44
|
-
#
|
44
|
+
# XmlMapper::SupportedTypes.register_type DateTime do |value|
|
45
45
|
# DateTime.parse(value,to_s)
|
46
46
|
# end
|
47
47
|
#
|
data/spec/attributes_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe "Attribute Method Conversion" do
|
|
10
10
|
|
11
11
|
module AttributeMethodConversion
|
12
12
|
class Document
|
13
|
-
include
|
13
|
+
include XmlMapper
|
14
14
|
|
15
15
|
has_many :link, String, :attributes => { :'data-src' => String, :type => String, :href => String }
|
16
16
|
|
@@ -33,4 +33,4 @@ describe "Attribute Method Conversion" do
|
|
33
33
|
expect(document.link.first.type).to eq "recipe"
|
34
34
|
end
|
35
35
|
|
36
|
-
end
|
36
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<address><street>Milchstrasse</street><housenumber>23</housenumber></address>
|
@@ -2,11 +2,11 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module Sheep
|
4
4
|
class Item
|
5
|
-
include
|
5
|
+
include XmlMapper
|
6
6
|
end
|
7
7
|
|
8
8
|
class Navigator
|
9
|
-
include
|
9
|
+
include XmlMapper
|
10
10
|
tag 'navigator'
|
11
11
|
|
12
12
|
# This is purposefully set to have the name 'items' with the tag 'item'.
|
data/spec/ignay_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
class CatalogTree
|
4
|
-
include
|
4
|
+
include XmlMapper
|
5
5
|
|
6
6
|
tag 'CatalogTree'
|
7
7
|
register_namespace 'xmlns', 'urn:eventis:prodis:onlineapi:1.0'
|
@@ -16,7 +16,7 @@ end
|
|
16
16
|
|
17
17
|
|
18
18
|
class CatalogNode
|
19
|
-
include
|
19
|
+
include XmlMapper
|
20
20
|
|
21
21
|
tag 'Node'
|
22
22
|
|
@@ -28,7 +28,7 @@ class CatalogNode
|
|
28
28
|
has_many :translations, 'CatalogNode::Translations', :tag => 'Translation', :xpath => 'child::*'
|
29
29
|
|
30
30
|
class Translations
|
31
|
-
include
|
31
|
+
include XmlMapper
|
32
32
|
tag 'Translation'
|
33
33
|
|
34
34
|
attribute :language, String, :tag => 'Language'
|
@@ -40,7 +40,7 @@ class CatalogNode
|
|
40
40
|
|
41
41
|
end
|
42
42
|
|
43
|
-
describe
|
43
|
+
describe XmlMapper do
|
44
44
|
|
45
45
|
it "should not be nil" do
|
46
46
|
catalog_tree.should_not be_nil
|
@@ -92,4 +92,4 @@ describe HappyMapper do
|
|
92
92
|
xml_reference = "#{File.dirname(__FILE__)}/fixtures/inagy.xml"
|
93
93
|
@catalog_tree = CatalogTree.parse(File.read(xml_reference), :single => true)
|
94
94
|
end
|
95
|
-
end
|
95
|
+
end
|
data/spec/inheritance_spec.rb
CHANGED
@@ -3,24 +3,24 @@ require 'spec_helper'
|
|
3
3
|
describe "Using inheritance to share elements and attributes" do
|
4
4
|
|
5
5
|
class Genetics
|
6
|
-
include
|
6
|
+
include XmlMapper
|
7
7
|
content :dna, String
|
8
8
|
end
|
9
9
|
|
10
10
|
class Parent
|
11
|
-
include
|
11
|
+
include XmlMapper
|
12
12
|
attribute :love, Integer
|
13
13
|
element :genetics, Genetics
|
14
14
|
end
|
15
15
|
|
16
16
|
class Child < Parent
|
17
|
-
include
|
17
|
+
include XmlMapper
|
18
18
|
attribute :naivety, String
|
19
19
|
has_many :immunities, String
|
20
20
|
end
|
21
21
|
|
22
22
|
class Overwrite < Parent
|
23
|
-
include
|
23
|
+
include XmlMapper
|
24
24
|
|
25
25
|
attribute :love, String
|
26
26
|
element :genetics, Integer
|
@@ -76,7 +76,7 @@ describe "Using inheritance to share elements and attributes" do
|
|
76
76
|
expect(subject.love).to eq 99
|
77
77
|
expect(subject.genetics.dna).to eq "ABBA"
|
78
78
|
expect(subject.naivety).to eq "trusting"
|
79
|
-
expect(subject.immunities).to
|
79
|
+
expect(subject.immunities.size).to eq(1)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -99,7 +99,7 @@ describe "Using inheritance to share elements and attributes" do
|
|
99
99
|
|
100
100
|
it "saves both the Child and Parent elements" do
|
101
101
|
expect(subject.xpath("genetics").text).to eq "GATTACA"
|
102
|
-
expect(subject.xpath("immunities")).to
|
102
|
+
expect(subject.xpath("immunities").size).to eq 3
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -21,7 +21,7 @@ describe "A document with mixed namespaces" do
|
|
21
21
|
|
22
22
|
module MixedNamespaces
|
23
23
|
class Address
|
24
|
-
include
|
24
|
+
include XmlMapper
|
25
25
|
|
26
26
|
namespace :prefix
|
27
27
|
tag :address
|
@@ -58,4 +58,4 @@ describe "A document with mixed namespaces" do
|
|
58
58
|
expect(address.city).to eq "Oldenburg"
|
59
59
|
end
|
60
60
|
|
61
|
-
end
|
61
|
+
end
|
@@ -52,20 +52,20 @@ describe "Updating existing objects with .parse and #parse" do
|
|
52
52
|
|
53
53
|
module ParseInstanceSpec
|
54
54
|
class SubItem
|
55
|
-
include
|
55
|
+
include XmlMapper
|
56
56
|
tag 'subitem'
|
57
57
|
attribute :attr1, String
|
58
58
|
element :name, String
|
59
59
|
end
|
60
60
|
class Item
|
61
|
-
include
|
61
|
+
include XmlMapper
|
62
62
|
tag 'item'
|
63
63
|
attribute :attr1, String
|
64
64
|
element :description, String
|
65
65
|
has_many :sub_items, SubItem
|
66
66
|
end
|
67
67
|
class Root
|
68
|
-
include
|
68
|
+
include XmlMapper
|
69
69
|
tag 'root'
|
70
70
|
attribute :attr1, String
|
71
71
|
has_many :items, Item
|
@@ -108,4 +108,4 @@ describe "Updating existing objects with .parse and #parse" do
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
end
|
111
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/to_xml_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe "Saving #to_xml" do
|
|
4
4
|
|
5
5
|
module ToXML
|
6
6
|
class Address
|
7
|
-
include
|
7
|
+
include XmlMapper
|
8
8
|
|
9
9
|
tag 'address'
|
10
10
|
|
@@ -72,7 +72,7 @@ describe "Saving #to_xml" do
|
|
72
72
|
# value.
|
73
73
|
#
|
74
74
|
class Country
|
75
|
-
include
|
75
|
+
include XmlMapper
|
76
76
|
|
77
77
|
attribute :code, String, :tag => 'countryCode'
|
78
78
|
has_one :name, String, :tag => 'countryName'
|
@@ -83,7 +83,7 @@ describe "Saving #to_xml" do
|
|
83
83
|
# and optional attributes
|
84
84
|
#
|
85
85
|
class Description
|
86
|
-
include
|
86
|
+
include XmlMapper
|
87
87
|
content :description, String
|
88
88
|
attribute :category, String, :tag => 'category'
|
89
89
|
attribute :rating, String, :tag => 'rating', :state_when_nil => true
|
@@ -184,7 +184,7 @@ describe "Saving #to_xml" do
|
|
184
184
|
end
|
185
185
|
end
|
186
186
|
|
187
|
-
context "when an element type is a
|
187
|
+
context "when an element type is a XmlMapper subclass" do
|
188
188
|
it "saves attributes" do
|
189
189
|
expect(subject.xpath('country/@countryCode').text).to eq "us"
|
190
190
|
end
|
@@ -197,4 +197,4 @@ describe "Saving #to_xml" do
|
|
197
197
|
expect(subject.xpath('country/description').text).to eq "A lovely country"
|
198
198
|
end
|
199
199
|
end
|
200
|
-
end
|
200
|
+
end
|
@@ -6,7 +6,7 @@ module ToXMLWithNamespaces
|
|
6
6
|
# Similar example as the to_xml but this time with namespacing
|
7
7
|
#
|
8
8
|
class Address
|
9
|
-
include
|
9
|
+
include XmlMapper
|
10
10
|
|
11
11
|
register_namespace 'address', 'http://www.company.com/address'
|
12
12
|
register_namespace 'country', 'http://www.company.com/country'
|
@@ -70,7 +70,7 @@ module ToXMLWithNamespaces
|
|
70
70
|
# value.
|
71
71
|
#
|
72
72
|
class Country
|
73
|
-
include
|
73
|
+
include XmlMapper
|
74
74
|
|
75
75
|
register_namespace 'countryName', 'http://www.company.com/countryName'
|
76
76
|
|
@@ -91,7 +91,7 @@ module ToXMLWithNamespaces
|
|
91
91
|
#xmlns="urn:eventis:prodis:onlineapi:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
92
92
|
#
|
93
93
|
class Recipe
|
94
|
-
include
|
94
|
+
include XmlMapper
|
95
95
|
|
96
96
|
# this is the default namespace of the document
|
97
97
|
register_namespace 'xmlns', 'urn:eventis:prodis:onlineapi:1.0'
|
@@ -176,7 +176,7 @@ describe "Saving #to_xml", "with xml namespaces" do
|
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
179
|
-
context "when an element type is a
|
179
|
+
context "when an element type is a XmlMapper subclass" do
|
180
180
|
it "saves attributes" do
|
181
181
|
expect(subject.xpath('country:country/@countryCode').text).to eq "us"
|
182
182
|
end
|
@@ -207,14 +207,14 @@ describe "Saving #to_xml", "with xml namespaces" do
|
|
207
207
|
end
|
208
208
|
|
209
209
|
class Beverage
|
210
|
-
include
|
210
|
+
include XmlMapper
|
211
211
|
namespace 'coffee'
|
212
212
|
|
213
213
|
attribute :name, String
|
214
214
|
end
|
215
215
|
|
216
216
|
class CoffeeMachine
|
217
|
-
include
|
217
|
+
include XmlMapper
|
218
218
|
register_namespace 'coffee', "http://coffee.org/Coffee/0.1"
|
219
219
|
register_namespace 'beverage', "http://beverages.org/Beverage/0.1"
|
220
220
|
|
@@ -16,7 +16,7 @@ describe "Wildcard Root Tag" do
|
|
16
16
|
module GenericBase
|
17
17
|
class Base
|
18
18
|
include Comparable
|
19
|
-
include
|
19
|
+
include XmlMapper
|
20
20
|
|
21
21
|
def initialize(params = {})
|
22
22
|
@name = params[:name]
|
@@ -34,12 +34,12 @@ describe "Wildcard Root Tag" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
class Sub
|
37
|
-
include
|
37
|
+
include XmlMapper
|
38
38
|
tag 'subelement'
|
39
39
|
has_one :jello, Base, :tag => 'jello'
|
40
40
|
end
|
41
41
|
class Root
|
42
|
-
include
|
42
|
+
include XmlMapper
|
43
43
|
tag 'root'
|
44
44
|
element :description, String
|
45
45
|
has_many :blargs, Base, :tag => 'blarg', :xpath => '.'
|
@@ -60,9 +60,9 @@ describe "Wildcard Root Tag" do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it 'should filter on xpath appropriately' do
|
63
|
-
subject.blargs.
|
64
|
-
subject.jellos.
|
65
|
-
subject.subjellos.
|
63
|
+
expect(subject.blargs.size).to eq 2
|
64
|
+
expect(subject.jellos.size).to eq 1
|
65
|
+
expect(subject.subjellos.size).to eq 1
|
66
66
|
end
|
67
67
|
|
68
68
|
def base_with(name,href,other)
|
@@ -89,8 +89,8 @@ describe "Wildcard Root Tag" do
|
|
89
89
|
validate_xpath("/root/jello[1]","jelloname","http://jello.com","")
|
90
90
|
end
|
91
91
|
|
92
|
-
it "should properly respect child
|
93
|
-
xml.xpath('root/subelement').
|
92
|
+
it "should properly respect child XmlMapper tags if tag isn't provided on the element defintion" do
|
93
|
+
expect(xml.xpath('root/subelement').size).to eq 1
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|