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/inline_test.rb
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class InlineTest < Minitest::Spec
|
|
4
|
-
let(:song) { Song.new("Alive") }
|
|
5
|
-
let(:request) { representer.prepare(OpenStruct.new(:song => song)) }
|
|
6
|
-
|
|
7
|
-
{
|
|
8
|
-
:hash => [Representable::Hash, {"song"=>{"name"=>"Alive"}}, {"song"=>{"name"=>"You've Taken Everything"}}],
|
|
9
|
-
:json => [Representable::JSON, "{\"song\":{\"name\":\"Alive\"}}", "{\"song\":{\"name\":\"You've Taken Everything\"}}"],
|
|
10
|
-
:xml => [
|
|
11
|
-
Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>",
|
|
12
|
-
"<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"
|
|
13
|
-
],
|
|
14
|
-
:yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"]
|
|
15
|
-
}.each do |format, cfg|
|
|
16
|
-
mod, output, input = cfg
|
|
17
|
-
|
|
18
|
-
describe "[#{format}] with :class" do
|
|
19
|
-
representer!(:module => mod) do
|
|
20
|
-
property :song, :class => Song do
|
|
21
|
-
property :name
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
let(:format) { format }
|
|
26
|
-
|
|
27
|
-
it { render(request).must_equal_document output }
|
|
28
|
-
it { _(parse(request, input).song.name).must_equal "You've Taken Everything" }
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
:hash => [Representable::Hash, {"songs"=>[{"name"=>"Alive"}]}, {"songs"=>[{"name"=>"You've Taken Everything"}]}],
|
|
34
|
-
:json => [Representable::JSON, "{\"songs\":[{\"name\":\"Alive\"}]}", "{\"songs\":[{\"name\":\"You've Taken Everything\"}]}"],
|
|
35
|
-
:xml => [
|
|
36
|
-
Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>",
|
|
37
|
-
"<open_struct><song><name>You've Taken Everything</name></song></open_struct>", {:as => :song}
|
|
38
|
-
],
|
|
39
|
-
:yaml => [Representable::YAML, "---\nsongs:\n- name: Alive\n", "---\nsongs:\n- name: You've Taken Everything\n"]
|
|
40
|
-
}.each do |format, cfg|
|
|
41
|
-
mod, output, input, collection_options = cfg
|
|
42
|
-
collection_options ||= {}
|
|
43
|
-
|
|
44
|
-
describe "[#{format}] collection with :class" do
|
|
45
|
-
let(:request) { representer.prepare(OpenStruct.new(:songs => [song])) }
|
|
46
|
-
|
|
47
|
-
representer!(:module => mod) do
|
|
48
|
-
collection :songs, collection_options.merge(:class => Song) do
|
|
49
|
-
property :name
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
let(:format) { format } # FIXME: why do we have to define this?
|
|
54
|
-
|
|
55
|
-
it { render(request).must_equal_document output }
|
|
56
|
-
it { _(parse(request, input).songs.first.name).must_equal "You've Taken Everything" }
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
describe "without :class" do
|
|
61
|
-
representer! do
|
|
62
|
-
property :song do
|
|
63
|
-
property :name
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it { _(request.to_hash).must_equal({"song"=>{"name"=>"Alive"}}) }
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
for_formats(
|
|
71
|
-
:hash => [Representable::Hash, {}]
|
|
72
|
-
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
|
|
73
|
-
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
|
74
|
-
) do |format, mod, input|
|
|
75
|
-
describe "parsing [#{format}] where nested property missing" do
|
|
76
|
-
representer!(:module => mod) do
|
|
77
|
-
property :song do
|
|
78
|
-
property :name
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
it "doesn't change represented object" do
|
|
83
|
-
_(request.send("from_#{format}", input).song).must_equal song
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
describe "inheriting from outer representer" do
|
|
89
|
-
let(:request) { Struct.new(:song, :requester).new(song, "Josephine") }
|
|
90
|
-
|
|
91
|
-
[false, true].each do |is_decorator| # test for module and decorator.
|
|
92
|
-
representer!(:decorator => is_decorator) do
|
|
93
|
-
property :requester
|
|
94
|
-
|
|
95
|
-
property :song, :class => Song do
|
|
96
|
-
property :name
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
let(:decorator) { representer.prepare(request) }
|
|
101
|
-
|
|
102
|
-
it { _(decorator.to_hash).must_equal({"requester" => "Josephine", "song" => {"name"=>"Alive"}}) }
|
|
103
|
-
it { _(decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name).must_equal "You've Taken Everything" }
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
describe "object pollution" do
|
|
108
|
-
representer!(:decorator => true) do
|
|
109
|
-
property :song do
|
|
110
|
-
property :name
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
it "uses an inline decorator and doesn't alter represented" do
|
|
115
|
-
representer.prepare(Struct.new(:song).new(song)).to_hash
|
|
116
|
-
_(song).wont_be_kind_of Representable
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
# TODO: should be in extend:/decorator: test.
|
|
121
|
-
# FIXME: this tests :getter{represented}+:extend - represented gets extended twice and the inline decorator overrides original config.
|
|
122
|
-
# for_formats(
|
|
123
|
-
# :hash => [Representable::Hash, {"album" => {"artist" => {"label"=>"Epitaph"}}}],
|
|
124
|
-
# # :xml => [Representable::XML, "<open_struct></open_struct>"],
|
|
125
|
-
# #:yaml => [Representable::YAML, "---\nlabel:\n label: Epitaph\n owner: Brett Gurewitz\n"]
|
|
126
|
-
# ) do |format, mod, output, input|
|
|
127
|
-
|
|
128
|
-
# module ArtistRepresenter
|
|
129
|
-
# include Representable::JSON
|
|
130
|
-
# property :label
|
|
131
|
-
# end
|
|
132
|
-
|
|
133
|
-
# describe ":getter with inline representer" do
|
|
134
|
-
# let(:format) { format }
|
|
135
|
-
|
|
136
|
-
# representer!(:module => mod) do
|
|
137
|
-
# self.representation_wrap = :album
|
|
138
|
-
|
|
139
|
-
# property :artist, :getter => lambda { |args| represented }, :extend => ArtistRepresenter
|
|
140
|
-
# end
|
|
141
|
-
|
|
142
|
-
# let(:album) { OpenStruct.new(:label => "Epitaph").extend(representer) }
|
|
143
|
-
|
|
144
|
-
# it "renders nested Album-properties in separate section" do
|
|
145
|
-
# render(album).must_equal_document output
|
|
146
|
-
# end
|
|
147
|
-
# end
|
|
148
|
-
# end
|
|
149
|
-
|
|
150
|
-
for_formats(
|
|
151
|
-
{
|
|
152
|
-
:hash => [Representable::Hash, {"album" => {"artist" => {"label"=>"Epitaph"}}}]
|
|
153
|
-
# :xml => [Representable::XML, "<open_struct></open_struct>"],
|
|
154
|
-
# :yaml => [Representable::YAML, "---\nlabel:\n label: Epitaph\n owner: Brett Gurewitz\n"]
|
|
155
|
-
}
|
|
156
|
-
) do |format, mod, output, _input|
|
|
157
|
-
class ArtistDecorator < Representable::Decorator
|
|
158
|
-
include Representable::JSON
|
|
159
|
-
property :label
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
describe ":getter with :decorator" do
|
|
163
|
-
let(:format) { format }
|
|
164
|
-
|
|
165
|
-
representer!(:module => mod) do
|
|
166
|
-
self.representation_wrap = "album"
|
|
167
|
-
|
|
168
|
-
property :artist, :getter => ->(_args) { represented }, :decorator => ArtistDecorator
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
let(:album) { OpenStruct.new(:label => "Epitaph").extend(representer) }
|
|
172
|
-
|
|
173
|
-
it "renders nested Album-properties in separate section" do
|
|
174
|
-
render(album).must_equal_document output
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# test helper methods within inline representer
|
|
180
|
-
for_formats(
|
|
181
|
-
{
|
|
182
|
-
:hash => [Representable::Hash, {"song"=>{"name"=>"ALIVE"}}],
|
|
183
|
-
:xml => [Representable::XML, "<request>\n <song>\n <name>ALIVE</name>\n </song>\n</request>"],
|
|
184
|
-
:yaml => [Representable::YAML, "---\nsong:\n name: ALIVE\n"]
|
|
185
|
-
}
|
|
186
|
-
) do |format, mod, output|
|
|
187
|
-
describe "helper method within inline representer [#{format}]" do
|
|
188
|
-
let(:format) { format }
|
|
189
|
-
|
|
190
|
-
representer!(:module => mod, :decorator => true) do
|
|
191
|
-
self.representation_wrap = :request if format == :xml
|
|
192
|
-
|
|
193
|
-
property :requester
|
|
194
|
-
property :song do
|
|
195
|
-
property :name, :exec_context => :decorator
|
|
196
|
-
|
|
197
|
-
define_method :name do
|
|
198
|
-
represented.name.upcase
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
self.representation_wrap = :song if format == :xml
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
let(:request) { representer.prepare(OpenStruct.new(:song => Song.new("Alive"))) }
|
|
206
|
-
|
|
207
|
-
it do
|
|
208
|
-
render(request).must_equal_document output
|
|
209
|
-
end
|
|
210
|
-
end
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
describe "include module in inline representers" do
|
|
214
|
-
representer! do
|
|
215
|
-
extension = Module.new do
|
|
216
|
-
include Representable::Hash
|
|
217
|
-
property :title
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
property :song do
|
|
221
|
-
include extension
|
|
222
|
-
property :artist
|
|
223
|
-
end
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
it do
|
|
227
|
-
_(
|
|
228
|
-
OpenStruct.new(
|
|
229
|
-
:song => OpenStruct.new(
|
|
230
|
-
:title => "The Fever And The Sound",
|
|
231
|
-
:artist => "Strung Out"
|
|
232
|
-
)
|
|
233
|
-
).extend(representer)
|
|
234
|
-
.to_hash
|
|
235
|
-
)
|
|
236
|
-
.must_equal({"song"=>{"artist" => "Strung Out", "title" => "The Fever And The Sound"}})
|
|
237
|
-
end
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
# define method in inline representer
|
|
241
|
-
describe "define method in inline representer" do
|
|
242
|
-
Mod = Module.new do
|
|
243
|
-
include Representable::Hash
|
|
244
|
-
|
|
245
|
-
def song
|
|
246
|
-
"Object.new"
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
property :song do
|
|
250
|
-
property :duration
|
|
251
|
-
|
|
252
|
-
def duration
|
|
253
|
-
"6:53"
|
|
254
|
-
end
|
|
255
|
-
end
|
|
256
|
-
end
|
|
257
|
-
|
|
258
|
-
it { _(Object.new.extend(Mod).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
# define method inline with Decorator
|
|
262
|
-
describe "define method inline with Decorator" do
|
|
263
|
-
dec = Class.new(Representable::Decorator) do
|
|
264
|
-
include Representable::Hash
|
|
265
|
-
|
|
266
|
-
def song
|
|
267
|
-
"Object.new"
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
property :song, :exec_context => :decorator do
|
|
271
|
-
property :duration, :exec_context => :decorator
|
|
272
|
-
|
|
273
|
-
def duration
|
|
274
|
-
"6:53"
|
|
275
|
-
end
|
|
276
|
-
end
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
it { _(dec.new(Object.new).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
|
280
|
-
end
|
|
281
|
-
end
|
data/test/instance_test.rb
DELETED
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class InstanceTest < BaseTest
|
|
4
|
-
Song = Struct.new(:id, :title)
|
|
5
|
-
Song.class_eval do
|
|
6
|
-
def self.find(id)
|
|
7
|
-
new(id, "Invincible")
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe "lambda { fragment } (new way of class: lambda { nil })" do
|
|
12
|
-
representer! do
|
|
13
|
-
property :title, :instance => ->(options) { options[:fragment] }
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it "skips creating new instance" do
|
|
17
|
-
object = Object.new
|
|
18
|
-
object.instance_eval do
|
|
19
|
-
def from_hash(hash, *_args)
|
|
20
|
-
hash
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
song = OpenStruct.new.extend(representer).from_hash({"title" => object})
|
|
25
|
-
_(song.title).must_equal object
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# TODO: use *args in from_hash.
|
|
30
|
-
# DISCUSS: do we need parse_strategy?
|
|
31
|
-
describe "property with :instance" do
|
|
32
|
-
representer!(:inject => :song_representer) do
|
|
33
|
-
property :song,
|
|
34
|
-
:instance => ->(options) { options[:fragment]["id"] == song.id ? song : Song.find(options[:fragment]["id"]) },
|
|
35
|
-
:extend => song_representer
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it("xxx") {
|
|
39
|
-
_(
|
|
40
|
-
OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer)
|
|
41
|
-
.from_hash("song" => {"id" => 1}).song
|
|
42
|
-
).must_equal Song.new(1, "The Answer Is Still No")
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
it {
|
|
46
|
-
_(
|
|
47
|
-
OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer)
|
|
48
|
-
.from_hash("song" => {"id" => 2}).song
|
|
49
|
-
).must_equal Song.new(2, "Invincible")
|
|
50
|
-
}
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
describe "collection with :instance" do
|
|
54
|
-
representer!(:inject => :song_representer) do
|
|
55
|
-
collection :songs,
|
|
56
|
-
:instance => ->(options) {
|
|
57
|
-
options[:fragment]["id"] == songs[options[:index]].id ? songs[options[:index]] : Song.find(options[:fragment]["id"])
|
|
58
|
-
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
|
59
|
-
|
|
60
|
-
:extend => song_representer
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
it {
|
|
64
|
-
album = Struct.new(:songs).new(
|
|
65
|
-
[
|
|
66
|
-
Song.new(1, "The Answer Is Still No"),
|
|
67
|
-
Song.new(2, "")
|
|
68
|
-
]
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
_(
|
|
72
|
-
album
|
|
73
|
-
.extend(representer)
|
|
74
|
-
.from_hash("songs" => [{"id" => 2}, {"id" => 2, "title" => "The Answer Is Still No"}]).songs
|
|
75
|
-
).must_equal [
|
|
76
|
-
Song.new(2, "Invincible"), Song.new(2, "The Answer Is Still No")
|
|
77
|
-
]
|
|
78
|
-
}
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
describe "property with lambda receiving fragment and args" do
|
|
82
|
-
representer!(:inject => :song_representer) do
|
|
83
|
-
property :song, :instance => ->(options) {
|
|
84
|
-
Struct.new(:args, :id).new([options[:fragment], options[:user_options]])
|
|
85
|
-
}, :extend => song_representer
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
it {
|
|
89
|
-
_(
|
|
90
|
-
OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer)
|
|
91
|
-
.from_hash({"song" => {"id" => 1}}, user_options: {volume: 1}).song.args
|
|
92
|
-
).must_equal([{"id"=>1}, {:volume=>1}])
|
|
93
|
-
}
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
# TODO: raise and test instance:{nil}
|
|
97
|
-
# describe "property with instance: { nil }" do # TODO: introduce :representable option?
|
|
98
|
-
# representer!(:inject => :song_representer) do
|
|
99
|
-
# property :song, :instance => lambda { |*| nil }, :extend => song_representer
|
|
100
|
-
# end
|
|
101
|
-
|
|
102
|
-
# let(:hit) { hit = OpenStruct.new(:song => song).extend(representer) }
|
|
103
|
-
|
|
104
|
-
# it "calls #to_hash on song instance, nothing else" do
|
|
105
|
-
# hit.to_hash.must_equal("song"=>{"title"=>"Resist Stance"})
|
|
106
|
-
# end
|
|
107
|
-
|
|
108
|
-
# it "calls #from_hash on the existing song instance, nothing else" do
|
|
109
|
-
# song_id = hit.song.object_id
|
|
110
|
-
# hit.from_hash("song"=>{"title"=>"Suffer"})
|
|
111
|
-
# hit.song.title.must_equal "Suffer"
|
|
112
|
-
# hit.song.object_id.must_equal song_id
|
|
113
|
-
# end
|
|
114
|
-
# end
|
|
115
|
-
|
|
116
|
-
# lambda { |fragment, i, Context(binding: <..>, args: [..])| }
|
|
117
|
-
|
|
118
|
-
describe "sync" do
|
|
119
|
-
representer!(:inject => :song_representer) do
|
|
120
|
-
collection :songs,
|
|
121
|
-
:instance => ->(options) {
|
|
122
|
-
songs[options[:index]]
|
|
123
|
-
},
|
|
124
|
-
:extend => song_representer,
|
|
125
|
-
# :parse_strategy => :sync
|
|
126
|
-
:setter => ->(*) {}
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
it {
|
|
130
|
-
album = Struct.new(:songs).new(
|
|
131
|
-
songs = [
|
|
132
|
-
Song.new(1, "The Answer Is Still No"),
|
|
133
|
-
Song.new(2, "Invncble")
|
|
134
|
-
]
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
album
|
|
138
|
-
.extend(representer)
|
|
139
|
-
.from_hash("songs" => [{"title" => "The Answer Is Still No"}, {"title" => "Invincible"}])
|
|
140
|
-
|
|
141
|
-
_(album.songs).must_equal [
|
|
142
|
-
Song.new(1, "The Answer Is Still No"),
|
|
143
|
-
Song.new(2, "Invincible")
|
|
144
|
-
]
|
|
145
|
-
|
|
146
|
-
_(songs.object_id).must_equal album.songs.object_id
|
|
147
|
-
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
148
|
-
_(songs[1].object_id).must_equal album.songs[1].object_id
|
|
149
|
-
}
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
describe "update existing elements, only" do
|
|
153
|
-
representer!(:inject => :song_representer) do
|
|
154
|
-
collection :songs,
|
|
155
|
-
:instance => ->(options) {
|
|
156
|
-
# fragment["id"] == songs[i].id ? songs[i] : Song.find(fragment["id"])
|
|
157
|
-
songs.find { |s| s.id == options[:fragment]["id"] }
|
|
158
|
-
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
|
159
|
-
|
|
160
|
-
:extend => song_representer,
|
|
161
|
-
# :parse_strategy => :sync
|
|
162
|
-
:setter => ->(*) {}
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
it("hooray") {
|
|
166
|
-
album = Struct.new(:songs).new(
|
|
167
|
-
songs = [
|
|
168
|
-
Song.new(1, "The Answer Is Still No"),
|
|
169
|
-
Song.new(2, "Invncble")
|
|
170
|
-
]
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
_(
|
|
174
|
-
album
|
|
175
|
-
.extend(representer)
|
|
176
|
-
.from_hash("songs" => [{"id" => 2, "title" => "Invincible"}])
|
|
177
|
-
.songs
|
|
178
|
-
).must_equal [
|
|
179
|
-
Song.new(1, "The Answer Is Still No"),
|
|
180
|
-
Song.new(2, "Invincible")
|
|
181
|
-
]
|
|
182
|
-
# TODO: check elements object_id!
|
|
183
|
-
|
|
184
|
-
_(songs.object_id).must_equal album.songs.object_id
|
|
185
|
-
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
186
|
-
_(songs[1].object_id).must_equal album.songs[1].object_id
|
|
187
|
-
}
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
describe "add incoming elements, only" do
|
|
191
|
-
representer!(:inject => :song_representer) do
|
|
192
|
-
collection :songs,
|
|
193
|
-
:instance => ->(_options) {
|
|
194
|
-
songs << song = Song.new(2)
|
|
195
|
-
song
|
|
196
|
-
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
|
197
|
-
|
|
198
|
-
:extend => song_representer,
|
|
199
|
-
# :parse_strategy => :sync
|
|
200
|
-
:setter => ->(*) {}
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
it {
|
|
204
|
-
album = Struct.new(:songs).new(
|
|
205
|
-
songs = [
|
|
206
|
-
Song.new(1, "The Answer Is Still No")
|
|
207
|
-
]
|
|
208
|
-
)
|
|
209
|
-
|
|
210
|
-
_(
|
|
211
|
-
album
|
|
212
|
-
.extend(representer)
|
|
213
|
-
.from_hash("songs" => [{"title" => "Invincible"}])
|
|
214
|
-
.songs
|
|
215
|
-
).must_equal [
|
|
216
|
-
Song.new(1, "The Answer Is Still No"),
|
|
217
|
-
Song.new(2, "Invincible")
|
|
218
|
-
]
|
|
219
|
-
|
|
220
|
-
_(songs.object_id).must_equal album.songs.object_id
|
|
221
|
-
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
222
|
-
}
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
# not sure if this must be a library strategy
|
|
226
|
-
describe "replace existing element" do
|
|
227
|
-
representer!(:inject => :song_representer) do
|
|
228
|
-
collection :songs,
|
|
229
|
-
:instance => ->(options) {
|
|
230
|
-
id = options[:fragment].delete("replace_id")
|
|
231
|
-
replaced = songs.find { |s| s.id == id }
|
|
232
|
-
songs[songs.index(replaced)] = song = Song.new(3)
|
|
233
|
-
song
|
|
234
|
-
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
|
235
|
-
|
|
236
|
-
:extend => song_representer,
|
|
237
|
-
# :parse_strategy => :sync
|
|
238
|
-
:setter => ->(*) {}
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
it {
|
|
242
|
-
album = Struct.new(:songs).new(
|
|
243
|
-
songs = [
|
|
244
|
-
Song.new(1, "The Answer Is Still No"),
|
|
245
|
-
Song.new(2, "Invincible")
|
|
246
|
-
]
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
_(
|
|
250
|
-
album
|
|
251
|
-
.extend(representer)
|
|
252
|
-
.from_hash("songs" => [{"replace_id" => 2, "id" => 3, "title" => "Soulmate"}])
|
|
253
|
-
.songs
|
|
254
|
-
).must_equal [
|
|
255
|
-
Song.new(1, "The Answer Is Still No"),
|
|
256
|
-
Song.new(3, "Soulmate")
|
|
257
|
-
]
|
|
258
|
-
|
|
259
|
-
_(songs.object_id).must_equal album.songs.object_id
|
|
260
|
-
_(songs[0].object_id).must_equal album.songs[0].object_id
|
|
261
|
-
}
|
|
262
|
-
end
|
|
263
|
-
|
|
264
|
-
describe "replace collection" do
|
|
265
|
-
representer!(:inject => :song_representer) do
|
|
266
|
-
collection :songs,
|
|
267
|
-
:extend => song_representer, :class => Song
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
it {
|
|
271
|
-
album = Struct.new(:songs).new(
|
|
272
|
-
songs = [
|
|
273
|
-
Song.new(1, "The Answer Is Still No")
|
|
274
|
-
]
|
|
275
|
-
)
|
|
276
|
-
|
|
277
|
-
_(
|
|
278
|
-
album
|
|
279
|
-
.extend(representer)
|
|
280
|
-
.from_hash("songs" => [{"title" => "Invincible"}])
|
|
281
|
-
.songs
|
|
282
|
-
).must_equal [
|
|
283
|
-
Song.new(nil, "Invincible")
|
|
284
|
-
]
|
|
285
|
-
|
|
286
|
-
_(songs.object_id).wont_equal album.songs.object_id
|
|
287
|
-
}
|
|
288
|
-
end
|
|
289
|
-
end
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class IsRepresentableTest < BaseTest
|
|
4
|
-
describe "representable: false, extend:" do
|
|
5
|
-
representer!(:inject => :song_representer) do
|
|
6
|
-
property :song,
|
|
7
|
-
:representable => false,
|
|
8
|
-
:extend => song_representer
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it "does extend but doesn't call #to_hash" do
|
|
12
|
-
_(
|
|
13
|
-
Struct.new(:song).new(song = Object.new).extend(representer)
|
|
14
|
-
.to_hash
|
|
15
|
-
).must_equal("song" => song)
|
|
16
|
-
_(song).must_be_kind_of Representable::Hash
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
describe "representable: true, no extend:" do
|
|
21
|
-
representer!(:inject => :song_representer) do
|
|
22
|
-
property :song,
|
|
23
|
-
:representable => true
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it "doesn't extend but calls #to_hash" do
|
|
27
|
-
song = Object.new
|
|
28
|
-
song.instance_eval do
|
|
29
|
-
def to_hash(*)
|
|
30
|
-
1
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
_(
|
|
35
|
-
Struct.new(:song).new(song).extend(representer)
|
|
36
|
-
.to_hash
|
|
37
|
-
).must_equal("song" => 1)
|
|
38
|
-
_(song).wont_be_kind_of Representable::Hash
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# TODO: TEST implement for from_hash.
|
|
43
|
-
|
|
44
|
-
describe "representable: false, with class:" do
|
|
45
|
-
representer!(:inject => :song_representer) do
|
|
46
|
-
property :song,
|
|
47
|
-
:representable => false, :class => OpenStruct, :extend => song_representer
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it "does extend but doesn't call #from_hash" do
|
|
51
|
-
hit = Struct.new(:song).new.extend(representer)
|
|
52
|
-
.from_hash("song" => 1)
|
|
53
|
-
|
|
54
|
-
_(hit.song).must_equal OpenStruct.new
|
|
55
|
-
_(hit.song).must_be_kind_of Representable::Hash
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
describe "representable: true, without extend: but class:" do
|
|
60
|
-
SongReader = Class.new do
|
|
61
|
-
def from_hash(*)
|
|
62
|
-
"Piano?"
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
representer!(:inject => :song_representer) do
|
|
67
|
-
property :song,
|
|
68
|
-
:representable => true, :class => SongReader
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
it "doesn't extend but calls #from_hash" do
|
|
72
|
-
hit = Struct.new(:song).new.extend(representer)
|
|
73
|
-
.from_hash("song" => "Sonata No.2")
|
|
74
|
-
|
|
75
|
-
_(hit.song).must_equal "Piano?"
|
|
76
|
-
_(hit.song).wont_be_kind_of Representable::Hash
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|