representative 1.0.5 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +1 -1
- data/README.markdown +9 -6
- data/Rakefile +0 -1
- data/lib/representative/abstract_xml.rb +1 -0
- data/lib/representative/base.rb +2 -2
- data/lib/representative/json.rb +2 -2
- data/lib/representative/nokogiri.rb +7 -8
- data/lib/representative/version.rb +1 -1
- data/lib/representative/xml.rb +4 -4
- data/spec/representative/json_spec.rb +42 -25
- data/spec/representative/nokogiri_spec.rb +6 -6
- data/spec/representative/tilt_integration_spec.rb +7 -7
- data/spec/representative/xml_behaviour.rb +36 -37
- metadata +41 -43
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fc5e48edec35400a5461c71ba4627468910096372de5963d2f324c4e4c4b3596
|
4
|
+
data.tar.gz: e4752a8734226a997f9eef820cc6b57683640cd3b0426b1de148305b1f59083e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c174f553dada2ef722f402703fa253f3f6a5b0d431dd5cd81cf1af3fc0f3d56f39207cd9da9d6f817f510b38051fe1232296d1ca2bab186e95b8d51fa3c21ed
|
7
|
+
data.tar.gz: 380a2542367e38778bdcb35e80df6e2f9c0e395b32b26fd2acc882dd462b1d652cead243ea06072e828b0b7800c5f05c443841977119774c055d3a819a96a560
|
data/LICENSE
CHANGED
data/README.markdown
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
Representative
|
2
2
|
==============
|
3
3
|
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/representative.png)](http://badge.fury.io/rb/representative)
|
5
|
+
[![Build Status](https://travis-ci.org/mdub/representative.svg?branch=master)](https://travis-ci.org/mdub/representative)
|
6
|
+
|
4
7
|
"Representative" makes it easier to create XML or JSON representations of your Ruby objects.
|
5
8
|
|
6
9
|
It works best when you want the output to roughly follow the object structure, but still want complete control of the result.
|
@@ -12,7 +15,7 @@ Given a Ruby data-structure:
|
|
12
15
|
|
13
16
|
@books = [
|
14
17
|
Book.new(
|
15
|
-
:title => "Sailing for old dogs",
|
18
|
+
:title => "Sailing for old dogs",
|
16
19
|
:authors => ["Jim Watson"],
|
17
20
|
:published => Publication.new(
|
18
21
|
:by => "Credulous Print",
|
@@ -20,7 +23,7 @@ Given a Ruby data-structure:
|
|
20
23
|
)
|
21
24
|
),
|
22
25
|
Book.new(
|
23
|
-
:title => "On the horizon",
|
26
|
+
:title => "On the horizon",
|
24
27
|
:authors => ["Zoe Primpton", "Stan Ford"],
|
25
28
|
:published => Publication.new(
|
26
29
|
:by => "McGraw-Hill",
|
@@ -37,7 +40,7 @@ Given a Ruby data-structure:
|
|
37
40
|
Representative::Nokogiri can be used to generate XML:
|
38
41
|
|
39
42
|
xml = Representative::Nokogiri.new do |r|
|
40
|
-
|
43
|
+
|
41
44
|
r.list_of :books, @books do
|
42
45
|
r.element :title
|
43
46
|
r.list_of :authors
|
@@ -46,7 +49,7 @@ Representative::Nokogiri can be used to generate XML:
|
|
46
49
|
r.element :year
|
47
50
|
end
|
48
51
|
end
|
49
|
-
|
52
|
+
|
50
53
|
end
|
51
54
|
|
52
55
|
puts xml.to_s
|
@@ -98,7 +101,7 @@ Generating JSON
|
|
98
101
|
Representative::Json can be used to generate JSON, using exactly the same DSL:
|
99
102
|
|
100
103
|
json = Representative::Json.new do |r|
|
101
|
-
|
104
|
+
|
102
105
|
r.list_of :books, @books do
|
103
106
|
r.element :title
|
104
107
|
r.list_of :authors
|
@@ -169,7 +172,7 @@ This registers handlers for "`.xml.rep`" and "`.json.rep`" templates.
|
|
169
172
|
Copyright
|
170
173
|
---------
|
171
174
|
|
172
|
-
Copyright (c) 2009 Mike Williams. See LICENSE for details.
|
175
|
+
Copyright (c) 2009-2018 Mike Williams. See LICENSE for details.
|
173
176
|
|
174
177
|
Similar projects
|
175
178
|
----------------
|
data/Rakefile
CHANGED
data/lib/representative/base.rb
CHANGED
@@ -43,8 +43,8 @@ module Representative
|
|
43
43
|
current_subject
|
44
44
|
elsif value_generator.kind_of?(Symbol)
|
45
45
|
current_subject.send(value_generator) unless current_subject.nil?
|
46
|
-
elsif value_generator.
|
47
|
-
value_generator.
|
46
|
+
elsif value_generator.kind_of?(Proc)
|
47
|
+
value_generator.call(current_subject) unless current_subject.nil?
|
48
48
|
else
|
49
49
|
value_generator
|
50
50
|
end
|
data/lib/representative/json.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
-
require "active_support/core_ext"
|
2
1
|
require "nokogiri"
|
3
2
|
require "representative/abstract_xml"
|
4
3
|
require "representative/empty"
|
5
4
|
|
6
5
|
module Representative
|
7
|
-
|
6
|
+
|
8
7
|
# Easily generate XML while traversing an object-graph.
|
9
8
|
#
|
10
9
|
class Nokogiri < AbstractXml
|
11
|
-
|
10
|
+
|
12
11
|
def initialize(subject = nil, options = {})
|
13
12
|
super(subject, options)
|
14
13
|
@doc = ::Nokogiri::XML::Document.new
|
@@ -35,7 +34,7 @@ module Representative
|
|
35
34
|
comment_node = ::Nokogiri::XML::Comment.new(doc, " #{text} ")
|
36
35
|
current_element.add_child(comment_node)
|
37
36
|
end
|
38
|
-
|
37
|
+
|
39
38
|
def attribute(name, value_generator = name)
|
40
39
|
attribute_name = name.to_s.dasherize
|
41
40
|
value = resolve_value(value_generator)
|
@@ -45,7 +44,7 @@ module Representative
|
|
45
44
|
end
|
46
45
|
|
47
46
|
private
|
48
|
-
|
47
|
+
|
49
48
|
def generate_element(name, resolved_attributes, content_string)
|
50
49
|
tag_args = [content_string, resolved_attributes].compact
|
51
50
|
new_element = doc.create_element(name, *tag_args)
|
@@ -54,13 +53,13 @@ module Representative
|
|
54
53
|
old_element = @current_element
|
55
54
|
begin
|
56
55
|
@current_element = new_element
|
57
|
-
yield
|
56
|
+
yield
|
58
57
|
ensure
|
59
58
|
@current_element = old_element
|
60
59
|
end
|
61
60
|
end
|
62
61
|
end
|
63
|
-
|
62
|
+
|
64
63
|
end
|
65
|
-
|
64
|
+
|
66
65
|
end
|
data/lib/representative/xml.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "active_support/
|
1
|
+
require "active_support/inflector"
|
2
2
|
require "builder"
|
3
3
|
require "representative/abstract_xml"
|
4
4
|
require "representative/empty"
|
@@ -24,14 +24,14 @@ module Representative
|
|
24
24
|
def comment(text)
|
25
25
|
@xml.comment!(text)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
protected
|
29
|
-
|
29
|
+
|
30
30
|
def generate_element(name, resolved_attributes, content_string, &content_block)
|
31
31
|
tag_args = [content_string, resolved_attributes].compact
|
32
32
|
@xml.tag!(name, *tag_args, &content_block)
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
@@ -20,7 +20,7 @@ describe Representative::Json do
|
|
20
20
|
|
21
21
|
it "outputs the value as JSON" do
|
22
22
|
r.element :name, "Fred"
|
23
|
-
resulting_json.
|
23
|
+
expect(resulting_json).to eq %{"Fred"\n}
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
@@ -29,7 +29,7 @@ describe Representative::Json do
|
|
29
29
|
|
30
30
|
it "outputs the value as JSON" do
|
31
31
|
r.element :age, 36
|
32
|
-
resulting_json.
|
32
|
+
expect(resulting_json).to eq "36\n"
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
@@ -38,7 +38,7 @@ describe Representative::Json do
|
|
38
38
|
|
39
39
|
it "generates null" do
|
40
40
|
r.element :flavour, nil
|
41
|
-
resulting_json.
|
41
|
+
expect(resulting_json).to eq "null\n"
|
42
42
|
end
|
43
43
|
|
44
44
|
describe "and a block" do
|
@@ -47,7 +47,7 @@ describe Representative::Json do
|
|
47
47
|
r.element :book, nil do
|
48
48
|
r.element :author
|
49
49
|
end
|
50
|
-
resulting_json.
|
50
|
+
expect(resulting_json).to eq "null\n"
|
51
51
|
end
|
52
52
|
|
53
53
|
end
|
@@ -63,7 +63,7 @@ describe Representative::Json do
|
|
63
63
|
r.element :book, @book, :lang => :lang do
|
64
64
|
r.element :title
|
65
65
|
end
|
66
|
-
resulting_json.
|
66
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
67
67
|
{
|
68
68
|
"@lang": "fr",
|
69
69
|
"title": "En Edge"
|
@@ -77,7 +77,7 @@ describe Representative::Json do
|
|
77
77
|
|
78
78
|
it "ignores the attributes" do
|
79
79
|
r.element :review, "Blah de blah", :lang => "fr"
|
80
|
-
resulting_json.
|
80
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
81
81
|
"Blah de blah"
|
82
82
|
JSON
|
83
83
|
end
|
@@ -93,7 +93,7 @@ describe Representative::Json do
|
|
93
93
|
r.representing(@author) do
|
94
94
|
r.element :name
|
95
95
|
end
|
96
|
-
resulting_json.
|
96
|
+
expect(resulting_json).to eq %{"Fred"\n}
|
97
97
|
end
|
98
98
|
|
99
99
|
end
|
@@ -103,7 +103,7 @@ describe Representative::Json do
|
|
103
103
|
it "outputs an object" do
|
104
104
|
r.element :something, Object.new do
|
105
105
|
end
|
106
|
-
resulting_json.
|
106
|
+
expect(resulting_json).to eq "{}\n"
|
107
107
|
end
|
108
108
|
|
109
109
|
end
|
@@ -116,7 +116,7 @@ describe Representative::Json do
|
|
116
116
|
|
117
117
|
it "outputs the array as JSON" do
|
118
118
|
r.list_of :names, %w(Hewey Dewey Louie)
|
119
|
-
resulting_json.
|
119
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
120
120
|
[
|
121
121
|
"Hewey",
|
122
122
|
"Dewey",
|
@@ -134,7 +134,7 @@ describe Representative::Json do
|
|
134
134
|
r.element(:duck, @donald) do
|
135
135
|
r.list_of :nephews
|
136
136
|
end
|
137
|
-
resulting_json.
|
137
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
138
138
|
{
|
139
139
|
"nephews": [
|
140
140
|
"Hewey",
|
@@ -159,7 +159,7 @@ describe Representative::Json do
|
|
159
159
|
r.element :name
|
160
160
|
r.element :age
|
161
161
|
end
|
162
|
-
resulting_json.
|
162
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
163
163
|
[
|
164
164
|
{
|
165
165
|
"name": "Hewey",
|
@@ -190,7 +190,7 @@ describe Representative::Json do
|
|
190
190
|
r.element :name
|
191
191
|
r.element :age
|
192
192
|
end
|
193
|
-
resulting_json.
|
193
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
194
194
|
[
|
195
195
|
{
|
196
196
|
"@about": "Hewey is 3 years old",
|
@@ -215,16 +215,18 @@ describe Representative::Json do
|
|
215
215
|
describe "with list attributes" do
|
216
216
|
it "raises an ArgumentError" do
|
217
217
|
@authors = []
|
218
|
-
|
218
|
+
expect(-> {
|
219
|
+
r.list_of(:authors, @authors, :list_attributes => {}) {}
|
220
|
+
}).to raise_exception(ArgumentError)
|
219
221
|
end
|
220
222
|
end
|
221
223
|
|
222
224
|
describe "with unnecessary arguments" do
|
223
225
|
it "raises an ArgumentError" do
|
224
226
|
@authors = []
|
225
|
-
|
227
|
+
expect(-> {
|
226
228
|
r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
|
227
|
-
}.
|
229
|
+
}).to raise_exception(ArgumentError)
|
228
230
|
end
|
229
231
|
end
|
230
232
|
|
@@ -234,7 +236,7 @@ describe Representative::Json do
|
|
234
236
|
|
235
237
|
it "inserts a comment" do
|
236
238
|
r.comment "now pay attention"
|
237
|
-
resulting_json.
|
239
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
238
240
|
// now pay attention
|
239
241
|
JSON
|
240
242
|
end
|
@@ -252,7 +254,7 @@ describe Representative::Json do
|
|
252
254
|
r.element :name, "Fred"
|
253
255
|
r.element :age, 36
|
254
256
|
end
|
255
|
-
resulting_json.
|
257
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
256
258
|
{
|
257
259
|
"name": "Fred",
|
258
260
|
"age": 36
|
@@ -268,7 +270,7 @@ describe Representative::Json do
|
|
268
270
|
r.element :name
|
269
271
|
r.element :age
|
270
272
|
end
|
271
|
-
resulting_json.
|
273
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
272
274
|
{
|
273
275
|
"name": "Fred",
|
274
276
|
"age": 36
|
@@ -278,6 +280,21 @@ describe Representative::Json do
|
|
278
280
|
|
279
281
|
end
|
280
282
|
|
283
|
+
describe "when value is a Hash" do
|
284
|
+
|
285
|
+
it "renders the Hash as JSON" do
|
286
|
+
@article = OpenStruct.new(:title => "blah", :tags => {:foo => "bar"})
|
287
|
+
r.element :article, @article do
|
288
|
+
r.element :tags
|
289
|
+
end
|
290
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
291
|
+
{
|
292
|
+
"tags": {"foo":"bar"}
|
293
|
+
}
|
294
|
+
JSON
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
281
298
|
end
|
282
299
|
|
283
300
|
describe "#attribute" do
|
@@ -287,7 +304,7 @@ describe Representative::Json do
|
|
287
304
|
r.attribute :href, "http://example.com/authors/1"
|
288
305
|
r.element :name, "Fred"
|
289
306
|
end
|
290
|
-
resulting_json.
|
307
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
291
308
|
{
|
292
309
|
"@href": "http://example.com/authors/1",
|
293
310
|
"name": "Fred"
|
@@ -306,7 +323,7 @@ describe Representative::Json do
|
|
306
323
|
r.comment "age is irrelevant"
|
307
324
|
r.element :age
|
308
325
|
end
|
309
|
-
resulting_json.
|
326
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
310
327
|
{
|
311
328
|
"name": "Fred",
|
312
329
|
// age is irrelevant
|
@@ -325,7 +342,7 @@ describe Representative::Json do
|
|
325
342
|
r.element :user, Object.new do
|
326
343
|
r.element :full_name, "Fred Bloggs"
|
327
344
|
end
|
328
|
-
resulting_json.
|
345
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
329
346
|
{
|
330
347
|
"full_name": "Fred Bloggs"
|
331
348
|
}
|
@@ -342,7 +359,7 @@ describe Representative::Json do
|
|
342
359
|
r.attribute :alt_url, "http://xyz.com"
|
343
360
|
r.element :full_name
|
344
361
|
end
|
345
|
-
resulting_json.
|
362
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
346
363
|
{
|
347
364
|
"@altUrl": "http://xyz.com",
|
348
365
|
"fullName": "Fred Bloggs"
|
@@ -368,7 +385,7 @@ describe Representative::Json do
|
|
368
385
|
r.element :name
|
369
386
|
r.element :age
|
370
387
|
end
|
371
|
-
resulting_json.
|
388
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
372
389
|
[
|
373
390
|
{
|
374
391
|
"name": "Hewey",
|
@@ -391,7 +408,7 @@ describe Representative::Json do
|
|
391
408
|
r.element :name
|
392
409
|
r.element :age
|
393
410
|
end
|
394
|
-
resulting_json.
|
411
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
395
412
|
[
|
396
413
|
\t{
|
397
414
|
\t\t"name": "Hewey",
|
@@ -414,7 +431,7 @@ describe Representative::Json do
|
|
414
431
|
r.element :name
|
415
432
|
r.element :age
|
416
433
|
end
|
417
|
-
resulting_json.
|
434
|
+
expect(resulting_json).to eq %([{"name":"Hewey","age":3},{"name":"Dewey","age":4}])
|
418
435
|
end
|
419
436
|
|
420
437
|
end
|
@@ -29,7 +29,7 @@ describe Representative::Nokogiri do
|
|
29
29
|
r.element :person, @subject do
|
30
30
|
r.attribute :name
|
31
31
|
end
|
32
|
-
resulting_xml.
|
32
|
+
expect(resulting_xml).to eq %(<person name="Fred"/>)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -38,16 +38,16 @@ describe Representative::Nokogiri do
|
|
38
38
|
r.element :person, @subject do
|
39
39
|
r.attribute :lang, "fr"
|
40
40
|
end
|
41
|
-
resulting_xml.
|
41
|
+
expect(resulting_xml).to eq %(<person lang="fr"/>)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe "with a value that supports #to_proc" do
|
46
46
|
it "calls the Proc on the subject to generate attribute value" do
|
47
47
|
r.element :person, @subject do
|
48
|
-
r.attribute :name, lambda { |person| person.name.reverse }
|
48
|
+
r.attribute :name, lambda { |person| person.name.reverse }
|
49
49
|
end
|
50
|
-
resulting_xml.
|
50
|
+
expect(resulting_xml).to eq %(<person name="derF"/>)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -55,7 +55,7 @@ describe Representative::Nokogiri do
|
|
55
55
|
r.element :name do
|
56
56
|
r.attribute :sourced_from, "phonebook"
|
57
57
|
end
|
58
|
-
resulting_xml.
|
58
|
+
expect(resulting_xml).to eq %(<name sourced-from="phonebook"/>)
|
59
59
|
end
|
60
60
|
|
61
61
|
describe "with value nil" do
|
@@ -63,7 +63,7 @@ describe Representative::Nokogiri do
|
|
63
63
|
r.element :person, @subject do
|
64
64
|
r.attribute :name, nil
|
65
65
|
end
|
66
|
-
resulting_xml.
|
66
|
+
expect(resulting_xml).to eq %(<person/>)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -26,7 +26,7 @@ describe Representative::Tilt do
|
|
26
26
|
r.element :foo, "bar"
|
27
27
|
RUBY
|
28
28
|
render
|
29
|
-
resulting_xml.
|
29
|
+
expect(resulting_xml).to eq %{<foo>bar</foo>\n}
|
30
30
|
end
|
31
31
|
|
32
32
|
it "provides access to scope" do
|
@@ -43,7 +43,7 @@ describe Representative::Tilt do
|
|
43
43
|
end
|
44
44
|
render(scope)
|
45
45
|
|
46
|
-
resulting_xml.
|
46
|
+
expect(resulting_xml).to eq undent(<<-XML)
|
47
47
|
<author>
|
48
48
|
<name>Mike</name>
|
49
49
|
</author>
|
@@ -61,7 +61,7 @@ describe Representative::Tilt do
|
|
61
61
|
|
62
62
|
render(Object.new, {:author => OpenStruct.new(:name => "Mike")})
|
63
63
|
|
64
|
-
resulting_xml.
|
64
|
+
expect(resulting_xml).to eq undent(<<-XML)
|
65
65
|
<author>
|
66
66
|
<name>Mike</name>
|
67
67
|
</author>
|
@@ -72,7 +72,7 @@ describe Representative::Tilt do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
describe "JSON template" do
|
77
77
|
|
78
78
|
def resulting_json
|
@@ -86,7 +86,7 @@ describe Representative::Tilt do
|
|
86
86
|
r.element :foo, "bar"
|
87
87
|
RUBY
|
88
88
|
render
|
89
|
-
resulting_json.
|
89
|
+
expect(resulting_json).to eq %{"bar"\n}
|
90
90
|
end
|
91
91
|
|
92
92
|
it "provides access to scope" do
|
@@ -103,7 +103,7 @@ describe Representative::Tilt do
|
|
103
103
|
end
|
104
104
|
render(scope)
|
105
105
|
|
106
|
-
resulting_json.
|
106
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
107
107
|
{
|
108
108
|
"name": "Mike"
|
109
109
|
}
|
@@ -121,7 +121,7 @@ describe Representative::Tilt do
|
|
121
121
|
|
122
122
|
render(Object.new, {:author => OpenStruct.new(:name => "Mike")})
|
123
123
|
|
124
|
-
resulting_json.
|
124
|
+
expect(resulting_json).to eq undent(<<-JSON)
|
125
125
|
{
|
126
126
|
"name": "Mike"
|
127
127
|
}
|
@@ -19,31 +19,31 @@ shared_examples_for "an XML Representative" do
|
|
19
19
|
|
20
20
|
it "generates an element with content extracted from the subject" do
|
21
21
|
r.element :name
|
22
|
-
resulting_xml.
|
22
|
+
expect(resulting_xml).to eq %(<name>Fred</name>)
|
23
23
|
end
|
24
24
|
|
25
25
|
it "dasherizes the property name" do
|
26
26
|
r.element :full_name
|
27
|
-
resulting_xml.
|
27
|
+
expect(resulting_xml).to eq %(<full-name>Fredrick</full-name>)
|
28
28
|
end
|
29
29
|
|
30
30
|
describe "with attributes" do
|
31
31
|
|
32
32
|
it "generates attributes on the element" do
|
33
33
|
r.element :name, :lang => "fr"
|
34
|
-
resulting_xml.
|
34
|
+
expect(resulting_xml).to eq %(<name lang="fr">Fred</name>)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "dasherizes the attribute name" do
|
38
38
|
r.element :name, :sourced_from => "phonebook"
|
39
|
-
resulting_xml.
|
39
|
+
expect(resulting_xml).to eq %(<name sourced-from="phonebook">Fred</name>)
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "whose value supports #to_proc" do
|
43
43
|
|
44
44
|
it "calls the Proc on the subject to generate a value" do
|
45
45
|
r.element :name, :rev => :reverse
|
46
|
-
resulting_xml.
|
46
|
+
expect(resulting_xml).to eq %(<name rev="derF">Fred</name>)
|
47
47
|
end
|
48
48
|
|
49
49
|
end
|
@@ -52,7 +52,7 @@ shared_examples_for "an XML Representative" do
|
|
52
52
|
|
53
53
|
it "omits the attribute" do
|
54
54
|
r.element :name, :lang => nil
|
55
|
-
resulting_xml.
|
55
|
+
expect(resulting_xml).to eq %(<name>Fred</name>)
|
56
56
|
end
|
57
57
|
|
58
58
|
end
|
@@ -63,14 +63,14 @@ shared_examples_for "an XML Representative" do
|
|
63
63
|
|
64
64
|
it "generates an element with explicitly provided content" do
|
65
65
|
r.element :name, "Bloggs"
|
66
|
-
resulting_xml.
|
66
|
+
expect(resulting_xml).to eq %(<name>Bloggs</name>)
|
67
67
|
end
|
68
68
|
|
69
69
|
describe "AND attributes" do
|
70
70
|
|
71
71
|
it "generates attributes on the element" do
|
72
72
|
r.element :name, "Bloggs", :lang => "fr"
|
73
|
-
resulting_xml.
|
73
|
+
expect(resulting_xml).to eq %(<name lang="fr">Bloggs</name>)
|
74
74
|
end
|
75
75
|
|
76
76
|
end
|
@@ -81,7 +81,7 @@ shared_examples_for "an XML Representative" do
|
|
81
81
|
|
82
82
|
it "calls the named method to generate a value" do
|
83
83
|
r.element :name, :width
|
84
|
-
resulting_xml.
|
84
|
+
expect(resulting_xml).to eq %(<name>200</name>)
|
85
85
|
end
|
86
86
|
|
87
87
|
end
|
@@ -90,7 +90,7 @@ shared_examples_for "an XML Representative" do
|
|
90
90
|
|
91
91
|
it "calls the Proc on the subject to generate a value" do
|
92
92
|
r.element :name, (lambda { |x| x.width * 3 })
|
93
|
-
resulting_xml.
|
93
|
+
expect(resulting_xml).to eq %(<name>600</name>)
|
94
94
|
end
|
95
95
|
|
96
96
|
end
|
@@ -101,7 +101,7 @@ shared_examples_for "an XML Representative" do
|
|
101
101
|
r.element :info, :self do
|
102
102
|
r.element :name
|
103
103
|
end
|
104
|
-
resulting_xml.
|
104
|
+
expect(resulting_xml).to eq %(<info><name>Fred</name></info>)
|
105
105
|
end
|
106
106
|
|
107
107
|
end
|
@@ -110,19 +110,19 @@ shared_examples_for "an XML Representative" do
|
|
110
110
|
|
111
111
|
it "builds an empty element" do
|
112
112
|
r.element :name, nil
|
113
|
-
resulting_xml.
|
113
|
+
expect(resulting_xml).to eq %(<name/>)
|
114
114
|
end
|
115
115
|
|
116
116
|
describe "and attributes" do
|
117
117
|
|
118
118
|
it "omits attributes derived from the subject" do
|
119
119
|
r.element :name, nil, :size => :size
|
120
|
-
resulting_xml.
|
120
|
+
expect(resulting_xml).to eq %(<name/>)
|
121
121
|
end
|
122
122
|
|
123
123
|
it "retains attributes with explicit values" do
|
124
124
|
r.element :name, nil, :lang => "en"
|
125
|
-
resulting_xml.
|
125
|
+
expect(resulting_xml).to eq %(<name lang="en"/>)
|
126
126
|
end
|
127
127
|
|
128
128
|
end
|
@@ -133,7 +133,7 @@ shared_examples_for "an XML Representative" do
|
|
133
133
|
r.element :name, nil do
|
134
134
|
raise "hell"
|
135
135
|
end
|
136
|
-
resulting_xml.
|
136
|
+
expect(resulting_xml).to eq %(<name/>)
|
137
137
|
end
|
138
138
|
|
139
139
|
end
|
@@ -147,14 +147,14 @@ shared_examples_for "an XML Representative" do
|
|
147
147
|
r.element :year
|
148
148
|
r.element :make
|
149
149
|
end
|
150
|
-
resulting_xml.
|
150
|
+
expect(resulting_xml).to eq %(<vehicle><year>1959</year><make>Chevrolet</make></vehicle>)
|
151
151
|
end
|
152
152
|
|
153
153
|
it "yields each new subject" do
|
154
154
|
r.element :vehicle do |vehicle|
|
155
155
|
r.element :year, vehicle.year
|
156
156
|
end
|
157
|
-
resulting_xml.
|
157
|
+
expect(resulting_xml).to eq %(<vehicle><year>1959</year></vehicle>)
|
158
158
|
end
|
159
159
|
|
160
160
|
end
|
@@ -163,7 +163,7 @@ shared_examples_for "an XML Representative" do
|
|
163
163
|
|
164
164
|
it "generates an empty element" do
|
165
165
|
r.element :vehicle, :year => :year, &r.empty
|
166
|
-
resulting_xml.
|
166
|
+
expect(resulting_xml).to eq %(<vehicle year="1959"/>)
|
167
167
|
end
|
168
168
|
|
169
169
|
end
|
@@ -178,21 +178,21 @@ shared_examples_for "an XML Representative" do
|
|
178
178
|
|
179
179
|
it "generates an array element" do
|
180
180
|
r.list_of(:nick_names)
|
181
|
-
resulting_xml.
|
181
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name>Freddie</nick-name><nick-name>Knucklenose</nick-name></nick-names>)
|
182
182
|
end
|
183
183
|
|
184
184
|
describe "with a Symbol value argument" do
|
185
185
|
|
186
186
|
it "calls the named method to generate a value" do
|
187
187
|
r.list_of(:foo_bars, :nick_names)
|
188
|
-
resulting_xml.
|
188
|
+
expect(resulting_xml).to eq %(<foo-bars type="array"><foo-bar>Freddie</foo-bar><foo-bar>Knucklenose</foo-bar></foo-bars>)
|
189
189
|
end
|
190
190
|
|
191
191
|
end
|
192
192
|
|
193
193
|
it "generates an array element" do
|
194
194
|
r.list_of(:nick_names)
|
195
|
-
resulting_xml.
|
195
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name>Freddie</nick-name><nick-name>Knucklenose</nick-name></nick-names>)
|
196
196
|
end
|
197
197
|
|
198
198
|
describe "with :list_attributes" do
|
@@ -200,10 +200,10 @@ shared_examples_for "an XML Representative" do
|
|
200
200
|
it "attaches attributes to the array element" do
|
201
201
|
r.list_of(:nick_names, :list_attributes => {:color => "blue", :size => :size})
|
202
202
|
array_element_attributes = REXML::Document.new(resulting_xml).root.attributes
|
203
|
-
array_element_attributes["type"].
|
204
|
-
array_element_attributes["color"].
|
205
|
-
array_element_attributes["size"].
|
206
|
-
array_element_attributes.size.
|
203
|
+
expect(array_element_attributes["type"]).to eq "array"
|
204
|
+
expect(array_element_attributes["color"]).to eq "blue"
|
205
|
+
expect(array_element_attributes["size"]).to eq "2"
|
206
|
+
expect(array_element_attributes.size).to eq 3
|
207
207
|
end
|
208
208
|
|
209
209
|
end
|
@@ -212,7 +212,7 @@ shared_examples_for "an XML Representative" do
|
|
212
212
|
|
213
213
|
it "attaches attributes to each item element" do
|
214
214
|
r.list_of(:nick_names, :item_attributes => {:length => :size})
|
215
|
-
resulting_xml.
|
215
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name length="7">Freddie</nick-name><nick-name length="11">Knucklenose</nick-name></nick-names>)
|
216
216
|
end
|
217
217
|
|
218
218
|
end
|
@@ -220,7 +220,7 @@ shared_examples_for "an XML Representative" do
|
|
220
220
|
describe "with an explicit :item_name" do
|
221
221
|
it "uses the name provided" do
|
222
222
|
r.list_of(:nick_names, :item_name => :nick)
|
223
|
-
resulting_xml.
|
223
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick>Freddie</nick><nick>Knucklenose</nick></nick-names>)
|
224
224
|
end
|
225
225
|
end
|
226
226
|
|
@@ -230,7 +230,7 @@ shared_examples_for "an XML Representative" do
|
|
230
230
|
r.list_of(:services) do
|
231
231
|
r.date
|
232
232
|
end
|
233
|
-
resulting_xml.
|
233
|
+
expect(resulting_xml).to eq %(<services/>)
|
234
234
|
end
|
235
235
|
|
236
236
|
end
|
@@ -241,7 +241,7 @@ shared_examples_for "an XML Representative" do
|
|
241
241
|
r.list_of(:nick_names) do
|
242
242
|
r.element :length
|
243
243
|
end
|
244
|
-
resulting_xml.
|
244
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name><length>7</length></nick-name><nick-name><length>11</length></nick-name></nick-names>)
|
245
245
|
end
|
246
246
|
|
247
247
|
end
|
@@ -250,7 +250,7 @@ shared_examples_for "an XML Representative" do
|
|
250
250
|
|
251
251
|
it "generates empty elements for each list element" do
|
252
252
|
r.list_of(:nick_names, :item_attributes => {:value => :to_s}, &r.empty)
|
253
|
-
resulting_xml.
|
253
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name value="Freddie"/><nick-name value="Knucklenose"/></nick-names>)
|
254
254
|
end
|
255
255
|
|
256
256
|
end
|
@@ -261,7 +261,7 @@ shared_examples_for "an XML Representative" do
|
|
261
261
|
r.list_of(:nick_names, :item_attributes => {:length => :size}) do
|
262
262
|
r.element :reverse
|
263
263
|
end
|
264
|
-
resulting_xml.
|
264
|
+
expect(resulting_xml).to eq %(<nick-names type="array"><nick-name length="7"><reverse>eidderF</reverse></nick-name><nick-name length="11"><reverse>esonelkcunK</reverse></nick-name></nick-names>)
|
265
265
|
end
|
266
266
|
|
267
267
|
end
|
@@ -274,7 +274,7 @@ shared_examples_for "an XML Representative" do
|
|
274
274
|
r.representing :vehicle do
|
275
275
|
r.element :make
|
276
276
|
end
|
277
|
-
resulting_xml.
|
277
|
+
expect(resulting_xml).to eq %(<make>Chevrolet</make>)
|
278
278
|
end
|
279
279
|
|
280
280
|
end
|
@@ -286,8 +286,7 @@ shared_examples_for "an XML Representative" do
|
|
286
286
|
r.comment "Year of manufacture"
|
287
287
|
r.element :year
|
288
288
|
end
|
289
|
-
resulting_xml.
|
290
|
-
%(<vehicle><!-- Year of manufacture --><year>1959</year></vehicle>)
|
289
|
+
expect(resulting_xml).to eq %(<vehicle><!-- Year of manufacture --><year>1959</year></vehicle>)
|
291
290
|
end
|
292
291
|
|
293
292
|
end
|
@@ -296,7 +295,7 @@ shared_examples_for "an XML Representative" do
|
|
296
295
|
|
297
296
|
it "generates camelCased element and attribute names" do
|
298
297
|
r(:naming_strategy => :camelcase).element :full_name, :foo_bar => 5
|
299
|
-
resulting_xml.
|
298
|
+
expect(resulting_xml).to eq %(<fullName fooBar="5">Fredrick</fullName>)
|
300
299
|
end
|
301
300
|
|
302
301
|
end
|
@@ -306,7 +305,7 @@ shared_examples_for "an XML Representative" do
|
|
306
305
|
it "generates suitably transformed names" do
|
307
306
|
biff = lambda { |name| name.upcase }
|
308
307
|
r(:naming_strategy => biff).element :full_name
|
309
|
-
resulting_xml.
|
308
|
+
expect(resulting_xml).to eq %(<FULL_NAME>Fredrick</FULL_NAME>)
|
310
309
|
end
|
311
310
|
|
312
311
|
end
|
@@ -315,7 +314,7 @@ shared_examples_for "an XML Representative" do
|
|
315
314
|
|
316
315
|
it "uses raw names" do
|
317
316
|
r(:naming_strategy => nil).element :full_name
|
318
|
-
resulting_xml.
|
317
|
+
expect(resulting_xml).to eq %(<full_name>Fredrick</full_name>)
|
319
318
|
end
|
320
319
|
|
321
320
|
end
|
metadata
CHANGED
@@ -1,78 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: representative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Mike Williams
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2018-06-28 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
14
|
+
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: 3.4.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport-json_encoder
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
22
34
|
type: :runtime
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
|
-
- -
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
40
|
+
version: 1.1.0
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: i18n
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
|
-
- -
|
45
|
+
- - ">="
|
36
46
|
- !ruby/object:Gem::Version
|
37
47
|
version: 0.4.1
|
38
48
|
type: :runtime
|
39
49
|
prerelease: false
|
40
50
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
51
|
requirements:
|
43
|
-
- -
|
52
|
+
- - ">="
|
44
53
|
- !ruby/object:Gem::Version
|
45
54
|
version: 0.4.1
|
46
55
|
- !ruby/object:Gem::Dependency
|
47
56
|
name: builder
|
48
57
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
58
|
requirements:
|
51
|
-
- -
|
59
|
+
- - ">="
|
52
60
|
- !ruby/object:Gem::Version
|
53
61
|
version: 2.1.2
|
54
62
|
type: :runtime
|
55
63
|
prerelease: false
|
56
64
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
65
|
requirements:
|
59
|
-
- -
|
66
|
+
- - ">="
|
60
67
|
- !ruby/object:Gem::Version
|
61
68
|
version: 2.1.2
|
62
69
|
- !ruby/object:Gem::Dependency
|
63
70
|
name: nokogiri
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
|
-
- -
|
73
|
+
- - ">="
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: 1.4.2
|
70
76
|
type: :runtime
|
71
77
|
prerelease: false
|
72
78
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
79
|
requirements:
|
75
|
-
- -
|
80
|
+
- - ">="
|
76
81
|
- !ruby/object:Gem::Version
|
77
82
|
version: 1.4.2
|
78
83
|
description:
|
@@ -81,6 +86,11 @@ executables: []
|
|
81
86
|
extensions: []
|
82
87
|
extra_rdoc_files: []
|
83
88
|
files:
|
89
|
+
- LICENSE
|
90
|
+
- README.markdown
|
91
|
+
- Rakefile
|
92
|
+
- examples/books.rb
|
93
|
+
- lib/representative.rb
|
84
94
|
- lib/representative/abstract_xml.rb
|
85
95
|
- lib/representative/base.rb
|
86
96
|
- lib/representative/empty.rb
|
@@ -90,52 +100,40 @@ files:
|
|
90
100
|
- lib/representative/tilt_integration.rb
|
91
101
|
- lib/representative/version.rb
|
92
102
|
- lib/representative/xml.rb
|
93
|
-
- lib/representative.rb
|
94
|
-
- examples/books.rb
|
95
|
-
- README.markdown
|
96
|
-
- LICENSE
|
97
103
|
- spec/representative/json_spec.rb
|
98
104
|
- spec/representative/nokogiri_spec.rb
|
99
105
|
- spec/representative/tilt_integration_spec.rb
|
100
106
|
- spec/representative/xml_behaviour.rb
|
101
107
|
- spec/representative/xml_spec.rb
|
102
108
|
- spec/spec_helper.rb
|
103
|
-
- Rakefile
|
104
109
|
homepage: http://github.com/mdub/representative
|
105
110
|
licenses: []
|
111
|
+
metadata: {}
|
106
112
|
post_install_message:
|
107
113
|
rdoc_options: []
|
108
114
|
require_paths:
|
109
115
|
- lib
|
110
116
|
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
-
none: false
|
112
117
|
requirements:
|
113
|
-
- -
|
118
|
+
- - ">="
|
114
119
|
- !ruby/object:Gem::Version
|
115
|
-
version:
|
116
|
-
segments:
|
117
|
-
- 0
|
118
|
-
hash: 868732793501301304
|
120
|
+
version: 2.0.0
|
119
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
-
none: false
|
121
122
|
requirements:
|
122
|
-
- -
|
123
|
+
- - ">="
|
123
124
|
- !ruby/object:Gem::Version
|
124
125
|
version: '0'
|
125
|
-
segments:
|
126
|
-
- 0
|
127
|
-
hash: 868732793501301304
|
128
126
|
requirements: []
|
129
127
|
rubyforge_project:
|
130
|
-
rubygems_version:
|
128
|
+
rubygems_version: 2.7.6
|
131
129
|
signing_key:
|
132
|
-
specification_version:
|
130
|
+
specification_version: 4
|
133
131
|
summary: Builds XML and JSON representations of your Ruby objects
|
134
132
|
test_files:
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
- spec/representative/xml_spec.rb
|
135
135
|
- spec/representative/json_spec.rb
|
136
|
+
- spec/representative/xml_behaviour.rb
|
136
137
|
- spec/representative/nokogiri_spec.rb
|
137
138
|
- spec/representative/tilt_integration_spec.rb
|
138
|
-
- spec/representative/xml_behaviour.rb
|
139
|
-
- spec/representative/xml_spec.rb
|
140
|
-
- spec/spec_helper.rb
|
141
139
|
- Rakefile
|