representable 1.0.1 → 1.1.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.
- data/CHANGES.textile +10 -0
- data/README.rdoc +34 -0
- data/lib/representable.rb +5 -0
- data/lib/representable/binding.rb +12 -15
- data/lib/representable/bindings/json_bindings.rb +54 -34
- data/lib/representable/bindings/xml_bindings.rb +96 -59
- data/lib/representable/definition.rb +30 -30
- data/lib/representable/json.rb +3 -2
- data/lib/representable/json/collection.rb +38 -0
- data/lib/representable/json/hash.rb +38 -0
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml.rb +4 -3
- data/lib/representable/xml/collection.rb +38 -0
- data/lib/representable/xml/hash.rb +38 -0
- data/test/definition_test.rb +23 -24
- data/test/json_bindings_test.rb +119 -0
- data/test/json_test.rb +127 -3
- data/test/polymorphic_test.rb +45 -0
- data/test/representable_test.rb +0 -1
- data/test/test_helper.rb +4 -0
- data/test/xml_bindings_test.rb +153 -0
- data/test/xml_test.rb +10 -5
- metadata +109 -73
- data/test/bindings_test.rb +0 -16
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PolymorphicExtender
|
4
|
+
def self.extended(model)
|
5
|
+
model.extend(representer_name_for(model))
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.representer_name_for(model)
|
9
|
+
PolymorphicTest.const_get("#{model.class.to_s.split("::").last}Representer")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class PolymorphicTest < MiniTest::Spec
|
15
|
+
class PopSong < Song
|
16
|
+
end
|
17
|
+
|
18
|
+
module SongRepresenter
|
19
|
+
include Representable::JSON
|
20
|
+
property :name
|
21
|
+
end
|
22
|
+
|
23
|
+
module PopSongRepresenter
|
24
|
+
include Representable::JSON
|
25
|
+
property :name, :from => "known_as"
|
26
|
+
end
|
27
|
+
|
28
|
+
class Album
|
29
|
+
attr_accessor :songs
|
30
|
+
end
|
31
|
+
|
32
|
+
module AlbumRepresenter
|
33
|
+
include Representable::JSON
|
34
|
+
collection :songs, :extend => PolymorphicExtender
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
describe "PolymorphicExtender" do
|
39
|
+
it "extends each model with the correct representer in #to_json" do
|
40
|
+
album = Album.new
|
41
|
+
album.songs = [PopSong.new("Let Me Down"), Song.new("The 4 Horsemen")]
|
42
|
+
assert_equal "{\"songs\":[{\"known_as\":\"Let Me Down\"},{\"name\":\"The 4 Horsemen\"}]}", album.extend(AlbumRepresenter).to_json
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/test/representable_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'representable/json' # FIXME.
|
3
|
+
require 'representable/xml/collection'
|
4
|
+
require 'representable/xml/hash'
|
5
|
+
|
6
|
+
require 'representable/xml'
|
7
|
+
|
8
|
+
class XMLBindingTest < MiniTest::Spec
|
9
|
+
module SongRepresenter
|
10
|
+
include Representable::XML
|
11
|
+
property :name
|
12
|
+
self.representation_wrap = :song
|
13
|
+
end
|
14
|
+
|
15
|
+
class SongWithRepresenter < ::Song
|
16
|
+
include Representable
|
17
|
+
include SongRepresenter
|
18
|
+
self.representation_wrap = :song
|
19
|
+
end
|
20
|
+
|
21
|
+
before do
|
22
|
+
@doc = Nokogiri::XML::Document.new
|
23
|
+
@song = SongWithRepresenter.new("Thinning the Herd")
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "PropertyBinding" do
|
27
|
+
describe "with plain text" do
|
28
|
+
before do
|
29
|
+
@property = Representable::XML::PropertyBinding.new(Representable::Definition.new(:song))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "extracts with #read" do
|
33
|
+
assert_equal "Thinning the Herd", @property.read(Nokogiri::XML("<song>Thinning the Herd</song>"))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "inserts with #write" do
|
37
|
+
@property.write(@doc, "Thinning the Herd")
|
38
|
+
assert_xml_equal "<song>Thinning the Herd</song>", @doc.to_s
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "with an object" do
|
43
|
+
before do
|
44
|
+
@property = Representable::XML::PropertyBinding.new(Representable::Definition.new(:song, :class => SongWithRepresenter))
|
45
|
+
end
|
46
|
+
|
47
|
+
it "extracts with #read" do
|
48
|
+
assert_equal @song, @property.read(Nokogiri::XML("<song><name>Thinning the Herd</name></song>"))
|
49
|
+
end
|
50
|
+
|
51
|
+
it "inserts with #write" do
|
52
|
+
@property.write(@doc, @song)
|
53
|
+
assert_xml_equal("<song><name>Thinning the Herd</name></song>", @doc.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "with an object and :extend" do
|
58
|
+
before do
|
59
|
+
@property = Representable::XML::PropertyBinding.new(Representable::Definition.new(:song, :class => Song, :extend => SongRepresenter))
|
60
|
+
end
|
61
|
+
|
62
|
+
it "extracts with #read" do
|
63
|
+
assert_equal @song, @property.read(Nokogiri::XML("<song><name>Thinning the Herd</name></song>"))
|
64
|
+
end
|
65
|
+
|
66
|
+
it "inserts with #write" do
|
67
|
+
@property.write(@doc, @song)
|
68
|
+
assert_xml_equal("<song><name>Thinning the Herd</name></song>", @doc.to_s)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
describe "CollectionBinding" do
|
75
|
+
describe "with plain text items" do
|
76
|
+
before do
|
77
|
+
@property = Representable::XML::CollectionBinding.new(Representable::Definition.new(:song, :collection => true))
|
78
|
+
end
|
79
|
+
|
80
|
+
it "extracts with #read" do
|
81
|
+
assert_equal ["The Gargoyle", "Bronx"], @property.read(Nokogiri::XML("<doc><song>The Gargoyle</song><song>Bronx</song></doc>").root)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "inserts with #write" do
|
85
|
+
parent = Nokogiri::XML::Node.new("parent", @doc)
|
86
|
+
@property.write(parent, ["The Gargoyle", "Bronx"])
|
87
|
+
assert_xml_equal("<songs><song>The Gargoyle</song><song>Bronx</song></songs>", parent.to_s)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "with objects" do
|
92
|
+
before do
|
93
|
+
@property = Representable::XML::PropertyBinding.new(Representable::Definition.new(:song, :collection => true, :class => SongWithRepresenter))
|
94
|
+
end
|
95
|
+
|
96
|
+
it "extracts with #read" do
|
97
|
+
assert_equal @song, @property.read(Nokogiri::XML("<song><name>Thinning the Herd</name></song>"))
|
98
|
+
end
|
99
|
+
|
100
|
+
it "inserts with #write" do
|
101
|
+
@property.write(@doc, @song)
|
102
|
+
assert_xml_equal("<song><name>Thinning the Herd</name></song>", @doc.to_s)
|
103
|
+
assert_kind_of Nokogiri::XML::Node, @doc.children.first
|
104
|
+
assert_equal "song", @doc.children.first.name
|
105
|
+
assert_equal "name", @doc.children.first.children.first.name
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
describe "HashBinding" do
|
112
|
+
describe "with plain text items" do
|
113
|
+
before do
|
114
|
+
@property = Representable::XML::HashBinding.new(Representable::Definition.new(:songs, :hash => true))
|
115
|
+
end
|
116
|
+
|
117
|
+
it "extracts with #read" do
|
118
|
+
assert_equal({"first" => "The Gargoyle", "second" => "Bronx"} , @property.read(Nokogiri::XML("<songs><first>The Gargoyle</first><second>Bronx</second></songs>")))
|
119
|
+
end
|
120
|
+
|
121
|
+
it "inserts with #write" do
|
122
|
+
parent = Nokogiri::XML::Node.new("parent", @doc)
|
123
|
+
@property.write(parent, {"first" => "The Gargoyle", "second" => "Bronx"})
|
124
|
+
assert_xml_equal("<songs><first>The Gargoyle</first><second>Bronx</second></songs>", parent.to_s)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "with objects" do
|
129
|
+
before do
|
130
|
+
@property = Representable::XML::HashBinding.new(Representable::Definition.new(:songs, :hash => true, :class => Song, :extend => SongRepresenter))
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
describe "AttributeBinding" do
|
137
|
+
describe "with plain text items" do
|
138
|
+
before do
|
139
|
+
@property = Representable::XML::AttributeBinding.new(Representable::Definition.new(:name, :attribute => true))
|
140
|
+
end
|
141
|
+
|
142
|
+
it "extracts with #read" do
|
143
|
+
assert_equal "The Gargoyle", @property.read(Nokogiri::XML("<song name=\"The Gargoyle\" />").root)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "inserts with #write" do
|
147
|
+
parent = Nokogiri::XML::Node.new("song", @doc)
|
148
|
+
@property.write(parent, "The Gargoyle")
|
149
|
+
assert_xml_equal("<song name=\"The Gargoyle\" />", parent.to_s)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
data/test/xml_test.rb
CHANGED
@@ -127,12 +127,17 @@ class XmlTest < MiniTest::Spec
|
|
127
127
|
assert_kind_of XML::AttributeBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => "band", :attribute => true))
|
128
128
|
end
|
129
129
|
|
130
|
-
it "returns
|
131
|
-
assert_kind_of XML::
|
130
|
+
it "returns PropertyBinding" do
|
131
|
+
assert_kind_of XML::PropertyBinding, Representable::XML.binding_for_definition(Def.new(:band, :class => Hash))
|
132
|
+
assert_kind_of XML::PropertyBinding, Representable::XML.binding_for_definition(Def.new(:band, :from => :content))
|
132
133
|
end
|
133
134
|
|
134
|
-
it "returns
|
135
|
-
assert_kind_of XML::
|
135
|
+
it "returns CollectionBinding" do
|
136
|
+
assert_kind_of XML::CollectionBinding, Representable::XML.binding_for_definition(Def.new(:band, :collection => :true))
|
137
|
+
end
|
138
|
+
|
139
|
+
it "returns HashBinding" do
|
140
|
+
assert_kind_of XML::HashBinding, Representable::XML.binding_for_definition(Def.new(:band, :hash => :true))
|
136
141
|
end
|
137
142
|
end
|
138
143
|
|
@@ -261,7 +266,7 @@ class TypedPropertyTest < MiniTest::Spec
|
|
261
266
|
it "doesn't escape and wrap string from Band#to_node" do
|
262
267
|
band = Band.new("Bad Religion")
|
263
268
|
band.instance_eval do
|
264
|
-
def to_node
|
269
|
+
def to_node(*)
|
265
270
|
"<band>Baaaad Religion</band>"
|
266
271
|
end
|
267
272
|
end
|
metadata
CHANGED
@@ -1,90 +1,112 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: representable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- Nick Sutterer
|
9
13
|
autorequire:
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
|
17
|
+
date: 2012-02-03 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: nokogiri
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
22
31
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
26
34
|
name: json
|
27
|
-
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
37
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
33
44
|
type: :runtime
|
34
|
-
|
35
|
-
|
36
|
-
- !ruby/object:Gem::Dependency
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
37
47
|
name: rake
|
38
|
-
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
44
57
|
type: :development
|
45
|
-
|
46
|
-
|
47
|
-
- !ruby/object:Gem::Dependency
|
58
|
+
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
48
60
|
name: test_xml
|
49
|
-
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
63
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
55
70
|
type: :development
|
56
|
-
|
57
|
-
|
58
|
-
- !ruby/object:Gem::Dependency
|
71
|
+
version_requirements: *id004
|
72
|
+
- !ruby/object:Gem::Dependency
|
59
73
|
name: minitest
|
60
|
-
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
61
76
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 2
|
82
|
+
- 8
|
83
|
+
- 1
|
65
84
|
version: 2.8.1
|
66
85
|
type: :development
|
67
|
-
|
68
|
-
|
69
|
-
- !ruby/object:Gem::Dependency
|
86
|
+
version_requirements: *id005
|
87
|
+
- !ruby/object:Gem::Dependency
|
70
88
|
name: mocha
|
71
|
-
|
89
|
+
prerelease: false
|
90
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
72
91
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
77
98
|
type: :development
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
and JSON support, plain properties, collections and compositions.
|
82
|
-
email:
|
99
|
+
version_requirements: *id006
|
100
|
+
description: Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties, collections and compositions.
|
101
|
+
email:
|
83
102
|
- apotonick@gmail.com
|
84
103
|
executables: []
|
104
|
+
|
85
105
|
extensions: []
|
106
|
+
|
86
107
|
extra_rdoc_files: []
|
87
|
-
|
108
|
+
|
109
|
+
files:
|
88
110
|
- .gitignore
|
89
111
|
- .rspec
|
90
112
|
- .travis.yml
|
@@ -99,38 +121,52 @@ files:
|
|
99
121
|
- lib/representable/bindings/xml_bindings.rb
|
100
122
|
- lib/representable/definition.rb
|
101
123
|
- lib/representable/json.rb
|
124
|
+
- lib/representable/json/collection.rb
|
125
|
+
- lib/representable/json/hash.rb
|
102
126
|
- lib/representable/version.rb
|
103
127
|
- lib/representable/xml.rb
|
128
|
+
- lib/representable/xml/collection.rb
|
129
|
+
- lib/representable/xml/hash.rb
|
104
130
|
- representable.gemspec
|
105
|
-
- test/bindings_test.rb
|
106
131
|
- test/definition_test.rb
|
132
|
+
- test/json_bindings_test.rb
|
107
133
|
- test/json_test.rb
|
134
|
+
- test/polymorphic_test.rb
|
108
135
|
- test/representable_test.rb
|
109
136
|
- test/test_helper.rb
|
137
|
+
- test/xml_bindings_test.rb
|
110
138
|
- test/xml_test.rb
|
139
|
+
has_rdoc: true
|
111
140
|
homepage: http://representable.apotomo.de
|
112
141
|
licenses: []
|
142
|
+
|
113
143
|
post_install_message:
|
114
144
|
rdoc_options: []
|
115
|
-
|
145
|
+
|
146
|
+
require_paths:
|
116
147
|
- lib
|
117
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
149
|
none: false
|
119
|
-
requirements:
|
120
|
-
- -
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
|
123
|
-
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
segments:
|
154
|
+
- 0
|
155
|
+
version: "0"
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
157
|
none: false
|
125
|
-
requirements:
|
126
|
-
- -
|
127
|
-
- !ruby/object:Gem::Version
|
128
|
-
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
segments:
|
162
|
+
- 0
|
163
|
+
version: "0"
|
129
164
|
requirements: []
|
165
|
+
|
130
166
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.
|
167
|
+
rubygems_version: 1.3.7
|
132
168
|
signing_key:
|
133
169
|
specification_version: 3
|
134
|
-
summary: Maps representation documents from and to Ruby objects. Includes XML and
|
135
|
-
JSON support, plain properties, collections and compositions.
|
170
|
+
summary: Maps representation documents from and to Ruby objects. Includes XML and JSON support, plain properties, collections and compositions.
|
136
171
|
test_files: []
|
172
|
+
|