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/representable_test.rb
DELETED
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class RepresentableTest < Minitest::Spec
|
|
4
|
-
class Band
|
|
5
|
-
include Representable::Hash
|
|
6
|
-
property :name
|
|
7
|
-
attr_accessor :name
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
class PunkBand < Band
|
|
11
|
-
property :street_cred
|
|
12
|
-
attr_accessor :street_cred
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
module BandRepresentation
|
|
16
|
-
include Representable
|
|
17
|
-
|
|
18
|
-
property :name
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
module PunkBandRepresentation
|
|
22
|
-
include Representable
|
|
23
|
-
include BandRepresentation
|
|
24
|
-
|
|
25
|
-
property :street_cred
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
describe "#representable_attrs" do
|
|
29
|
-
describe "in module" do
|
|
30
|
-
it "allows including the concrete representer module later" do
|
|
31
|
-
vd = class VD
|
|
32
|
-
attr_accessor :name, :street_cred
|
|
33
|
-
|
|
34
|
-
include Representable::JSON
|
|
35
|
-
include PunkBandRepresentation
|
|
36
|
-
end.new
|
|
37
|
-
vd.name = "Vention Dention"
|
|
38
|
-
vd.street_cred = 1
|
|
39
|
-
assert_json "{\"name\":\"Vention Dention\",\"street_cred\":1}", vd.to_json
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# it "allows including the concrete representer module only" do
|
|
43
|
-
# require 'representable/json'
|
|
44
|
-
# module RockBandRepresentation
|
|
45
|
-
# include Representable::JSON
|
|
46
|
-
# property :name
|
|
47
|
-
# end
|
|
48
|
-
# vd = class VH
|
|
49
|
-
# include RockBandRepresentation
|
|
50
|
-
# end.new
|
|
51
|
-
# vd.name = "Van Halen"
|
|
52
|
-
# assert_equal "{\"name\":\"Van Halen\"}", vd.to_json
|
|
53
|
-
# end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
describe "inheritance" do
|
|
58
|
-
class CoverSong < OpenStruct
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
module SongRepresenter
|
|
62
|
-
include Representable::Hash
|
|
63
|
-
property :name
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
module CoverSongRepresenter
|
|
67
|
-
include Representable::Hash
|
|
68
|
-
include SongRepresenter
|
|
69
|
-
property :by
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it "merges properties from all ancestors" do
|
|
73
|
-
props = {"name" => "The Brews", "by" => "Nofx"}
|
|
74
|
-
assert_equal(props, CoverSong.new(props).extend(CoverSongRepresenter).to_hash)
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
it "allows mixing in multiple representers" do
|
|
78
|
-
class Bodyjar
|
|
79
|
-
include Representable::XML
|
|
80
|
-
include Representable::JSON
|
|
81
|
-
include PunkBandRepresentation
|
|
82
|
-
|
|
83
|
-
self.representation_wrap = "band"
|
|
84
|
-
attr_accessor :name, :street_cred
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
band = Bodyjar.new
|
|
88
|
-
band.name = "Bodyjar"
|
|
89
|
-
|
|
90
|
-
assert_json "{\"band\":{\"name\":\"Bodyjar\"}}", band.to_json
|
|
91
|
-
assert_xml_equal(band.to_xml, "<band><name>Bodyjar</name></band>")
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
it "allows extending with different representers subsequentially" do
|
|
95
|
-
module SongXmlRepresenter
|
|
96
|
-
include Representable::XML
|
|
97
|
-
property :name, :as => "name", :attribute => true
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
module SongJsonRepresenter
|
|
101
|
-
include Representable::JSON
|
|
102
|
-
property :name
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
@song = Song.new("Days Go By")
|
|
106
|
-
assert_xml_equal(@song.extend(SongXmlRepresenter).to_xml, "<song name=\"Days Go By\"/>")
|
|
107
|
-
assert_json "{\"name\":\"Days Go By\"}", @song.extend(SongJsonRepresenter).to_json
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# test if we call super in
|
|
111
|
-
# ::inherited
|
|
112
|
-
# ::included
|
|
113
|
-
# ::extended
|
|
114
|
-
module Representer
|
|
115
|
-
include Representable # overrides ::inherited.
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
class BaseClass
|
|
119
|
-
def self.inherited(subclass)
|
|
120
|
-
super
|
|
121
|
-
subclass.instance_eval do
|
|
122
|
-
def other
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
include Representable # overrides ::inherited.
|
|
128
|
-
include Representer
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
# triggers Representable::inherited, then OtherModule::inherited.
|
|
132
|
-
class SubClass < BaseClass
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
# test ::inherited.
|
|
136
|
-
it do
|
|
137
|
-
_(BaseClass.respond_to?(:other)).must_equal false
|
|
138
|
-
_(SubClass.respond_to?(:other)).must_equal true
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
module DifferentIncluded
|
|
142
|
-
def included(includer)
|
|
143
|
-
includer.instance_eval do
|
|
144
|
-
def different
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
module CombinedIncluded
|
|
151
|
-
extend DifferentIncluded # defines ::included.
|
|
152
|
-
include Representable # overrides ::included.
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
class IncludingClass
|
|
156
|
-
include Representable
|
|
157
|
-
include CombinedIncluded
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
# test ::included.
|
|
161
|
-
it do
|
|
162
|
-
IncludingClass.respond_to?(:representable_attrs) # from Representable
|
|
163
|
-
IncludingClass.respond_to?(:different)
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
describe "#property" do
|
|
168
|
-
it "doesn't modify options hash" do
|
|
169
|
-
options = {}
|
|
170
|
-
representer.property(:title, options)
|
|
171
|
-
_(options).must_equal({})
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
representer! {}
|
|
175
|
-
|
|
176
|
-
it "returns the Definition instance" do
|
|
177
|
-
_(representer.property(:name)).must_be_kind_of Representable::Definition
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
describe "#collection" do
|
|
182
|
-
class RockBand < Band
|
|
183
|
-
collection :albums
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
it "creates correct Definition" do
|
|
187
|
-
assert_equal "albums", RockBand.representable_attrs.get(:albums).name
|
|
188
|
-
assert RockBand.representable_attrs.get(:albums).array?
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
describe "#hash" do
|
|
193
|
-
it "also responds to the original method" do
|
|
194
|
-
assert_kind_of Integer, BandRepresentation.hash
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
class Hometown
|
|
199
|
-
attr_accessor :name
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
module HometownRepresentable
|
|
203
|
-
include Representable::JSON
|
|
204
|
-
property :name
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
# DISCUSS: i don't like the JSON requirement here, what about some generic test module?
|
|
208
|
-
class PopBand
|
|
209
|
-
include Representable::JSON
|
|
210
|
-
property :name
|
|
211
|
-
property :groupies
|
|
212
|
-
property :hometown, class: Hometown, extend: HometownRepresentable
|
|
213
|
-
attr_accessor :name, :groupies, :hometown
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
describe "#update_properties_from" do
|
|
217
|
-
before do
|
|
218
|
-
@band = PopBand.new
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
it "copies values from document to object" do
|
|
222
|
-
@band.from_hash({"name" => "No One's Choice", "groupies" => 2})
|
|
223
|
-
assert_equal "No One's Choice", @band.name
|
|
224
|
-
assert_equal 2, @band.groupies
|
|
225
|
-
end
|
|
226
|
-
|
|
227
|
-
it "ignores non-writeable properties" do
|
|
228
|
-
@band = Class.new(Band) { property :name; collection :founders, :writeable => false; attr_accessor :founders }.new
|
|
229
|
-
@band.from_hash("name" => "Iron Maiden", "groupies" => 2, "founders" => ["Steve Harris"])
|
|
230
|
-
assert_equal "Iron Maiden", @band.name
|
|
231
|
-
assert_nil @band.founders
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
it "always returns the represented" do
|
|
235
|
-
assert_equal @band, @band.from_hash({"name"=>"Nofx"})
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
it "includes false attributes" do
|
|
239
|
-
@band.from_hash({"groupies"=>false})
|
|
240
|
-
refute @band.groupies
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
it "ignores properties not present in the incoming document" do
|
|
244
|
-
@band.instance_eval do
|
|
245
|
-
def name=(*); fail "I should never be called!"; end
|
|
246
|
-
end
|
|
247
|
-
@band.from_hash({})
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
# FIXME: do we need this test with XML _and_ JSON?
|
|
251
|
-
it "ignores (no-default) properties not present in the incoming document" do
|
|
252
|
-
{
|
|
253
|
-
Representable::Hash => [:from_hash, {}],
|
|
254
|
-
Representable::XML => [:from_xml, xml(%{<band/>}).to_s]
|
|
255
|
-
}.each do |format, config|
|
|
256
|
-
nested_repr = Module.new do # this module is never applied. # FIXME: can we make that a simpler test?
|
|
257
|
-
include format
|
|
258
|
-
property :created_at
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
repr = Module.new do
|
|
262
|
-
include format
|
|
263
|
-
property :name, :class => Object, :extend => nested_repr
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
@band = Band.new.extend(repr)
|
|
267
|
-
@band.send(config.first, config.last)
|
|
268
|
-
assert_nil @band.name, "Failed in #{format}"
|
|
269
|
-
end
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
describe "passing options" do
|
|
273
|
-
module TrackRepresenter
|
|
274
|
-
include Representable::Hash
|
|
275
|
-
end
|
|
276
|
-
|
|
277
|
-
representer! do
|
|
278
|
-
property :track, class: OpenStruct do
|
|
279
|
-
property :nr
|
|
280
|
-
|
|
281
|
-
property :length, class: OpenStruct do
|
|
282
|
-
def to_hash(options)
|
|
283
|
-
{seconds: options[:user_options][:nr]}
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
def from_hash(hash, options)
|
|
287
|
-
super.tap do
|
|
288
|
-
self.seconds = options[:user_options][:nr]
|
|
289
|
-
end
|
|
290
|
-
end
|
|
291
|
-
end
|
|
292
|
-
|
|
293
|
-
def to_hash(options)
|
|
294
|
-
super.merge({"nr" => options[:user_options][:nr]})
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
def from_hash(data, options)
|
|
298
|
-
super.tap do
|
|
299
|
-
self.nr = options[:user_options][:nr]
|
|
300
|
-
end
|
|
301
|
-
end
|
|
302
|
-
end
|
|
303
|
-
end
|
|
304
|
-
|
|
305
|
-
it "#to_hash propagates to nested objects" do
|
|
306
|
-
_(
|
|
307
|
-
OpenStruct.new(
|
|
308
|
-
track: OpenStruct.new(
|
|
309
|
-
nr: 1,
|
|
310
|
-
length: OpenStruct.new(seconds: nil)
|
|
311
|
-
)
|
|
312
|
-
).extend(representer).extend(Representable::Debug)
|
|
313
|
-
.to_hash(user_options: {nr: 9})
|
|
314
|
-
).must_equal({"track"=>{"nr" => 9, "length" => {seconds: 9}}})
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
it "#from_hash propagates to nested objects" do
|
|
318
|
-
song = OpenStruct.new.extend(representer).from_hash(
|
|
319
|
-
{"track"=>{"nr" => "replace me", "length" => {"seconds"=>"replacing"}}}, user_options: {nr: 9}
|
|
320
|
-
)
|
|
321
|
-
_(song.track.nr).must_equal 9
|
|
322
|
-
_(song.track.length.seconds).must_equal 9
|
|
323
|
-
end
|
|
324
|
-
end
|
|
325
|
-
end
|
|
326
|
-
|
|
327
|
-
describe "#create_representation_with" do
|
|
328
|
-
before do
|
|
329
|
-
@band = PopBand.new
|
|
330
|
-
@band.name = "No One's Choice"
|
|
331
|
-
@band.groupies = 2
|
|
332
|
-
end
|
|
333
|
-
|
|
334
|
-
it "compiles document from properties in object" do
|
|
335
|
-
assert_equal({"name" => "No One's Choice", "groupies" => 2}, @band.to_hash)
|
|
336
|
-
end
|
|
337
|
-
|
|
338
|
-
it "ignores non-readable properties" do
|
|
339
|
-
@band = Class.new(Band) { property :name; collection :founder_ids, :readable => false; attr_accessor :founder_ids }.new
|
|
340
|
-
@band.name = "Iron Maiden"
|
|
341
|
-
@band.founder_ids = [1, 2, 3]
|
|
342
|
-
|
|
343
|
-
hash = @band.to_hash
|
|
344
|
-
assert_equal({"name" => "Iron Maiden"}, hash)
|
|
345
|
-
end
|
|
346
|
-
|
|
347
|
-
it "does not write nil attributes" do
|
|
348
|
-
@band.groupies = nil
|
|
349
|
-
assert_equal({"name"=>"No One's Choice"}, @band.to_hash)
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
it "writes false attributes" do
|
|
353
|
-
@band.groupies = false
|
|
354
|
-
assert_equal({"name" => "No One's Choice", "groupies" => false}, @band.to_hash)
|
|
355
|
-
end
|
|
356
|
-
end
|
|
357
|
-
|
|
358
|
-
describe ":extend and :class" do
|
|
359
|
-
module UpcaseRepresenter
|
|
360
|
-
include Representable
|
|
361
|
-
def to_hash(*); upcase; end
|
|
362
|
-
# DISCUSS: from_hash must return self.
|
|
363
|
-
def from_hash(hsh, *_args); replace hsh.upcase; end
|
|
364
|
-
end
|
|
365
|
-
|
|
366
|
-
module DowncaseRepresenter
|
|
367
|
-
include Representable
|
|
368
|
-
def to_hash(*); downcase; end
|
|
369
|
-
def from_hash(hsh, *_args); replace hsh.downcase; end
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
class UpcaseString < String; end
|
|
373
|
-
|
|
374
|
-
describe "lambda blocks" do
|
|
375
|
-
representer! do
|
|
376
|
-
property :name, :extend => ->(name, *) { compute_representer(name) }
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
it "executes lambda in represented instance context" do
|
|
380
|
-
_(
|
|
381
|
-
Song.new("Carnage").instance_eval {
|
|
382
|
-
def compute_representer(_name)
|
|
383
|
-
UpcaseRepresenter
|
|
384
|
-
end
|
|
385
|
-
self
|
|
386
|
-
}.extend(representer).to_hash
|
|
387
|
-
).must_equal({"name" => "CARNAGE"})
|
|
388
|
-
end
|
|
389
|
-
end
|
|
390
|
-
|
|
391
|
-
describe ":instance" do
|
|
392
|
-
obj = String.new("Fate")
|
|
393
|
-
mod = Module.new { include Representable; def from_hash(*); self; end }
|
|
394
|
-
representer! do
|
|
395
|
-
property :name, :extend => mod, :instance => ->(*) { obj }
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
it "uses object from :instance but still extends it" do
|
|
399
|
-
song = Song.new.extend(representer).from_hash("name" => "Eric's Had A Bad Day")
|
|
400
|
-
_(song.name).must_equal obj
|
|
401
|
-
_(song.name).must_be_kind_of mod
|
|
402
|
-
end
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
describe "property with :extend" do
|
|
406
|
-
representer! do
|
|
407
|
-
property :name, :extend => ->(options) {
|
|
408
|
-
options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter
|
|
409
|
-
}, :class => String
|
|
410
|
-
end
|
|
411
|
-
|
|
412
|
-
it "uses lambda when rendering" do
|
|
413
|
-
assert_equal({"name" => "you make me thick"}, Song.new("You Make Me Thick").extend(representer).to_hash)
|
|
414
|
-
assert_equal({"name" => "STEPSTRANGER"}, Song.new(UpcaseString.new("Stepstranger")).extend(representer).to_hash)
|
|
415
|
-
end
|
|
416
|
-
|
|
417
|
-
it "uses lambda when parsing" do
|
|
418
|
-
_(Song.new.extend(representer).from_hash({"name" => "You Make Me Thick"}).name).must_equal "you make me thick"
|
|
419
|
-
_(Song.new.extend(representer).from_hash({"name" => "Stepstranger"}).name).must_equal "stepstranger" # DISCUSS: we compare "".is_a?(UpcaseString)
|
|
420
|
-
end
|
|
421
|
-
|
|
422
|
-
describe "with :class lambda" do
|
|
423
|
-
representer! do
|
|
424
|
-
property :name, :extend => ->(options) {
|
|
425
|
-
options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter
|
|
426
|
-
},
|
|
427
|
-
:class => ->(options) { options[:fragment] == "Still Failing?" ? String : UpcaseString }
|
|
428
|
-
end
|
|
429
|
-
|
|
430
|
-
it "creates instance from :class lambda when parsing" do
|
|
431
|
-
song = OpenStruct.new.extend(representer).from_hash({"name" => "Quitters Never Win"})
|
|
432
|
-
_(song.name).must_be_kind_of UpcaseString
|
|
433
|
-
_(song.name).must_equal "QUITTERS NEVER WIN"
|
|
434
|
-
|
|
435
|
-
song = OpenStruct.new.extend(representer).from_hash({"name" => "Still Failing?"})
|
|
436
|
-
_(song.name).must_be_kind_of String
|
|
437
|
-
_(song.name).must_equal "still failing?"
|
|
438
|
-
end
|
|
439
|
-
end
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
describe "collection with :extend" do
|
|
443
|
-
representer! do
|
|
444
|
-
collection :songs, :extend => ->(options) {
|
|
445
|
-
options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter
|
|
446
|
-
}, :class => String
|
|
447
|
-
end
|
|
448
|
-
|
|
449
|
-
it "uses lambda for each item when rendering" do
|
|
450
|
-
_(
|
|
451
|
-
Album.new(
|
|
452
|
-
[
|
|
453
|
-
UpcaseString.new("Dean Martin"),
|
|
454
|
-
"Charlie Still Smirks"
|
|
455
|
-
]
|
|
456
|
-
).extend(representer).to_hash
|
|
457
|
-
).must_equal("songs" => [
|
|
458
|
-
"DEAN MARTIN",
|
|
459
|
-
"charlie still smirks"
|
|
460
|
-
])
|
|
461
|
-
end
|
|
462
|
-
|
|
463
|
-
it "uses lambda for each item when parsing" do
|
|
464
|
-
album = Album.new.extend(representer).from_hash("songs"=>["DEAN MARTIN", "charlie still smirks"])
|
|
465
|
-
_(album.songs).must_equal ["dean martin", "charlie still smirks"] # DISCUSS: we compare "".is_a?(UpcaseString)
|
|
466
|
-
end
|
|
467
|
-
|
|
468
|
-
describe "with :class lambda" do
|
|
469
|
-
representer! do
|
|
470
|
-
collection :songs, :extend => ->(options) {
|
|
471
|
-
options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter
|
|
472
|
-
},
|
|
473
|
-
:class => ->(options) { options[:input] == "Still Failing?" ? String : UpcaseString }
|
|
474
|
-
end
|
|
475
|
-
|
|
476
|
-
it "creates instance from :class lambda for each item when parsing" do
|
|
477
|
-
album = Album.new.extend(representer).from_hash("songs"=>["Still Failing?", "charlie still smirks"])
|
|
478
|
-
_(album.songs).must_equal ["still failing?", "CHARLIE STILL SMIRKS"]
|
|
479
|
-
end
|
|
480
|
-
end
|
|
481
|
-
end
|
|
482
|
-
|
|
483
|
-
describe ":decorator" do
|
|
484
|
-
let(:extend_rpr) { Module.new { include Representable::Hash; collection :songs, :extend => SongRepresenter } }
|
|
485
|
-
let(:decorator_rpr) { Module.new { include Representable::Hash; collection :songs, :decorator => SongRepresenter } }
|
|
486
|
-
let(:songs) { [Song.new("Bloody Mary")] }
|
|
487
|
-
|
|
488
|
-
it "is aliased to :extend" do
|
|
489
|
-
_(Album.new(songs).extend(extend_rpr).to_hash).must_equal Album.new(songs).extend(decorator_rpr).to_hash
|
|
490
|
-
end
|
|
491
|
-
end
|
|
492
|
-
|
|
493
|
-
# TODO: Move to global place since it's used twice.
|
|
494
|
-
class SongRepresentation < Representable::Decorator
|
|
495
|
-
include Representable::JSON
|
|
496
|
-
property :name
|
|
497
|
-
end
|
|
498
|
-
|
|
499
|
-
class AlbumRepresentation < Representable::Decorator
|
|
500
|
-
include Representable::JSON
|
|
501
|
-
|
|
502
|
-
collection :songs, :class => Song, :extend => SongRepresentation
|
|
503
|
-
end
|
|
504
|
-
|
|
505
|
-
describe "::prepare" do
|
|
506
|
-
let(:song) { Song.new("Still Friends In The End") }
|
|
507
|
-
let(:album) { Album.new([song]) }
|
|
508
|
-
|
|
509
|
-
describe "module including Representable" do
|
|
510
|
-
it "uses :extend strategy" do
|
|
511
|
-
album_rpr = Module.new { include Representable::Hash; collection :songs, :class => Song, :extend => SongRepresenter }
|
|
512
|
-
|
|
513
|
-
_(album_rpr.prepare(album).to_hash).must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
|
|
514
|
-
_(album).must_respond_to :to_hash
|
|
515
|
-
end
|
|
516
|
-
end
|
|
517
|
-
|
|
518
|
-
describe "Decorator subclass" do
|
|
519
|
-
it "uses :decorate strategy" do
|
|
520
|
-
_(AlbumRepresentation.prepare(album).to_hash).must_equal({"songs"=>[{"name"=>"Still Friends In The End"}]})
|
|
521
|
-
_(album).wont_respond_to :to_hash
|
|
522
|
-
end
|
|
523
|
-
end
|
|
524
|
-
end
|
|
525
|
-
end
|
|
526
|
-
end
|
data/test/schema_test.rb
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
# Include Inherit Module And Decorator Test
|
|
4
|
-
class SchemaTest < Minitest::Spec
|
|
5
|
-
module Genre
|
|
6
|
-
include Representable
|
|
7
|
-
property :genre
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
module LinkFeature
|
|
11
|
-
def self.included(base)
|
|
12
|
-
base.extend(Link)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
module Link
|
|
16
|
-
def link
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
module Module
|
|
22
|
-
include Representable::Hash
|
|
23
|
-
feature LinkFeature
|
|
24
|
-
|
|
25
|
-
property :title
|
|
26
|
-
property :label do # extend: LabelModule
|
|
27
|
-
property :name
|
|
28
|
-
link # feature
|
|
29
|
-
|
|
30
|
-
property :location do
|
|
31
|
-
property :city
|
|
32
|
-
link # feature.
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
property :album, :extend => -> { fail "don't manifest me!" } # this is not an inline decorator, don't manifest it.
|
|
37
|
-
|
|
38
|
-
include Genre # Schema::Included::included is called!
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
class WithLocationStreetRepresenter < Representable::Decorator
|
|
42
|
-
include Representable::Hash
|
|
43
|
-
feature LinkFeature
|
|
44
|
-
|
|
45
|
-
property :title
|
|
46
|
-
property :label do # extend: LabelModule
|
|
47
|
-
property :name
|
|
48
|
-
link # feature
|
|
49
|
-
|
|
50
|
-
property :location do
|
|
51
|
-
property :city
|
|
52
|
-
link # feature.
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
describe "3-level deep with features" do
|
|
58
|
-
let(:label) { OpenStruct.new(:name => "Epitaph", :location => OpenStruct.new(:city => "Sanfran", :name => "DON'T SHOW ME!")) }
|
|
59
|
-
|
|
60
|
-
# Module does correctly include features in inlines.
|
|
61
|
-
it {
|
|
62
|
-
_(band.extend(Module).to_hash).must_equal(
|
|
63
|
-
{
|
|
64
|
-
"label" => {"name" => "Epitaph", "location" => {"city"=>"Sanfran"}},
|
|
65
|
-
"genre" => "Punkrock"
|
|
66
|
-
}
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
# Decorator does correctly include features in inlines.
|
|
71
|
-
it {
|
|
72
|
-
_(Decorator.new(band).to_hash).must_equal(
|
|
73
|
-
{
|
|
74
|
-
"label" => {"name" => "Epitaph", "location" => {"city"=>"Sanfran"}},
|
|
75
|
-
"genre" => "Punkrock"
|
|
76
|
-
}
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
class Decorator < Representable::Decorator
|
|
82
|
-
feature Representable::Hash
|
|
83
|
-
|
|
84
|
-
include Module
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
# puts Decorator.representable_attrs[:definitions].inspect
|
|
88
|
-
|
|
89
|
-
let(:label) do
|
|
90
|
-
OpenStruct.new(
|
|
91
|
-
:name => "Fat Wreck", :city => "San Francisco", :employees => [OpenStruct.new(:name => "Mike")],
|
|
92
|
-
:location => OpenStruct.new(:city => "Sanfran")
|
|
93
|
-
)
|
|
94
|
-
end
|
|
95
|
-
let(:band) { OpenStruct.new(:genre => "Punkrock", :label => label) }
|
|
96
|
-
|
|
97
|
-
# it { FlatlinersDecorator.new( OpenStruct.new(label: OpenStruct.new) ).
|
|
98
|
-
# to_hash.must_equal({}) }
|
|
99
|
-
it do
|
|
100
|
-
_(Decorator.new(band).to_hash).must_equal(
|
|
101
|
-
{
|
|
102
|
-
"genre" => "Punkrock",
|
|
103
|
-
"label" => {"name" => "Fat Wreck", "location" => {"city"=>"Sanfran"}}
|
|
104
|
-
}
|
|
105
|
-
)
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
class InheritDecorator < Representable::Decorator
|
|
109
|
-
include Representable::Hash
|
|
110
|
-
|
|
111
|
-
include Module
|
|
112
|
-
|
|
113
|
-
property :label, inherit: true do # decorator.rb:27:in `initialize': superclass must be a Class (Module given)
|
|
114
|
-
property :city
|
|
115
|
-
|
|
116
|
-
property :location, :inherit => true do
|
|
117
|
-
property :city
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
it do
|
|
123
|
-
_(InheritDecorator.new(band).to_hash).must_equal(
|
|
124
|
-
{
|
|
125
|
-
"genre" => "Punkrock",
|
|
126
|
-
"label" => {
|
|
127
|
-
"name" => "Fat Wreck", "city" => "San Francisco",
|
|
128
|
-
"location" => {"city"=>"Sanfran"}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
class InheritFromDecorator < InheritDecorator
|
|
135
|
-
property :label, inherit: true do
|
|
136
|
-
collection :employees do
|
|
137
|
-
property :name
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
it do
|
|
143
|
-
_(InheritFromDecorator.new(band).to_hash).must_equal(
|
|
144
|
-
{
|
|
145
|
-
"genre" => "Punkrock",
|
|
146
|
-
"label" => {
|
|
147
|
-
"name" => "Fat Wreck", "city" => "San Francisco", "employees" => [{"name"=>"Mike"}],
|
|
148
|
-
"location" => {"city"=>"Sanfran"}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
)
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
class ApplyTest < Minitest::Spec
|
|
156
|
-
class AlbumDecorator < Representable::Decorator
|
|
157
|
-
include Representable::Hash
|
|
158
|
-
|
|
159
|
-
property :title
|
|
160
|
-
|
|
161
|
-
property :hit do
|
|
162
|
-
property :title
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
collection :songs do
|
|
166
|
-
property :title
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
property :band do # yepp, people do crazy stuff like that.
|
|
170
|
-
property :label do
|
|
171
|
-
property :name
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
end
|