representative 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require "representative/xml"
4
-
4
+ require "representative/xml_behaviour"
5
5
  require "rexml/document"
6
6
 
7
7
  describe Representative::Xml do
@@ -18,267 +18,6 @@ describe Representative::Xml do
18
18
  @xml.target!
19
19
  end
20
20
 
21
- describe "for some 'subject'" do
22
-
23
- before do
24
- @subject = OpenStruct.new(:name => "Fred", :width => 200, :vehicle => OpenStruct.new(:year => "1959", :make => "Chevrolet"))
25
- end
26
-
27
- describe "#element" do
28
-
29
- it "generates an element with content extracted from the subject" do
30
- r.element :name
31
- resulting_xml.should == %(<name>Fred</name>)
32
- end
33
-
34
- it "dasherizes the property name" do
35
- @subject.full_name = "Fredrick"
36
- r.element :full_name
37
- resulting_xml.should == %(<full-name>Fredrick</full-name>)
38
- end
39
-
40
- describe "with attributes" do
41
-
42
- it "generates attributes on the element" do
43
- r.element :name, :lang => "fr"
44
- resulting_xml.should == %(<name lang="fr">Fred</name>)
45
- end
46
-
47
- it "dasherizes the attribute name" do
48
- r.element :name, :sourced_from => "phonebook"
49
- resulting_xml.should == %(<name sourced-from="phonebook">Fred</name>)
50
- end
51
-
52
- describe "whose value supports #to_proc" do
53
-
54
- it "calls the Proc on the subject to generate a value" do
55
- r.element :name, :rev => :reverse
56
- resulting_xml.should == %(<name rev="derF">Fred</name>)
57
- end
58
-
59
- end
60
-
61
- describe "with value nil" do
62
-
63
- it "omits the attribute" do
64
- r.element :name, :lang => nil
65
- resulting_xml.should == %(<name>Fred</name>)
66
- end
67
-
68
- end
69
-
70
- end
71
-
72
- describe "with an explicit value" do
73
-
74
- it "generates an element with explicitly provided content" do
75
- r.element :name, "Bloggs"
76
- resulting_xml.should == %(<name>Bloggs</name>)
77
- end
78
-
79
- describe "AND attributes" do
80
-
81
- it "generates attributes on the element" do
82
- r.element :name, "Bloggs", :lang => "fr"
83
- resulting_xml.should == %(<name lang="fr">Bloggs</name>)
84
- end
85
-
86
- end
87
-
88
- end
89
-
90
- describe "with a value argument that supports #to_proc" do
91
-
92
- it "calls the Proc on the subject to generate a value" do
93
- r.element :name, :width
94
- resulting_xml.should == %(<name>200</name>)
95
- end
96
-
97
- end
98
-
99
- describe "with value argument :self" do
100
-
101
- it "doesn't alter the subject" do
102
- r.element :info, :self do
103
- r.element :name
104
- end
105
- resulting_xml.should == %(<info><name>Fred</name></info>)
106
- end
107
-
108
- end
109
-
110
- describe "with value argument nil" do
111
-
112
- it "builds an empty element" do
113
- r.element :name, nil
114
- resulting_xml.should == %(<name/>)
115
- end
116
-
117
- describe "and attributes" do
118
-
119
- it "omits attributes derived from the subject" do
120
- r.element :name, nil, :size => :size
121
- resulting_xml.should == %(<name/>)
122
- end
123
-
124
- it "retains attributes with explicit values" do
125
- r.element :name, nil, :lang => "en"
126
- resulting_xml.should == %(<name lang="en"/>)
127
- end
128
-
129
- end
130
-
131
- describe "and a block" do
132
-
133
- it "doesn't call the block" do
134
- r.element :name, nil do
135
- raise "hell"
136
- end
137
- resulting_xml.should == %(<name/>)
138
- end
139
-
140
- end
141
-
142
- end
143
-
144
- describe "with a block" do
145
-
146
- it "generates nested elements" do
147
- r.element :vehicle do
148
- r.element :year
149
- r.element :make
150
- end
151
- resulting_xml.should == %(<vehicle><year>1959</year><make>Chevrolet</make></vehicle>)
152
- end
153
-
154
- it "yields each new subject" do
155
- r.element :vehicle do |vehicle|
156
- r.element :year, vehicle.year
157
- end
158
- resulting_xml.should == %(<vehicle><year>1959</year></vehicle>)
159
- end
160
-
161
- end
162
-
163
- describe "with an EMPTY block" do
164
-
165
- it "generates an empty element" do
166
- r.element :vehicle, :year => :year, &r.empty
167
- resulting_xml.should == %(<vehicle year="1959"/>)
168
- end
169
-
170
- end
171
-
172
- end
173
-
174
- describe "#list_of" do
175
-
176
- before do
177
- @subject.nick_names = ["Freddie", "Knucklenose"]
178
- end
179
-
180
- it "generates an array element" do
181
- r.list_of(:nick_names)
182
- resulting_xml.should == %(<nick-names type="array"><nick-name>Freddie</nick-name><nick-name>Knucklenose</nick-name></nick-names>)
183
- end
184
-
185
- describe "with :list_attributes" do
186
-
187
- it "attaches attributes to the array element" do
188
- r.list_of(:nick_names, :list_attributes => {:color => "blue", :size => :size})
189
- array_element_attributes = REXML::Document.new(resulting_xml).root.attributes
190
- array_element_attributes["type"].should == "array"
191
- array_element_attributes["color"].should == "blue"
192
- array_element_attributes["size"].should == "2"
193
- array_element_attributes.size.should == 3
194
- end
195
-
196
- end
197
-
198
- describe "with :item_attributes" do
199
-
200
- it "attaches attributes to each item element" do
201
- r.list_of(:nick_names, :item_attributes => {:length => :size})
202
- resulting_xml.should == %(<nick-names type="array"><nick-name length="7">Freddie</nick-name><nick-name length="11">Knucklenose</nick-name></nick-names>)
203
- end
204
-
205
- end
206
-
207
- describe "with an explicit :item_name" do
208
- it "uses the name provided" do
209
- r.list_of(:nick_names, :item_name => :nick)
210
- resulting_xml.should == %(<nick-names type="array"><nick>Freddie</nick><nick>Knucklenose</nick></nick-names>)
211
- end
212
- end
213
-
214
- describe "with an argument that resolves to nil" do
215
-
216
- it "omits the attribute" do
217
- r.list_of(:services) do
218
- r.date
219
- end
220
- resulting_xml.should == %(<services/>)
221
- end
222
-
223
- end
224
-
225
- describe "with a block" do
226
-
227
- it "generates a nested element for each list element" do
228
- r.list_of(:nick_names) do
229
- r.element :length
230
- end
231
- resulting_xml.should == %(<nick-names type="array"><nick-name><length>7</length></nick-name><nick-name><length>11</length></nick-name></nick-names>)
232
- end
233
-
234
- end
235
-
236
- describe "with an EMPTY block" do
237
-
238
- it "generates empty elements for each list element" do
239
- r.list_of(:nick_names, :item_attributes => {:value => :to_s}, &r.empty)
240
- resulting_xml.should == %(<nick-names type="array"><nick-name value="Freddie"/><nick-name value="Knucklenose"/></nick-names>)
241
- end
242
-
243
- end
244
-
245
- describe "with :item_attributes AND block" do
246
-
247
- it "generates attributes and nested elements" do
248
- r.list_of(:nick_names, :item_attributes => {:length => :size}) do
249
- r.element :reverse
250
- end
251
- resulting_xml.should == %(<nick-names type="array"><nick-name length="7"><reverse>eidderF</reverse></nick-name><nick-name length="11"><reverse>esonelkcunK</reverse></nick-name></nick-names>)
252
- end
253
-
254
- end
255
-
256
- end
257
-
258
- describe "#representing" do
259
-
260
- it "selects a new subject without generating an element" do
261
- r.representing :vehicle do
262
- r.element :make
263
- end
264
- resulting_xml.should == %(<make>Chevrolet</make>)
265
- end
266
-
267
- end
268
-
269
- describe "#comment" do
270
-
271
- it "inserts a comment" do
272
- r.element :vehicle do
273
- r.comment "Year of manufacture"
274
- r.element :year
275
- end
276
- resulting_xml.should ==
277
- %(<vehicle><!-- Year of manufacture --><year>1959</year></vehicle>)
278
- end
279
-
280
- end
281
-
282
- end
283
-
21
+ it_should_behave_like "an XML Representative"
22
+
284
23
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  require 'spec'
2
2
 
3
- $LOAD_PATH.unshift(File.dirname(__FILE__))
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
-
6
3
  require "rubygems"
7
4
 
8
5
  Spec::Runner.configure do |config|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: representative
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 5
10
- version: 0.2.5
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Williams
@@ -15,13 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-13 00:00:00 +10:00
18
+ date: 2010-11-16 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: activesupport
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
24
  requirements:
27
25
  - - ">="
@@ -32,12 +30,12 @@ dependencies:
32
30
  - 2
33
31
  - 2
34
32
  version: 2.2.2
33
+ requirement: *id001
35
34
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: i18n
35
+ name: activesupport
39
36
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
39
  none: false
42
40
  requirements:
43
41
  - - ">="
@@ -48,12 +46,12 @@ dependencies:
48
46
  - 4
49
47
  - 1
50
48
  version: 0.4.1
49
+ requirement: *id002
51
50
  type: :runtime
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: builder
51
+ name: i18n
55
52
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
53
+ - !ruby/object:Gem::Dependency
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
55
  none: false
58
56
  requirements:
59
57
  - - ">="
@@ -64,12 +62,12 @@ dependencies:
64
62
  - 1
65
63
  - 2
66
64
  version: 2.1.2
65
+ requirement: *id003
67
66
  type: :runtime
68
- version_requirements: *id003
69
- - !ruby/object:Gem::Dependency
70
- name: json
67
+ name: builder
71
68
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
69
+ - !ruby/object:Gem::Dependency
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
73
71
  none: false
74
72
  requirements:
75
73
  - - ">="
@@ -80,12 +78,12 @@ dependencies:
80
78
  - 4
81
79
  - 5
82
80
  version: 1.4.5
81
+ requirement: *id004
83
82
  type: :runtime
84
- version_requirements: *id004
85
- - !ruby/object:Gem::Dependency
86
- name: rake
83
+ name: json
87
84
  prerelease: false
88
- requirement: &id005 !ruby/object:Gem::Requirement
85
+ - !ruby/object:Gem::Dependency
86
+ version_requirements: &id005 !ruby/object:Gem::Requirement
89
87
  none: false
90
88
  requirements:
91
89
  - - ~>
@@ -96,12 +94,12 @@ dependencies:
96
94
  - 8
97
95
  - 7
98
96
  version: 0.8.7
97
+ requirement: *id005
99
98
  type: :development
100
- version_requirements: *id005
101
- - !ruby/object:Gem::Dependency
102
- name: rspec
99
+ name: rake
103
100
  prerelease: false
104
- requirement: &id006 !ruby/object:Gem::Requirement
101
+ - !ruby/object:Gem::Dependency
102
+ version_requirements: &id006 !ruby/object:Gem::Requirement
105
103
  none: false
106
104
  requirements:
107
105
  - - ~>
@@ -112,28 +110,43 @@ dependencies:
112
110
  - 3
113
111
  - 0
114
112
  version: 1.3.0
113
+ requirement: *id006
115
114
  type: :development
116
- version_requirements: *id006
117
- - !ruby/object:Gem::Dependency
118
- name: yard
115
+ name: rspec
119
116
  prerelease: false
120
- requirement: &id007 !ruby/object:Gem::Requirement
117
+ - !ruby/object:Gem::Dependency
118
+ version_requirements: &id007 !ruby/object:Gem::Requirement
121
119
  none: false
122
120
  requirements:
123
- - - ~>
121
+ - - ">="
124
122
  - !ruby/object:Gem::Version
125
- hash: 27
123
+ hash: 25
126
124
  segments:
127
125
  - 0
128
- - 5
129
- - 8
130
- version: 0.5.8
126
+ - 9
127
+ version: "0.9"
128
+ requirement: *id007
131
129
  type: :development
132
- version_requirements: *id007
130
+ name: tilt
131
+ prerelease: false
133
132
  - !ruby/object:Gem::Dependency
134
- name: diff-lcs
133
+ version_requirements: &id008 !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 1
141
+ - 4
142
+ - 2
143
+ version: 1.4.2
144
+ requirement: *id008
145
+ type: :development
146
+ name: nokogiri
135
147
  prerelease: false
136
- requirement: &id008 !ruby/object:Gem::Requirement
148
+ - !ruby/object:Gem::Dependency
149
+ version_requirements: &id009 !ruby/object:Gem::Requirement
137
150
  none: false
138
151
  requirements:
139
152
  - - ">="
@@ -142,8 +155,10 @@ dependencies:
142
155
  segments:
143
156
  - 0
144
157
  version: "0"
158
+ requirement: *id009
145
159
  type: :development
146
- version_requirements: *id008
160
+ name: diff-lcs
161
+ prerelease: false
147
162
  description:
148
163
  email: mdub@dogbiscuit.org
149
164
  executables: []
@@ -153,10 +168,13 @@ extensions: []
153
168
  extra_rdoc_files: []
154
169
 
155
170
  files:
171
+ - lib/representative/abstract_xml.rb
156
172
  - lib/representative/base.rb
157
173
  - lib/representative/empty.rb
158
174
  - lib/representative/json.rb
175
+ - lib/representative/nokogiri.rb
159
176
  - lib/representative/object_inspector.rb
177
+ - lib/representative/tilt_integration.rb
160
178
  - lib/representative/version.rb
161
179
  - lib/representative/xml.rb
162
180
  - lib/representative.rb
@@ -165,6 +183,9 @@ files:
165
183
  - README.markdown
166
184
  - LICENSE
167
185
  - spec/representative/json_spec.rb
186
+ - spec/representative/nokogiri_spec.rb
187
+ - spec/representative/tilt_integration_spec.rb
188
+ - spec/representative/xml_behaviour.rb
168
189
  - spec/representative/xml_spec.rb
169
190
  - spec/spec_helper.rb
170
191
  - Rakefile
@@ -204,6 +225,9 @@ specification_version: 3
204
225
  summary: Builds XML and JSON representations of your Ruby objects
205
226
  test_files:
206
227
  - spec/representative/json_spec.rb
228
+ - spec/representative/nokogiri_spec.rb
229
+ - spec/representative/tilt_integration_spec.rb
230
+ - spec/representative/xml_behaviour.rb
207
231
  - spec/representative/xml_spec.rb
208
232
  - spec/spec_helper.rb
209
233
  - Rakefile