xmlmapper 0.5.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -4
  3. data/README.md +35 -35
  4. data/lib/xmlmapper.rb +776 -1
  5. data/lib/{happymapper → xmlmapper}/anonymous_mapper.rb +23 -23
  6. data/lib/{happymapper → xmlmapper}/attribute.rb +1 -1
  7. data/lib/{happymapper → xmlmapper}/element.rb +1 -1
  8. data/lib/{happymapper → xmlmapper}/item.rb +1 -1
  9. data/lib/{happymapper → xmlmapper}/supported_types.rb +2 -2
  10. data/lib/{happymapper → xmlmapper}/text_node.rb +1 -1
  11. data/lib/xmlmapper/version.rb +3 -0
  12. data/spec/attribute_default_value_spec.rb +1 -1
  13. data/spec/attributes_spec.rb +2 -2
  14. data/spec/fixtures/unformatted_address.xml +1 -0
  15. data/spec/has_many_empty_array_spec.rb +2 -2
  16. data/spec/ignay_spec.rb +5 -5
  17. data/spec/inheritance_spec.rb +6 -6
  18. data/spec/mixed_namespaces_spec.rb +2 -2
  19. data/spec/parse_with_object_to_update_spec.rb +4 -4
  20. data/spec/spec_helper.rb +1 -1
  21. data/spec/to_xml_spec.rb +5 -5
  22. data/spec/to_xml_with_namespaces_spec.rb +6 -6
  23. data/spec/wilcard_tag_name_spec.rb +8 -8
  24. data/spec/wrap_spec.rb +5 -5
  25. data/spec/{happymapper → xmlmapper}/attribute_spec.rb +1 -1
  26. data/spec/{happymapper → xmlmapper}/element_spec.rb +2 -2
  27. data/spec/{happymapper → xmlmapper}/item_spec.rb +16 -16
  28. data/spec/xmlmapper/text_node_spec.rb +9 -0
  29. data/spec/{happymapper_parse_spec.rb → xmlmapper_parse_spec.rb} +3 -3
  30. data/spec/{happymapper_spec.rb → xmlmapper_spec.rb} +87 -66
  31. data/spec/xpath_spec.rb +5 -5
  32. metadata +22 -21
  33. data/lib/happymapper.rb +0 -776
  34. data/lib/happymapper/version.rb +0 -3
  35. data/spec/happymapper/text_node_spec.rb +0 -9
@@ -1,4 +1,4 @@
1
- module HappyMapper
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
- happymapper_class = create_happymapper_class_with_element(xml.root)
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 HappyMapper powers to parse
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
- happymapper_class.parse(xml_content, :single => true)
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 create_happymapper_class_with_tag(tag_name)
42
- happymapper_class = Class.new
43
- happymapper_class.class_eval do
44
- include HappyMapper
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
- happymapper_class
47
+ xmlmapper_class
48
48
  end
49
49
 
50
50
  #
51
- # Used internally to create and define the necessary happymapper
51
+ # Used internally to create and define the necessary xmlmapper
52
52
  # elements.
53
53
  #
54
- def create_happymapper_class_with_element(element)
55
- happymapper_class = create_happymapper_class_with_tag(element.name)
54
+ def create_xmlmapper_class_with_element(element)
55
+ xmlmapper_class = create_xmlmapper_class_with_tag(element.name)
56
56
 
57
- happymapper_class.namespace element.namespace.prefix if element.namespace
57
+ xmlmapper_class.namespace element.namespace.prefix if element.namespace
58
58
 
59
59
  element.namespaces.each do |prefix,namespace|
60
- happymapper_class.register_namespace prefix, namespace
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(happymapper_class,attribute)
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(happymapper_class,element)
68
+ define_element_on_class(xmlmapper_class,element)
69
69
  end
70
70
 
71
- happymapper_class
71
+ xmlmapper_class
72
72
  end
73
73
 
74
74
 
75
75
  #
76
- # Define a HappyMapper element on the provided class based on
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
- # HappyMapper content attribute if the text happens to content
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 HappyMapper
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
- create_happymapper_class_with_element(element)
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 HappyMapper attribute on the provided class based on
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 HappyMapper
1
+ module XmlMapper
2
2
  class Attribute < Item
3
3
  attr_accessor :default
4
4
 
@@ -1,4 +1,4 @@
1
- module HappyMapper
1
+ module XmlMapper
2
2
  class Element < Item
3
3
 
4
4
  def find(node, namespace, xpath_options)
@@ -1,4 +1,4 @@
1
- module HappyMapper
1
+ module XmlMapper
2
2
  class Item
3
3
  attr_accessor :name, :type, :tag, :options, :namespace
4
4
 
@@ -1,4 +1,4 @@
1
- module HappyMapper
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
- # HappyMapper::SupportedTypes.register_type DateTime do |value|
44
+ # XmlMapper::SupportedTypes.register_type DateTime do |value|
45
45
  # DateTime.parse(value,to_s)
46
46
  # end
47
47
  #
@@ -1,4 +1,4 @@
1
- module HappyMapper
1
+ module XmlMapper
2
2
  class TextNode < Item
3
3
 
4
4
  def find(node, namespace, xpath_options)
@@ -0,0 +1,3 @@
1
+ module XmlMapper
2
+ VERSION = "0.6.0"
3
+ end
@@ -5,7 +5,7 @@ describe "Attribute Default Value" do
5
5
  context "when given a default value" do
6
6
 
7
7
  class Meal
8
- include HappyMapper
8
+ include XmlMapper
9
9
  tag 'meal'
10
10
  attribute :type, String, :default => 'omnivore'
11
11
  end
@@ -10,7 +10,7 @@ describe "Attribute Method Conversion" do
10
10
 
11
11
  module AttributeMethodConversion
12
12
  class Document
13
- include HappyMapper
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 HappyMapper
5
+ include XmlMapper
6
6
  end
7
7
 
8
8
  class Navigator
9
- include HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper do
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
@@ -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 HappyMapper
6
+ include XmlMapper
7
7
  content :dna, String
8
8
  end
9
9
 
10
10
  class Parent
11
- include HappyMapper
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 HappyMapper
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 HappyMapper
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 have(1).item
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 have(3).items
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 HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper
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
@@ -1,6 +1,6 @@
1
1
  require 'rspec'
2
2
 
3
- require 'happymapper'
3
+ require 'xmlmapper'
4
4
 
5
5
  def fixture_file(filename)
6
6
  File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
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 HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper subclass" do
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 HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper subclass" do
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 HappyMapper
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 HappyMapper
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 HappyMapper
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 HappyMapper
37
+ include XmlMapper
38
38
  tag 'subelement'
39
39
  has_one :jello, Base, :tag => 'jello'
40
40
  end
41
41
  class Root
42
- include HappyMapper
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.should have(2).items
64
- subject.jellos.should have(1).items
65
- subject.subjellos.should have(1).items
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 HappyMapper tags if tag isn't provided on the element defintion" do
93
- xml.xpath('root/subelement').should have(1).item
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