representable 3.3.0 → 3.4.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.
- checksums.yaml +4 -4
- data/CHANGES.md +10 -0
- data/Gemfile +0 -1
- data/README.md +1 -7
- data/lib/representable/json.rb +11 -14
- data/lib/representable/version.rb +1 -1
- data/representable.gemspec +1 -2
- metadata +3 -65
- data/test/as_test.rb +0 -60
- data/test/benchmarking.rb +0 -78
- data/test/binding_test.rb +0 -54
- data/test/cached_test.rb +0 -177
- data/test/class_test.rb +0 -132
- data/test/coercion_test.rb +0 -73
- data/test/config/inherit_test.rb +0 -130
- data/test/config_test.rb +0 -120
- data/test/decorator_scope_test.rb +0 -30
- data/test/decorator_test.rb +0 -109
- data/test/default_test.rb +0 -34
- data/test/defaults_options_test.rb +0 -121
- data/test/definition_test.rb +0 -253
- data/test/examples/example.rb +0 -314
- data/test/examples/object.rb +0 -28
- data/test/exec_context_test.rb +0 -88
- data/test/features_test.rb +0 -89
- data/test/filter_test.rb +0 -66
- data/test/for_collection_test.rb +0 -71
- data/test/generic_test.rb +0 -109
- data/test/getter_setter_test.rb +0 -26
- data/test/hash_bindings_test.rb +0 -96
- data/test/hash_test.rb +0 -197
- data/test/heritage_test.rb +0 -64
- data/test/if_test.rb +0 -83
- data/test/include_exclude_test.rb +0 -96
- data/test/inherit_test.rb +0 -246
- data/test/inline_test.rb +0 -281
- data/test/instance_test.rb +0 -289
- data/test/is_representable_test.rb +0 -79
- data/test/json_test.rb +0 -379
- data/test/lonely_test.rb +0 -241
- data/test/nested_test.rb +0 -112
- data/test/object_test.rb +0 -60
- data/test/option_test.rb +0 -38
- data/test/parse_pipeline_test.rb +0 -68
- data/test/pipeline_test.rb +0 -309
- data/test/populator_test.rb +0 -108
- data/test/prepare_test.rb +0 -64
- data/test/private_options_test.rb +0 -19
- data/test/reader_writer_test.rb +0 -19
- data/test/realistic_benchmark.rb +0 -96
- data/test/render_nil_test.rb +0 -21
- data/test/represent_test.rb +0 -84
- data/test/representable_test.rb +0 -526
- data/test/schema_test.rb +0 -175
- data/test/serialize_deserialize_test.rb +0 -33
- data/test/skip_test.rb +0 -92
- data/test/stringify_hash_test.rb +0 -39
- data/test/test_helper.rb +0 -153
- data/test/test_helper_test.rb +0 -25
- data/test/uncategorized_test.rb +0 -72
- data/test/user_options_test.rb +0 -24
- data/test/wrap_test.rb +0 -162
- data/test/xml_assertions.rb +0 -29
- data/test/xml_bindings_test.rb +0 -55
- data/test/xml_namespace_test.rb +0 -193
- data/test/xml_test.rb +0 -584
- data/test/yaml_test.rb +0 -209
data/test/hash_test.rb
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class HashPublicMethodsTest < Minitest::Spec
|
|
4
|
-
#---
|
|
5
|
-
# from_hash
|
|
6
|
-
class BandRepresenter < Representable::Decorator
|
|
7
|
-
include Representable::Hash
|
|
8
|
-
property :id
|
|
9
|
-
property :name
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
let(:data) { {"id" => 1, "name" => "Rancid"} }
|
|
13
|
-
|
|
14
|
-
it { _(BandRepresenter.new(Band.new).from_hash(data)[:id, :name]).must_equal [1, "Rancid"] }
|
|
15
|
-
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal [1, "Rancid"] }
|
|
16
|
-
|
|
17
|
-
#---
|
|
18
|
-
# to_hash
|
|
19
|
-
let(:band) { Band.new(1, "Rancid") }
|
|
20
|
-
|
|
21
|
-
it { _(BandRepresenter.new(band).to_hash).must_equal data }
|
|
22
|
-
it { _(BandRepresenter.new(band).render).must_equal data }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
class HashWithScalarPropertyTest < Minitest::Spec
|
|
26
|
-
Album = Struct.new(:title)
|
|
27
|
-
|
|
28
|
-
representer! do
|
|
29
|
-
property :title
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
let(:album) { Album.new("Liar") }
|
|
33
|
-
|
|
34
|
-
describe "#to_hash" do
|
|
35
|
-
it "renders plain property" do
|
|
36
|
-
_(album.extend(representer).to_hash).must_equal("title" => "Liar")
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
describe "#from_hash" do
|
|
41
|
-
it "parses plain property" do
|
|
42
|
-
_(album.extend(representer).from_hash("title" => "This Song Is Recycled").title).must_equal "This Song Is Recycled"
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# Fixes issue #115
|
|
46
|
-
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
|
47
|
-
album = Album.new
|
|
48
|
-
_(album.title).must_be_nil
|
|
49
|
-
album.extend(representer).from_hash("title" => nil)
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
class HashWithTypedPropertyTest < Minitest::Spec
|
|
55
|
-
Album = Struct.new(:best_song)
|
|
56
|
-
|
|
57
|
-
representer! do
|
|
58
|
-
property :best_song, :class => Song do
|
|
59
|
-
property :name
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
let(:album) { Album.new(Song.new("Liar")) }
|
|
64
|
-
|
|
65
|
-
describe "#to_hash" do
|
|
66
|
-
it "renders embedded typed property" do
|
|
67
|
-
_(album.extend(representer).to_hash).must_equal("best_song" => {"name" => "Liar"})
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
describe "#from_hash" do
|
|
72
|
-
it "parses embedded typed property" do
|
|
73
|
-
album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
|
|
74
|
-
_(album.best_song.name).must_equal "Go With Me"
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# nested nil removes nested object.
|
|
78
|
-
it do
|
|
79
|
-
album = Album.new(Song.new("Pre-medicated Murder"))
|
|
80
|
-
album.extend(representer).from_hash("best_song" => nil)
|
|
81
|
-
_(album.best_song).must_be_nil
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
# nested blank hash creates blank object when not populated.
|
|
85
|
-
it do
|
|
86
|
-
album = Album.new # (Song.new("Pre-medicated Murder"))
|
|
87
|
-
album.extend(representer).from_hash("best_song" => {})
|
|
88
|
-
_(album.best_song.name).must_be_nil
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# Fixes issue #115
|
|
92
|
-
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
|
93
|
-
album = Album.new
|
|
94
|
-
album.extend(representer).from_hash("best_song" => nil)
|
|
95
|
-
_(album.best_song).must_be_nil
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
# TODO: move to AsTest.
|
|
101
|
-
class HashWithTypedPropertyAndAs < Minitest::Spec
|
|
102
|
-
representer! do
|
|
103
|
-
property :song, :class => Song, :as => :hit do
|
|
104
|
-
property :name
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }
|
|
109
|
-
|
|
110
|
-
it { _(album.to_hash).must_equal("hit" => {"name" => "Liar"}) }
|
|
111
|
-
it { _(album.from_hash("hit" => {"name" => "Go With Me"})).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
|
112
|
-
end
|
|
113
|
-
# # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
|
|
114
|
-
# # hash_song = Module.new do
|
|
115
|
-
# # include Representable::XML
|
|
116
|
-
# # self.representation_wrap = :song
|
|
117
|
-
# # property :name
|
|
118
|
-
# # end
|
|
119
|
-
|
|
120
|
-
# # let(:hash_album) { Module.new do
|
|
121
|
-
# # include Representable::XML
|
|
122
|
-
# # self.representation_wrap = :album
|
|
123
|
-
# # property :song, :extend => hash_song, :class => Song, :as => :hit
|
|
124
|
-
# # end }
|
|
125
|
-
|
|
126
|
-
# # let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(hash_album) }
|
|
127
|
-
|
|
128
|
-
# # it { album.to_xml.must_equal_xml("<album><hit><name>Liar</name></hit></album>") }
|
|
129
|
-
# # #it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
|
130
|
-
# # end
|
|
131
|
-
# end
|
|
132
|
-
|
|
133
|
-
class HashWithTypedCollectionTest < Minitest::Spec
|
|
134
|
-
Album = Struct.new(:songs)
|
|
135
|
-
|
|
136
|
-
representer! do
|
|
137
|
-
collection :songs, class: Song do
|
|
138
|
-
property :name
|
|
139
|
-
property :track
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
|
|
144
|
-
|
|
145
|
-
describe "#to_hash" do
|
|
146
|
-
it "renders collection of typed property" do
|
|
147
|
-
_(album.extend(representer).to_hash).must_equal(
|
|
148
|
-
"songs" => [
|
|
149
|
-
{"name" => "Liar", "track" => 1},
|
|
150
|
-
{"name" => "What I Know", "track" => 2}
|
|
151
|
-
]
|
|
152
|
-
)
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
describe "#from_hash" do
|
|
157
|
-
it "parses collection of typed property" do
|
|
158
|
-
_(
|
|
159
|
-
album.extend(representer).from_hash(
|
|
160
|
-
"songs" => [
|
|
161
|
-
{"name" => "One Shot Deal", "track" => 4},
|
|
162
|
-
{
|
|
163
|
-
"name" => "Three Way Dance",
|
|
164
|
-
"track" => 5
|
|
165
|
-
}
|
|
166
|
-
]
|
|
167
|
-
)
|
|
168
|
-
).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
class HashWithScalarCollectionTest < Minitest::Spec
|
|
174
|
-
Album = Struct.new(:songs)
|
|
175
|
-
representer! { collection :songs }
|
|
176
|
-
|
|
177
|
-
let(:album) { Album.new(["Jackhammer", "Terrible Man"]) }
|
|
178
|
-
|
|
179
|
-
describe "#to_hash" do
|
|
180
|
-
it "renders a block style list per default" do
|
|
181
|
-
_(album.extend(representer).to_hash).must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
describe "#from_hash" do
|
|
186
|
-
it "parses a block style list" do
|
|
187
|
-
_(
|
|
188
|
-
album.extend(representer).from_hash(
|
|
189
|
-
"songs" => [
|
|
190
|
-
"Off Key Melody",
|
|
191
|
-
"Sinking"
|
|
192
|
-
]
|
|
193
|
-
)
|
|
194
|
-
).must_equal Album.new(["Off Key Melody", "Sinking"])
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
end
|
data/test/heritage_test.rb
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class HeritageTest < Minitest::Spec
|
|
4
|
-
module Hello
|
|
5
|
-
def hello
|
|
6
|
-
"Hello!"
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
module Ciao
|
|
11
|
-
def ciao
|
|
12
|
-
"Ciao!"
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
class A < Representable::Decorator
|
|
17
|
-
include Representable::Hash
|
|
18
|
-
|
|
19
|
-
feature Hello
|
|
20
|
-
|
|
21
|
-
property :id do
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
class B < A
|
|
26
|
-
feature Ciao # does NOT extend id, of course.
|
|
27
|
-
|
|
28
|
-
property :id, inherit: true do
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
class C < A
|
|
33
|
-
property :id do end # overwrite old :id.
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it "B must inherit Hello! feature from A" do
|
|
37
|
-
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it "B must have Ciao from module (feauture) Ciao" do
|
|
41
|
-
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).ciao).must_equal "Ciao!"
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it "C must inherit Hello! feature from A" do
|
|
45
|
-
_(C.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
module M
|
|
49
|
-
include Representable
|
|
50
|
-
feature Hello
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
module N
|
|
54
|
-
include Representable
|
|
55
|
-
include M
|
|
56
|
-
feature Ciao
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
let(:obj_extending_N) { Object.new.extend(N) }
|
|
60
|
-
|
|
61
|
-
it "obj should inherit from N, and N from M" do
|
|
62
|
-
_(obj_extending_N.hello).must_equal "Hello!"
|
|
63
|
-
end
|
|
64
|
-
end
|
data/test/if_test.rb
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class IfTest < Minitest::Spec
|
|
4
|
-
let(:band_class) do
|
|
5
|
-
Class.new do
|
|
6
|
-
include Representable::Hash
|
|
7
|
-
attr_accessor :fame
|
|
8
|
-
|
|
9
|
-
self
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
it "respects property when condition true" do
|
|
14
|
-
band_class.class_eval { property :fame, :if => ->(*) { true } }
|
|
15
|
-
band = band_class.new
|
|
16
|
-
band.from_hash({"fame"=>"oh yes"})
|
|
17
|
-
assert_equal "oh yes", band.fame
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it "ignores property when condition false" do
|
|
21
|
-
band_class.class_eval { property :fame, :if => ->(*) { false } }
|
|
22
|
-
band = band_class.new
|
|
23
|
-
band.from_hash({"fame"=>"oh yes"})
|
|
24
|
-
assert_nil band.fame
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "ignores property when :exclude'ed even when condition is true" do
|
|
28
|
-
band_class.class_eval { property :fame, :if => ->(*) { true } }
|
|
29
|
-
band = band_class.new
|
|
30
|
-
band.from_hash({"fame"=>"oh yes"}, {:exclude => [:fame]})
|
|
31
|
-
assert_nil band.fame
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it "executes block in instance context" do
|
|
35
|
-
band_class.class_eval { property :fame, :if => ->(*) { groupies }; attr_accessor :groupies }
|
|
36
|
-
band = band_class.new
|
|
37
|
-
band.groupies = true
|
|
38
|
-
band.from_hash({"fame"=>"oh yes"})
|
|
39
|
-
assert_equal "oh yes", band.fame
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
describe "executing :if lambda in represented instance context" do
|
|
43
|
-
representer! do
|
|
44
|
-
property :label, :if => ->(*) { signed_contract }
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
subject { OpenStruct.new(:signed_contract => false, :label => "Fat") }
|
|
48
|
-
|
|
49
|
-
it "skips when false" do
|
|
50
|
-
_(subject.extend(representer).to_hash).must_equal({})
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
it "represents when true" do
|
|
54
|
-
subject.signed_contract = true
|
|
55
|
-
_(subject.extend(representer).to_hash).must_equal({"label"=>"Fat"})
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it "works with decorator" do
|
|
59
|
-
rpr = representer
|
|
60
|
-
_(
|
|
61
|
-
Class.new(Representable::Decorator) {
|
|
62
|
-
include Representable::Hash
|
|
63
|
-
include rpr
|
|
64
|
-
}.new(subject).to_hash
|
|
65
|
-
).must_equal({})
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
describe "propagating user options to the block" do
|
|
70
|
-
representer! do
|
|
71
|
-
property :name, :if => ->(opts) { opts[:user_options][:include_name] }
|
|
72
|
-
end
|
|
73
|
-
subject { OpenStruct.new(:name => "Outbound").extend(representer) }
|
|
74
|
-
|
|
75
|
-
it "works without specifying options" do
|
|
76
|
-
_(subject.to_hash).must_equal({})
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it "passes user options to block" do
|
|
80
|
-
_(subject.to_hash(user_options: {include_name: true})).must_equal({"name" => "Outbound"})
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
end
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class IncludeExcludeTest < Minitest::Spec
|
|
4
|
-
Song = Struct.new(:title, :artist, :id)
|
|
5
|
-
Artist = Struct.new(:name, :id, :songs)
|
|
6
|
-
|
|
7
|
-
representer!(decorator: true) do
|
|
8
|
-
property :title
|
|
9
|
-
property :artist, class: Artist do
|
|
10
|
-
property :name
|
|
11
|
-
property :id
|
|
12
|
-
|
|
13
|
-
collection :songs, class: Song do
|
|
14
|
-
property :title
|
|
15
|
-
property :id
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
let(:song) { Song.new("Listless", Artist.new("7yearsbadluck", 1)) }
|
|
21
|
-
let(:decorator) { representer.new(song) }
|
|
22
|
-
|
|
23
|
-
describe "#from_hash" do
|
|
24
|
-
it "accepts :exclude option" do
|
|
25
|
-
decorator.from_hash({"title" => "Don't Smile In Trouble", "artist" => {"id"=>2}}, exclude: [:title])
|
|
26
|
-
|
|
27
|
-
_(song.title).must_equal "Listless"
|
|
28
|
-
_(song.artist).must_equal Artist.new(nil, 2)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it "accepts :include option" do
|
|
32
|
-
decorator.from_hash({"title" => "Don't Smile In Trouble", "artist" => {"id"=>2}}, include: [:title])
|
|
33
|
-
|
|
34
|
-
_(song.title).must_equal "Don't Smile In Trouble"
|
|
35
|
-
_(song.artist).must_equal Artist.new("7yearsbadluck", 1)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it "accepts nested :exclude/:include option" do
|
|
39
|
-
decorator.from_hash(
|
|
40
|
-
{
|
|
41
|
-
"title" => "Don't Smile In Trouble",
|
|
42
|
-
"artist" => {"name" => "Foo", "id" => 2, "songs" => [{"id" => 1, "title" => "Listless"}]}
|
|
43
|
-
},
|
|
44
|
-
exclude: [:title],
|
|
45
|
-
artist: {
|
|
46
|
-
exclude: [:id],
|
|
47
|
-
songs: {include: [:title]}
|
|
48
|
-
}
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
_(song.title).must_equal "Listless"
|
|
52
|
-
_(song.artist).must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
describe "#to_hash" do
|
|
57
|
-
it "accepts :exclude option" do
|
|
58
|
-
_(decorator.to_hash(exclude: [:title])).must_equal({"artist"=>{"name" => "7yearsbadluck", "id" => 1}})
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it "accepts :include option" do
|
|
62
|
-
_(decorator.to_hash(include: [:title])).must_equal({"title"=>"Listless"})
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "accepts nested :exclude/:include option" do
|
|
66
|
-
decorator = representer.new(Song.new("Listless", Artist.new("7yearsbadluck", 1, [Song.new("C.O.A.B.I.E.T.L.")])))
|
|
67
|
-
|
|
68
|
-
_(
|
|
69
|
-
decorator.to_hash(
|
|
70
|
-
exclude: [:title],
|
|
71
|
-
artist: {
|
|
72
|
-
exclude: [:id],
|
|
73
|
-
songs: {include: [:title]}
|
|
74
|
-
}
|
|
75
|
-
)
|
|
76
|
-
).must_equal({"artist"=>{"name" => "7yearsbadluck", "songs" => [{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it "xdoes not propagate private options to nested objects" do
|
|
81
|
-
Cover = Struct.new(:title, :original)
|
|
82
|
-
|
|
83
|
-
cover_rpr = Module.new do
|
|
84
|
-
include Representable::Hash
|
|
85
|
-
property :title
|
|
86
|
-
property :original, extend: self
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# FIXME: we should test all representable-options (:include, :exclude, ?)
|
|
90
|
-
|
|
91
|
-
_(
|
|
92
|
-
Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr)
|
|
93
|
-
.to_hash(:include => [:original])
|
|
94
|
-
).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
|
95
|
-
end
|
|
96
|
-
end
|
data/test/inherit_test.rb
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class InheritTest < Minitest::Spec
|
|
4
|
-
# it's important to have a global module so we can test if stuff gets overridden in the original module.
|
|
5
|
-
module SongRepresenter
|
|
6
|
-
include Representable::Hash
|
|
7
|
-
property :name, :as => :title do
|
|
8
|
-
property :string, :as => :str
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
property :track, :as => :no
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
let(:song) { Song.new(Struct.new(:string).new("Roxanne"), 1) }
|
|
15
|
-
|
|
16
|
-
describe ":inherit plain property" do
|
|
17
|
-
representer! do
|
|
18
|
-
include SongRepresenter
|
|
19
|
-
|
|
20
|
-
property :track, :inherit => true, :getter => ->(*) { "n/a" }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it { _(SongRepresenter.prepare(song).to_hash).must_equal({"title" => {"str"=>"Roxanne"}, "no" => 1}) }
|
|
24
|
-
it { _(representer.prepare(song).to_hash).must_equal({"title" => {"str"=>"Roxanne"}, "no" => "n/a"}) } # as: inherited.
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
describe ":inherit with empty inline representer" do
|
|
28
|
-
representer! do
|
|
29
|
-
include SongRepresenter
|
|
30
|
-
|
|
31
|
-
property :name, :inherit => true do # inherit as: title
|
|
32
|
-
# that doesn't make sense.
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it {
|
|
37
|
-
_(
|
|
38
|
-
SongRepresenter.prepare(
|
|
39
|
-
Song.new(
|
|
40
|
-
Struct.new(:string).new("Believe It"),
|
|
41
|
-
1
|
|
42
|
-
)
|
|
43
|
-
).to_hash
|
|
44
|
-
).must_equal({"title" => {"str"=>"Believe It"}, "no" => 1})
|
|
45
|
-
}
|
|
46
|
-
# the block doesn't override the inline representer.
|
|
47
|
-
it {
|
|
48
|
-
_(
|
|
49
|
-
representer.prepare(
|
|
50
|
-
Song.new(
|
|
51
|
-
Struct.new(:string).new("Believe It"),
|
|
52
|
-
1
|
|
53
|
-
)
|
|
54
|
-
).to_hash
|
|
55
|
-
).must_equal({"title" => {"str"=>"Believe It"}, "no" => 1})
|
|
56
|
-
}
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
describe ":inherit with overriding inline representer" do
|
|
60
|
-
representer! do
|
|
61
|
-
include SongRepresenter
|
|
62
|
-
|
|
63
|
-
# passing block
|
|
64
|
-
property :name, :inherit => true do # inherit as: title
|
|
65
|
-
property :string, :as => :s
|
|
66
|
-
property :length
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
it {
|
|
71
|
-
_(
|
|
72
|
-
representer.prepare(
|
|
73
|
-
Song.new(
|
|
74
|
-
Struct.new(:string, :length).new("Believe It", 10),
|
|
75
|
-
1
|
|
76
|
-
)
|
|
77
|
-
).to_hash
|
|
78
|
-
).must_equal({"title" => {"s" => "Believe It", "length" => 10}, "no" => 1})
|
|
79
|
-
}
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
describe ":inherit with empty inline and options" do
|
|
83
|
-
representer! do
|
|
84
|
-
include SongRepresenter
|
|
85
|
-
|
|
86
|
-
property :name, inherit: true, as: :name do # inherit module, only.
|
|
87
|
-
# that doesn't make sense. but it should simply inherit the old nested properties.
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
it {
|
|
92
|
-
_(
|
|
93
|
-
SongRepresenter.prepare(
|
|
94
|
-
Song.new(
|
|
95
|
-
Struct.new(:string).new("Believe It"),
|
|
96
|
-
1
|
|
97
|
-
)
|
|
98
|
-
).to_hash
|
|
99
|
-
).must_equal({"title" => {"str"=>"Believe It"}, "no" => 1})
|
|
100
|
-
}
|
|
101
|
-
it {
|
|
102
|
-
_(
|
|
103
|
-
representer.prepare(
|
|
104
|
-
Song.new(
|
|
105
|
-
Struct.new(:string).new("Believe It"),
|
|
106
|
-
1
|
|
107
|
-
)
|
|
108
|
-
).to_hash
|
|
109
|
-
).must_equal({"name" => {"str"=>"Believe It"}, "no" => 1})
|
|
110
|
-
}
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
describe ":inherit with inline without block but options" do
|
|
114
|
-
representer! do
|
|
115
|
-
include SongRepresenter
|
|
116
|
-
|
|
117
|
-
property :name, :inherit => true, :as => :name # FIXME: add :getter or something else dynamic since this is double-wrapped.
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
it {
|
|
121
|
-
_(
|
|
122
|
-
SongRepresenter.prepare(
|
|
123
|
-
Song.new(
|
|
124
|
-
Struct.new(:string).new("Believe It"),
|
|
125
|
-
1
|
|
126
|
-
)
|
|
127
|
-
).to_hash
|
|
128
|
-
).must_equal({"title" => {"str"=>"Believe It"}, "no" => 1})
|
|
129
|
-
}
|
|
130
|
-
it {
|
|
131
|
-
_(
|
|
132
|
-
representer.prepare(
|
|
133
|
-
Song.new(
|
|
134
|
-
Struct.new(:string).new("Believe It"),
|
|
135
|
-
1
|
|
136
|
-
)
|
|
137
|
-
).to_hash
|
|
138
|
-
).must_equal({"name" => {"str"=>"Believe It"}, "no" => 1})
|
|
139
|
-
}
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# no :inherit
|
|
143
|
-
describe "overwriting without :inherit" do
|
|
144
|
-
representer! do
|
|
145
|
-
include SongRepresenter
|
|
146
|
-
|
|
147
|
-
property :track, :representable => true
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
it "replaces inherited property" do
|
|
151
|
-
_(representer.representable_attrs.size).must_equal 2
|
|
152
|
-
|
|
153
|
-
definition = representer.representable_attrs.get(:track) # TODO: find a better way to assert Definition identity.
|
|
154
|
-
# definition.keys.size.must_equal 2
|
|
155
|
-
_(definition[:representable]).must_equal true
|
|
156
|
-
_(definition.name).must_equal "track" # was "no".
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
# decorator
|
|
161
|
-
describe ":inherit with decorator" do
|
|
162
|
-
representer!(:decorator => true) do
|
|
163
|
-
property :hit do
|
|
164
|
-
property :title, exec_context: :decorator
|
|
165
|
-
|
|
166
|
-
def title
|
|
167
|
-
"Cheap Transistor Radio"
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
let(:inheriting) do
|
|
173
|
-
class InheritingDecorator < representer
|
|
174
|
-
include Representable::Debug
|
|
175
|
-
property :hit, :inherit => true do
|
|
176
|
-
include Representable::Debug
|
|
177
|
-
property :length
|
|
178
|
-
end
|
|
179
|
-
self
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
it {
|
|
184
|
-
_(
|
|
185
|
-
representer.new(
|
|
186
|
-
OpenStruct.new(
|
|
187
|
-
hit: OpenStruct.new(
|
|
188
|
-
title: "I WILL BE OVERRIDDEN",
|
|
189
|
-
:length => "2:59"
|
|
190
|
-
)
|
|
191
|
-
)
|
|
192
|
-
).to_hash
|
|
193
|
-
).must_equal(
|
|
194
|
-
{"hit"=>{"title"=>"Cheap Transistor Radio"}}
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
# inheriting decorator inherits inline representer class (InlineRepresenter#title).
|
|
199
|
-
# inheriting decorator adds :length.
|
|
200
|
-
it {
|
|
201
|
-
_(
|
|
202
|
-
inheriting.new(
|
|
203
|
-
OpenStruct.new(
|
|
204
|
-
:hit => OpenStruct.new(
|
|
205
|
-
:title => "Hole In Your Soul",
|
|
206
|
-
:length => "2:59"
|
|
207
|
-
)
|
|
208
|
-
)
|
|
209
|
-
).to_hash
|
|
210
|
-
).must_equal(
|
|
211
|
-
{
|
|
212
|
-
"hit" => {
|
|
213
|
-
"title" => "Cheap Transistor Radio", "length" => "2:59"
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
)
|
|
217
|
-
}
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
# :inherit when property doesn't exist, yet.
|
|
221
|
-
describe ":inherit without inheritable property" do
|
|
222
|
-
representer! do
|
|
223
|
-
property :name, :inherit => true
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
it { _(representer.prepare(Song.new("The Beginning")).to_hash).must_equal({"name"=>"The Beginning"}) }
|
|
227
|
-
end
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
# class InheritancingTest < Minitest::Spec
|
|
231
|
-
# class SongDecorator < Representable::Decorator
|
|
232
|
-
# include Representable::Hash
|
|
233
|
-
# property :album do
|
|
234
|
-
# # does have Hash.
|
|
235
|
-
# property :title
|
|
236
|
-
# end
|
|
237
|
-
# end
|
|
238
|
-
|
|
239
|
-
# class JsonSongDecorator < SongDecorator
|
|
240
|
-
# include Representable::XML
|
|
241
|
-
# end
|
|
242
|
-
|
|
243
|
-
# it do
|
|
244
|
-
# puts JsonSongDecorator.new(OpenStruct.new(:album => OpenStruct.new(:title => "Erotic Cakes", :tracks => nil))).to_xml
|
|
245
|
-
# end
|
|
246
|
-
# end
|