nokogiri-happymapper 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ track_files 'lib/**/*.rb'
6
+ add_filter '/spec/'
7
+ add_filter 'lib/happymapper/version.rb'
8
+ end
9
+
10
+ if ENV['CI']
11
+ begin
12
+ require 'coveralls'
13
+ Coveralls.wear!
14
+ rescue LoadError
15
+ nil
16
+ end
17
+ end
18
+
1
19
  require 'rspec'
2
20
 
3
21
  require 'happymapper'
@@ -1,14 +1,15 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe "Saving #to_xml" do
3
+ require 'spec_helper'
4
4
 
5
+ describe 'Saving #to_xml' do
5
6
  module ToXML
6
7
  class Address
7
8
  include HappyMapper
8
9
 
9
10
  tag 'address'
10
11
 
11
- attribute :location, String, :on_save => :when_saving_location
12
+ attribute :location, String, on_save: :when_saving_location
12
13
 
13
14
  element :street, String
14
15
  element :postcode, String
@@ -16,8 +17,8 @@ describe "Saving #to_xml" do
16
17
 
17
18
  element :housenumber, String
18
19
 
19
- attribute :modified, Boolean, :read_only => true
20
- element :temporary, Boolean, :read_only => true
20
+ attribute :modified, Boolean, read_only: true
21
+ element :temporary, Boolean, read_only: true
21
22
  #
22
23
  # to_xml will default to the attr_accessor method and not the attribute,
23
24
  # allowing for that to be overwritten
@@ -28,19 +29,18 @@ describe "Saving #to_xml" do
28
29
  end
29
30
 
30
31
  def when_saving_location(loc)
31
- loc + "-live"
32
+ loc + '-live'
32
33
  end
33
34
 
34
35
  #
35
36
  # Write a empty element even if this is not specified
36
37
  #
37
- element :description, String, :state_when_nil => true
38
+ element :description, String, state_when_nil: true
38
39
 
39
40
  #
40
41
  # Perform the on_save operation when saving
41
42
  #
42
- has_one :date_created, Time, :on_save => lambda {|time| DateTime.parse(time).strftime("%T %D") if time }
43
-
43
+ has_one :date_created, Time, on_save: ->(time) { Time.parse(time).strftime('%T %D') if time }
44
44
 
45
45
  #
46
46
  # Execute the method with the same name
@@ -48,23 +48,23 @@ describe "Saving #to_xml" do
48
48
  #
49
49
  # Write multiple elements and call on_save when saving
50
50
  #
51
- has_many :dates_updated, Time, :on_save => lambda {|times|
52
- times.compact.map {|time| DateTime.parse(time).strftime("%T %D") } if times }
51
+ has_many :dates_updated, Time, on_save: lambda { |times|
52
+ times.compact.map { |time| Time.parse(time).strftime('%T %D') } if times
53
+ }
53
54
 
54
55
  #
55
56
  # Class composition
56
57
  #
57
- element :country, 'Country', :tag => 'country'
58
+ element :country, 'Country', tag: 'country'
58
59
 
59
60
  attribute :occupied, Boolean
60
61
 
61
62
  def initialize(parameters)
62
- parameters.each_pair do |property,value|
63
- send("#{property}=",value) if respond_to?("#{property}=")
63
+ parameters.each_pair do |property, value|
64
+ send("#{property}=", value) if respond_to?("#{property}=")
64
65
  end
65
66
  @modified = @temporary = true
66
67
  end
67
-
68
68
  end
69
69
 
70
70
  #
@@ -75,9 +75,9 @@ describe "Saving #to_xml" do
75
75
  class Country
76
76
  include HappyMapper
77
77
 
78
- attribute :code, String, :tag => 'countryCode'
79
- has_one :name, String, :tag => 'countryName'
80
- has_one :description, 'Description', :tag => 'description'
78
+ attribute :code, String, tag: 'countryCode'
79
+ has_one :name, String, tag: 'countryName'
80
+ has_one :description, 'Description', tag: 'description'
81
81
 
82
82
  #
83
83
  # This inner-class here is to demonstrate saving a text node
@@ -86,8 +86,8 @@ describe "Saving #to_xml" do
86
86
  class Description
87
87
  include HappyMapper
88
88
  content :description, String
89
- attribute :category, String, :tag => 'category'
90
- attribute :rating, String, :tag => 'rating', :state_when_nil => true
89
+ attribute :category, String, tag: 'category'
90
+ attribute :rating, String, tag: 'rating', state_when_nil: true
91
91
 
92
92
  def initialize(desc)
93
93
  @description = desc
@@ -95,107 +95,108 @@ describe "Saving #to_xml" do
95
95
  end
96
96
 
97
97
  def initialize(parameters)
98
- parameters.each_pair do |property,value|
99
- send("#{property}=",value) if respond_to?("#{property}=")
98
+ parameters.each_pair do |property, value|
99
+ send("#{property}=", value) if respond_to?("#{property}=")
100
100
  end
101
101
  end
102
-
103
102
  end
104
103
  end
105
104
 
106
105
  let(:subject) do
106
+ country = ToXML::Country.new(name: 'USA', code: 'us', empty_code: nil,
107
+ description: ToXML::Country::Description.new('A lovely country'))
108
+
107
109
  address = ToXML::Address.new 'street' => 'Mockingbird Lane',
108
- 'location' => 'Home',
109
- 'housenumber' => '1313',
110
- 'postcode' => '98103',
111
- 'city' => 'Seattle',
112
- 'country' => ToXML::Country.new(:name => 'USA', :code => 'us', :empty_code => nil,
113
- :description => ToXML::Country::Description.new("A lovely country") ),
114
- 'date_created' => '2011-01-01 15:00:00',
115
- 'occupied' => false
110
+ 'location' => 'Home',
111
+ 'housenumber' => '1313',
112
+ 'postcode' => '98103',
113
+ 'city' => 'Seattle',
114
+ 'country' => country,
115
+ 'date_created' => '2011-01-01 15:00:00',
116
+ 'occupied' => false
116
117
 
117
- address.dates_updated = ["2011-01-01 16:01:00","2011-01-02 11:30:01"]
118
+ address.dates_updated = ['2011-01-01 16:01:00', '2011-01-02 11:30:01']
118
119
 
119
120
  Nokogiri::XML(address.to_xml).root
120
121
  end
121
122
 
122
- it "saves elements" do
123
+ it 'saves elements' do
123
124
  elements = { 'street' => 'Mockingbird Lane', 'postcode' => '98103', 'city' => 'Seattle' }
124
- elements.each_pair do |property,value|
125
- expect(subject.xpath("#{property}").text).to eq value
125
+ elements.each_pair do |property, value|
126
+ expect(subject.xpath(property.to_s).text).to eq value
126
127
  end
127
128
  end
128
129
 
129
- it "saves attributes" do
130
- expect(subject.xpath('@location').text).to eq "Home-live"
130
+ it 'saves attributes' do
131
+ expect(subject.xpath('@location').text).to eq 'Home-live'
131
132
  end
132
133
 
133
134
  it 'saves attributes that are Boolean and have a value of false' do
134
- expect(subject.xpath('@occupied').text).to eq "false"
135
+ expect(subject.xpath('@occupied').text).to eq 'false'
135
136
  end
136
137
 
137
138
  context "when an element has a 'read_only' parameter" do
138
- it "does not save elements" do
139
+ it 'does not save elements' do
139
140
  expect(subject.xpath('temporary')).to be_empty
140
141
  end
141
142
  end
142
143
 
143
144
  context "when an attribute has a 'read_only' parameter" do
144
- it "does not save attributes" do
145
- expect(subject.xpath("@modified")).to be_empty
145
+ it 'does not save attributes' do
146
+ expect(subject.xpath('@modified')).to be_empty
146
147
  end
147
148
  end
148
149
 
149
150
  context "when an element has a 'state_when_nil' parameter" do
150
- it "saves an empty element" do
151
- expect(subject.xpath('description').text).to eq ""
151
+ it 'saves an empty element' do
152
+ expect(subject.xpath('description').text).to eq ''
152
153
  end
153
154
  end
154
155
 
155
156
  context "when an element has a 'on_save' parameter" do
156
- context "with a symbol which represents a function" do
157
- it "saves the element with the result of the function" do
158
- expect(subject.xpath("housenumber").text).to eq "[1313]"
157
+ context 'with a symbol which represents a function' do
158
+ it 'saves the element with the result of the function' do
159
+ expect(subject.xpath('housenumber').text).to eq '[1313]'
159
160
  end
160
161
  end
161
162
 
162
- context "with a lambda" do
163
- it "saves the result of the lambda" do
164
- expect(subject.xpath('date_created').text).to eq "15:00:00 01/01/11"
163
+ context 'with a lambda' do
164
+ it 'saves the result of the lambda' do
165
+ expect(subject.xpath('date_created').text).to eq '15:00:00 01/01/11'
165
166
  end
166
167
  end
167
168
  end
168
169
 
169
170
  context "when a has_many has a 'on_save' parameter" do
170
- context "with a lambda" do
171
- it "saves the results" do
171
+ context 'with a lambda' do
172
+ it 'saves the results' do
172
173
  dates_updated = subject.xpath('dates_updated')
173
174
  expect(dates_updated.length).to eq 2
174
- expect(dates_updated.first.text).to eq "16:01:00 01/01/11"
175
- expect(dates_updated.last.text).to eq "11:30:01 01/02/11"
175
+ expect(dates_updated.first.text).to eq '16:01:00 01/01/11'
176
+ expect(dates_updated.last.text).to eq '11:30:01 01/02/11'
176
177
  end
177
178
  end
178
179
  end
179
180
 
180
181
  context "when an attribute has a 'on_save' parameter" do
181
- context "with a symbol which represents a function" do
182
- it "saves the result" do
183
- expect(subject.xpath('@location').text).to eq "Home-live"
182
+ context 'with a symbol which represents a function' do
183
+ it 'saves the result' do
184
+ expect(subject.xpath('@location').text).to eq 'Home-live'
184
185
  end
185
186
  end
186
187
  end
187
188
 
188
- context "when an element type is a HappyMapper subclass" do
189
- it "saves attributes" do
190
- expect(subject.xpath('country/@countryCode').text).to eq "us"
189
+ context 'when an element type is a HappyMapper subclass' do
190
+ it 'saves attributes' do
191
+ expect(subject.xpath('country/@countryCode').text).to eq 'us'
191
192
  end
192
193
 
193
- it "saves elements" do
194
- expect(subject.xpath('country/countryName').text).to eq "USA"
194
+ it 'saves elements' do
195
+ expect(subject.xpath('country/countryName').text).to eq 'USA'
195
196
  end
196
197
 
197
- it "saves elements" do
198
- expect(subject.xpath('country/description').text).to eq "A lovely country"
198
+ it 'saves elements' do
199
+ expect(subject.xpath('country/description').text).to eq 'A lovely country'
199
200
  end
200
201
  end
201
202
  end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module ToXMLWithNamespaces
4
-
5
6
  #
6
7
  # Similar example as the to_xml but this time with namespacing
7
8
  #
@@ -14,9 +15,9 @@ module ToXMLWithNamespaces
14
15
  tag 'Address'
15
16
  namespace 'address'
16
17
 
17
- element :country, 'Country', :tag => 'country', :namespace => 'country'
18
+ element :country, 'Country', tag: 'country', namespace: 'country'
18
19
 
19
- attribute :location, String, :on_save => :when_saving_location
20
+ attribute :location, String, on_save: :when_saving_location
20
21
 
21
22
  element :street, String
22
23
  element :postcode, String
@@ -40,29 +41,29 @@ module ToXMLWithNamespaces
40
41
  #
41
42
  # Write a empty element even if this is not specified
42
43
  #
43
- element :description, String, :state_when_nil => true
44
+ element :description, String, state_when_nil: true
44
45
 
45
46
  #
46
47
  # Perform the on_save operation when saving
47
48
  #
48
- has_one :date_created, Time, :on_save => lambda {|time| DateTime.parse(time).strftime("%T %D") if time }
49
+ has_one :date_created, Time, on_save: ->(time) { Time.parse(time).strftime('%T %D') if time }
49
50
 
50
51
  #
51
52
  # Write multiple elements and call on_save when saving
52
53
  #
53
- has_many :dates_updated, Time, :on_save => lambda {|times|
54
- times.compact.map {|time| DateTime.parse(time).strftime("%T %D") } if times }
54
+ has_many :dates_updated, Time, on_save: lambda { |times|
55
+ times.compact.map { |time| Time.parse(time).strftime('%T %D') } if times
56
+ }
55
57
 
56
58
  #
57
59
  # Class composition
58
60
  #
59
61
 
60
62
  def initialize(parameters)
61
- parameters.each_pair do |property,value|
62
- send("#{property}=",value) if respond_to?("#{property}=")
63
+ parameters.each_pair do |property, value|
64
+ send("#{property}=", value) if respond_to?("#{property}=")
63
65
  end
64
66
  end
65
-
66
67
  end
67
68
 
68
69
  #
@@ -75,122 +76,124 @@ module ToXMLWithNamespaces
75
76
 
76
77
  register_namespace 'countryName', 'http://www.company.com/countryName'
77
78
 
78
- attribute :code, String, :tag => 'countryCode'
79
- has_one :name, String, :tag => 'countryName', :namespace => 'countryName'
79
+ attribute :code, String, tag: 'countryCode'
80
+ has_one :name, String, tag: 'countryName', namespace: 'countryName'
80
81
 
81
82
  def initialize(parameters)
82
- parameters.each_pair do |property,value|
83
- send("#{property}=",value) if respond_to?("#{property}=")
83
+ parameters.each_pair do |property, value|
84
+ send("#{property}=", value) if respond_to?("#{property}=")
84
85
  end
85
86
  end
86
-
87
87
  end
88
88
 
89
-
90
89
  #
91
90
  # This class is an example of a class that has a default namespace
92
- #xmlns="urn:eventis:prodis:onlineapi:1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
91
+ # xmlns="urn:eventis:prodis:onlineapi:1.0"
92
+ # xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93
+ # xmlns:xsd="http://www.w3.org/2001/XMLSchema"
93
94
  #
94
95
  class Recipe
95
96
  include HappyMapper
96
97
 
97
98
  # this is the default namespace of the document
98
99
  register_namespace 'xmlns', 'urn:eventis:prodis:onlineapi:1.0'
99
- register_namespace 'xsi', "http://www.w3.org/2001/XMLSchema-instance"
100
- register_namespace 'xsd', "http://www.w3.org/2001/XMLSchema"
100
+ register_namespace 'xsi', 'http://www.w3.org/2001/XMLSchema-instance'
101
+ register_namespace 'xsd', 'http://www.w3.org/2001/XMLSchema'
101
102
 
102
103
  has_many :ingredients, String
103
104
 
104
105
  def initialize(parameters)
105
- parameters.each_pair {|property,value| send("#{property}=",value) if respond_to?("#{property}=") }
106
+ parameters.each_pair { |property, value| send("#{property}=", value) if respond_to?("#{property}=") }
106
107
  end
107
108
  end
108
109
  end
109
110
 
110
- describe "Saving #to_xml", "with xml namespaces" do
111
-
112
- context "#to_xml", "with namespaces" do
113
-
111
+ describe 'Saving #to_xml', 'with xml namespaces' do
112
+ context '#to_xml', 'with namespaces' do
114
113
  let(:subject) do
114
+ country = ToXMLWithNamespaces::Country.new(name: 'USA', code: 'us')
115
115
  address = ToXMLWithNamespaces::Address.new('street' => 'Mockingbird Lane',
116
- 'location' => 'Home',
117
- 'housenumber' => '1313',
118
- 'postcode' => '98103',
119
- 'city' => 'Seattle',
120
- 'country' => ToXMLWithNamespaces::Country.new(:name => 'USA', :code => 'us'),
121
- 'date_created' => '2011-01-01 15:00:00')
122
-
116
+ 'location' => 'Home',
117
+ 'housenumber' => '1313',
118
+ 'postcode' => '98103',
119
+ 'city' => 'Seattle',
120
+ 'country' => country,
121
+ 'date_created' => '2011-01-01 15:00:00')
123
122
 
124
- address.dates_updated = ["2011-01-01 16:01:00","2011-01-02 11:30:01"]
123
+ address.dates_updated = ['2011-01-01 16:01:00', '2011-01-02 11:30:01']
125
124
 
126
125
  Nokogiri::XML(address.to_xml).root
127
126
  end
128
127
 
129
- it "saves elements" do
128
+ it 'sets the namespace specified in the class' do
129
+ expect(subject.xpath('/').children.first.namespace.prefix).to eq 'address'
130
+ end
131
+
132
+ it 'saves elements' do
130
133
  elements = { 'street' => 'Mockingbird Lane', 'postcode' => '98103', 'city' => 'Seattle' }
131
134
 
132
- elements.each_pair do |property,value|
135
+ elements.each_pair do |property, value|
133
136
  expect(subject.xpath("address:#{property}").text).to eq value
134
137
  end
135
138
  end
136
139
 
137
- it "saves attributes" do
138
- expect(subject.xpath('@location').text).to eq "Home-live"
140
+ it 'saves attributes' do
141
+ expect(subject.xpath('@location').text).to eq 'Home-live'
139
142
  end
140
143
 
141
144
  context "when an element has a 'state_when_nil' parameter" do
142
- it "saves an empty element" do
143
- expect(subject.xpath('address:description').text).to eq ""
145
+ it 'saves an empty element' do
146
+ expect(subject.xpath('address:description').text).to eq ''
144
147
  end
145
148
  end
146
149
 
147
150
  context "when an element has a 'on_save' parameter" do
148
- context "with a symbol which represents a function" do
149
- it "saves the element with the result of a function call and not the value of the instance variable" do
150
- expect(subject.xpath("address:housenumber").text).to eq "[1313]"
151
+ context 'with a symbol which represents a function' do
152
+ it 'saves the element with the result of a function call and not the value of the ivar' do
153
+ expect(subject.xpath('address:housenumber').text).to eq '[1313]'
151
154
  end
152
155
  end
153
156
 
154
- context "with a lambda" do
155
- it "saves the results" do
156
- expect(subject.xpath('address:date_created').text).to eq "15:00:00 01/01/11"
157
+ context 'with a lambda' do
158
+ it 'saves the results' do
159
+ expect(subject.xpath('address:date_created').text).to eq '15:00:00 01/01/11'
157
160
  end
158
161
  end
159
162
  end
160
163
 
161
164
  context "when an attribute has a 'on_save' parameter" do
162
- context "with a lambda" do
163
- it "saves the result" do
164
- expect(subject.xpath('@location').text).to eq "Home-live"
165
+ context 'with a lambda' do
166
+ it 'saves the result' do
167
+ expect(subject.xpath('@location').text).to eq 'Home-live'
165
168
  end
166
169
  end
167
170
  end
168
171
 
169
172
  context "when a has_many has a 'on_save' parameter" do
170
- context "with a lambda" do
171
- it "saves the result" do
173
+ context 'with a lambda' do
174
+ it 'saves the result' do
172
175
  dates_updated = subject.xpath('address:dates_updated')
173
176
  expect(dates_updated.length).to eq 2
174
- expect(dates_updated.first.text).to eq "16:01:00 01/01/11"
175
- expect(dates_updated.last.text).to eq "11:30:01 01/02/11"
177
+ expect(dates_updated.first.text).to eq '16:01:00 01/01/11'
178
+ expect(dates_updated.last.text).to eq '11:30:01 01/02/11'
176
179
  end
177
180
  end
178
181
  end
179
182
 
180
- context "when an element type is a HappyMapper subclass" do
181
- it "saves attributes" do
182
- expect(subject.xpath('country:country/@countryCode').text).to eq "us"
183
+ context 'when an element type is a HappyMapper subclass' do
184
+ it 'saves attributes' do
185
+ expect(subject.xpath('country:country/@countryCode').text).to eq 'us'
183
186
  end
184
187
 
185
- it "saves elements" do
186
- expect(subject.xpath('country:country/countryName:countryName').text).to eq "USA"
188
+ it 'saves elements' do
189
+ expect(subject.xpath('country:country/countryName:countryName').text).to eq 'USA'
187
190
  end
188
191
  end
189
192
  end
190
193
 
191
- context "with a default namespace" do
192
- it "writes the default namespace to xml without repeating xmlns" do
193
- recipe = ToXMLWithNamespaces::Recipe.new(:ingredients => ['One Cup Flour', 'Two Scoops of Lovin'])
194
+ context 'with a default namespace' do
195
+ it 'writes the default namespace to xml without repeating xmlns' do
196
+ recipe = ToXMLWithNamespaces::Recipe.new(ingredients: ['One Cup Flour', 'Two Scoops of Lovin'])
194
197
  expect(recipe.to_xml).to match(/xmlns=\"urn:eventis:prodis:onlineapi:1\.0\"/)
195
198
  end
196
199
  end
@@ -201,7 +204,7 @@ describe "Saving #to_xml", "with xml namespaces" do
201
204
  let(:expected_xml) do
202
205
  <<-XML.gsub(/^\s*\|/, '')
203
206
  |<?xml version="1.0"?>
204
- |<coffeemachine xmlns:coffee="http://coffee.org/Coffee/0.1" xmlns:beverage="http://beverages.org/Beverage/0.1">
207
+ |<coffeemachine xmlns:beverage="http://beverages.org/Beverage/0.1" xmlns:coffee="http://coffee.org/Coffee/0.1">
205
208
  | <beverage:beverage name="coffee"/>
206
209
  |</coffeemachine>
207
210
  XML
@@ -216,17 +219,16 @@ describe "Saving #to_xml", "with xml namespaces" do
216
219
 
217
220
  class CoffeeMachine
218
221
  include HappyMapper
219
- register_namespace 'coffee', "http://coffee.org/Coffee/0.1"
220
- register_namespace 'beverage', "http://beverages.org/Beverage/0.1"
222
+ register_namespace 'coffee', 'http://coffee.org/Coffee/0.1'
223
+ register_namespace 'beverage', 'http://beverages.org/Beverage/0.1'
221
224
 
222
225
  element :beverage, 'beverage', namespace: 'beverage'
223
226
  end
224
227
 
225
228
  it 'uses the element declaration namespace on the element' do
226
229
  machine = CoffeeMachine.new
227
- machine.beverage = Beverage.new.tap {|obj| obj.name = 'coffee'}
230
+ machine.beverage = Beverage.new.tap { |obj| obj.name = 'coffee' }
228
231
  expect(machine.to_xml).to eq(expected_xml)
229
232
  end
230
233
  end
231
-
232
234
  end