representable 3.2.0 → 3.3.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/.github/workflows/ci.yml +17 -10
- data/.github/workflows/ci_jruby.yml +21 -0
- data/.github/workflows/ci_truffleruby.yml +19 -0
- data/CHANGES.md +5 -0
- data/Gemfile +1 -1
- data/README.md +0 -1
- data/lib/representable/hash/binding.rb +1 -0
- data/lib/representable/json.rb +14 -2
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml/binding.rb +0 -7
- data/lib/representable/xml.rb +7 -0
- data/representable.gemspec +1 -3
- data/test/as_test.rb +8 -13
- data/test/benchmarking.rb +20 -25
- data/test/binding_test.rb +13 -5
- data/test/cached_test.rb +55 -35
- data/test/class_test.rb +46 -33
- data/test/coercion_test.rb +29 -19
- data/test/config/inherit_test.rb +12 -17
- data/test/config_test.rb +5 -7
- data/test/decorator_scope_test.rb +13 -11
- data/test/decorator_test.rb +11 -11
- data/test/default_test.rb +2 -2
- data/test/defaults_options_test.rb +37 -9
- data/test/definition_test.rb +10 -12
- data/test/examples/example.rb +43 -38
- data/test/examples/object.rb +7 -6
- data/test/exec_context_test.rb +18 -23
- data/test/features_test.rb +29 -10
- data/test/filter_test.rb +29 -20
- data/test/for_collection_test.rb +12 -15
- data/test/generic_test.rb +15 -22
- data/test/getter_setter_test.rb +11 -6
- data/test/hash_bindings_test.rb +21 -12
- data/test/hash_test.rb +53 -38
- data/test/heritage_test.rb +0 -1
- data/test/if_test.rb +25 -21
- data/test/include_exclude_test.rb +24 -16
- data/test/inherit_test.rb +114 -27
- data/test/inline_test.rb +42 -33
- data/test/instance_test.rb +151 -109
- data/test/is_representable_test.rb +19 -17
- data/test/json_test.rb +48 -46
- data/test/lonely_test.rb +35 -33
- data/test/nested_test.rb +16 -19
- data/test/object_test.rb +6 -6
- data/test/option_test.rb +10 -8
- data/test/parse_pipeline_test.rb +21 -15
- data/test/pipeline_test.rb +144 -108
- data/test/populator_test.rb +18 -15
- data/test/prepare_test.rb +15 -17
- data/test/private_options_test.rb +3 -2
- data/test/reader_writer_test.rb +4 -4
- data/test/realistic_benchmark.rb +21 -27
- data/test/render_nil_test.rb +1 -1
- data/test/represent_test.rb +14 -18
- data/test/representable_test.rb +86 -53
- data/test/schema_test.rb +51 -21
- data/test/serialize_deserialize_test.rb +14 -14
- data/test/skip_test.rb +39 -28
- data/test/stringify_hash_test.rb +29 -31
- data/test/test_helper.rb +31 -25
- data/test/test_helper_test.rb +7 -7
- data/test/uncategorized_test.rb +20 -15
- data/test/user_options_test.rb +12 -3
- data/test/wrap_test.rb +27 -17
- data/test/xml_assertions.rb +29 -0
- data/test/xml_bindings_test.rb +5 -8
- data/test/xml_namespace_test.rb +46 -39
- data/test/xml_test.rb +109 -88
- data/test/yaml_test.rb +73 -51
- metadata +7 -32
data/test/json_test.rb
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require "test_helper"
|
|
2
|
+
require "json"
|
|
3
3
|
|
|
4
4
|
module JsonTest
|
|
5
|
-
class JSONPublicMethodsTest < Minitest::Spec
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
class JSONPublicMethodsTest < Minitest::Spec
|
|
6
|
+
#---
|
|
7
|
+
# from_json
|
|
8
|
+
class BandRepresenter < Representable::Decorator
|
|
9
|
+
include Representable::JSON
|
|
10
|
+
property :id
|
|
11
|
+
property :name
|
|
12
|
+
end
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
let(:json) { '{"id":1,"name":"Rancid"}' }
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
it { _(BandRepresenter.new(Band.new).from_json(json)[:id, :name]).must_equal [1, "Rancid"] }
|
|
17
|
+
it { _(BandRepresenter.new(Band.new).parse(json)[:id, :name]).must_equal [1, "Rancid"] }
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
#---
|
|
20
|
+
# to_json
|
|
21
|
+
let(:band) { Band.new(1, "Rancid") }
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
end
|
|
23
|
+
it { _(BandRepresenter.new(band).to_json).must_equal json }
|
|
24
|
+
it { _(BandRepresenter.new(band).render).must_equal json }
|
|
25
|
+
end
|
|
26
26
|
|
|
27
|
-
class APITest <
|
|
27
|
+
class APITest < Minitest::Spec
|
|
28
28
|
Json = Representable::JSON
|
|
29
29
|
Def = Representable::Definition
|
|
30
30
|
|
|
31
|
-
|
|
32
31
|
describe "JSON module" do
|
|
33
32
|
before do
|
|
34
33
|
@Band = Class.new do
|
|
@@ -37,7 +36,7 @@ end
|
|
|
37
36
|
property :label
|
|
38
37
|
attr_accessor :name, :label
|
|
39
38
|
|
|
40
|
-
def initialize(name=nil)
|
|
39
|
+
def initialize(name = nil)
|
|
41
40
|
self.name = name if name
|
|
42
41
|
end
|
|
43
42
|
end
|
|
@@ -45,29 +44,27 @@ end
|
|
|
45
44
|
@band = @Band.new
|
|
46
45
|
end
|
|
47
46
|
|
|
48
|
-
|
|
49
47
|
describe "#from_json" do
|
|
50
48
|
before do
|
|
51
49
|
@band = @Band.new
|
|
52
|
-
@json
|
|
50
|
+
@json = {:name => "Nofx", :label => "NOFX"}.to_json
|
|
53
51
|
end
|
|
54
52
|
|
|
55
53
|
it "parses JSON and assigns properties" do
|
|
56
54
|
@band.from_json(@json)
|
|
57
|
-
assert_equal [
|
|
55
|
+
assert_equal %w[Nofx NOFX], [@band.name, @band.label]
|
|
58
56
|
end
|
|
59
57
|
end
|
|
60
58
|
|
|
61
|
-
|
|
62
59
|
describe "#from_hash" do
|
|
63
60
|
before do
|
|
64
61
|
@band = @Band.new
|
|
65
|
-
@hash
|
|
62
|
+
@hash = {"name" => "Nofx", "label" => "NOFX"}
|
|
66
63
|
end
|
|
67
64
|
|
|
68
65
|
it "receives hash and assigns properties" do
|
|
69
66
|
@band.from_hash(@hash)
|
|
70
|
-
assert_equal [
|
|
67
|
+
assert_equal %w[Nofx NOFX], [@band.name, @band.label]
|
|
71
68
|
end
|
|
72
69
|
|
|
73
70
|
it "respects :wrap option" do
|
|
@@ -84,14 +81,12 @@ end
|
|
|
84
81
|
end
|
|
85
82
|
end
|
|
86
83
|
|
|
87
|
-
|
|
88
84
|
describe "#to_json" do
|
|
89
85
|
it "delegates to #to_hash and returns string" do
|
|
90
86
|
assert_json "{\"name\":\"Rise Against\"}", @Band.new("Rise Against").to_json
|
|
91
87
|
end
|
|
92
88
|
end
|
|
93
89
|
|
|
94
|
-
|
|
95
90
|
describe "#to_hash" do
|
|
96
91
|
it "returns unwrapped hash" do
|
|
97
92
|
hash = @Band.new("Rise Against").to_hash
|
|
@@ -116,12 +111,12 @@ end
|
|
|
116
111
|
end
|
|
117
112
|
|
|
118
113
|
it "returns CollectionBinding" do
|
|
119
|
-
assert_kind_of Representable::Hash::Binding::Collection,
|
|
114
|
+
assert_kind_of Representable::Hash::Binding::Collection,
|
|
115
|
+
Representable::Hash::Binding.build_for(Def.new(:band, :collection => true))
|
|
120
116
|
end
|
|
121
117
|
end
|
|
122
118
|
end
|
|
123
119
|
|
|
124
|
-
|
|
125
120
|
describe "DCI" do
|
|
126
121
|
module SongRepresenter
|
|
127
122
|
include Representable::JSON
|
|
@@ -134,7 +129,6 @@ end
|
|
|
134
129
|
collection :songs, :class => Song, :extend => SongRepresenter
|
|
135
130
|
end
|
|
136
131
|
|
|
137
|
-
|
|
138
132
|
it "allows adding the representer by using #extend" do
|
|
139
133
|
module BandRepresenter
|
|
140
134
|
include Representable::JSON
|
|
@@ -144,6 +138,7 @@ end
|
|
|
144
138
|
civ = Object.new
|
|
145
139
|
civ.instance_eval do
|
|
146
140
|
def name; "CIV"; end
|
|
141
|
+
|
|
147
142
|
def name=(v)
|
|
148
143
|
@name = v
|
|
149
144
|
end
|
|
@@ -154,14 +149,15 @@ end
|
|
|
154
149
|
end
|
|
155
150
|
|
|
156
151
|
it "extends contained models when serializing" do
|
|
157
|
-
@album = Album.new([Song.new("I Hate My Brain"), mr=Song.new("Mr. Charisma")], mr)
|
|
152
|
+
@album = Album.new([Song.new("I Hate My Brain"), mr = Song.new("Mr. Charisma")], mr)
|
|
158
153
|
@album.extend(AlbumRepresenter)
|
|
159
154
|
|
|
160
|
-
assert_json "{\"best_song\":{\"name\":\"Mr. Charisma\"},\"songs\":[{\"name\":\"I Hate My Brain\"},{\"name\":\"Mr. Charisma\"}]}",
|
|
155
|
+
assert_json "{\"best_song\":{\"name\":\"Mr. Charisma\"},\"songs\":[{\"name\":\"I Hate My Brain\"},{\"name\":\"Mr. Charisma\"}]}",
|
|
156
|
+
@album.to_json
|
|
161
157
|
end
|
|
162
158
|
|
|
163
159
|
it "extends contained models when deserializing" do
|
|
164
|
-
|
|
160
|
+
# @album = Album.new(Song.new("I Hate My Brain"), Song.new("Mr. Charisma"))
|
|
165
161
|
@album = Album.new
|
|
166
162
|
@album.extend(AlbumRepresenter)
|
|
167
163
|
|
|
@@ -171,8 +167,7 @@ end
|
|
|
171
167
|
end
|
|
172
168
|
end
|
|
173
169
|
|
|
174
|
-
|
|
175
|
-
class PropertyTest < MiniTest::Spec
|
|
170
|
+
class PropertyTest < Minitest::Spec
|
|
176
171
|
describe "property :name" do
|
|
177
172
|
class Band
|
|
178
173
|
include Representable::JSON
|
|
@@ -255,7 +250,7 @@ end
|
|
|
255
250
|
end
|
|
256
251
|
end
|
|
257
252
|
|
|
258
|
-
class CollectionTest <
|
|
253
|
+
class CollectionTest < Minitest::Spec
|
|
259
254
|
describe "collection :name" do
|
|
260
255
|
class CD
|
|
261
256
|
include Representable::JSON
|
|
@@ -282,7 +277,7 @@ end
|
|
|
282
277
|
property :name
|
|
283
278
|
attr_accessor :name
|
|
284
279
|
|
|
285
|
-
def initialize(name="")
|
|
280
|
+
def initialize(name = "")
|
|
286
281
|
self.name = name
|
|
287
282
|
end
|
|
288
283
|
end
|
|
@@ -295,9 +290,14 @@ end
|
|
|
295
290
|
|
|
296
291
|
describe "#from_json" do
|
|
297
292
|
it "pushes collection items to array" do
|
|
298
|
-
cd = Compilation.new.from_json(
|
|
299
|
-
{
|
|
300
|
-
|
|
293
|
+
cd = Compilation.new.from_json(
|
|
294
|
+
{
|
|
295
|
+
:bands => [
|
|
296
|
+
{:name => "Cobra Skulls"},
|
|
297
|
+
{:name => "Diesel Boy"}
|
|
298
|
+
]
|
|
299
|
+
}.to_json
|
|
300
|
+
)
|
|
301
301
|
assert_equal ["Cobra Skulls", "Diesel Boy"], cd.bands.map(&:name).sort
|
|
302
302
|
end
|
|
303
303
|
end
|
|
@@ -310,7 +310,6 @@ end
|
|
|
310
310
|
end
|
|
311
311
|
end
|
|
312
312
|
|
|
313
|
-
|
|
314
313
|
describe ":as => :songList" do
|
|
315
314
|
class Songs
|
|
316
315
|
include Representable::JSON
|
|
@@ -332,7 +331,7 @@ end
|
|
|
332
331
|
end
|
|
333
332
|
end
|
|
334
333
|
|
|
335
|
-
class HashTest <
|
|
334
|
+
class HashTest < Minitest::Spec
|
|
336
335
|
describe "hash :songs" do
|
|
337
336
|
representer!(:module => Representable::JSON) do
|
|
338
337
|
hash :songs
|
|
@@ -346,7 +345,10 @@ end
|
|
|
346
345
|
end
|
|
347
346
|
|
|
348
347
|
it "parses with #from_json" do
|
|
349
|
-
assert_equal(
|
|
348
|
+
assert_equal(
|
|
349
|
+
{"one" => "65", "two" => ["Emo Boy"]},
|
|
350
|
+
subject.from_json("{\"songs\":{\"one\":\"65\",\"two\":[\"Emo Boy\"]}}").songs
|
|
351
|
+
)
|
|
350
352
|
end
|
|
351
353
|
end
|
|
352
354
|
|
data/test/lonely_test.rb
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "test_helper"
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
|
|
5
|
-
class LonelyRepresenterTest < MiniTest::Spec
|
|
3
|
+
require "representable/json/hash"
|
|
6
4
|
|
|
5
|
+
class LonelyRepresenterTest < Minitest::Spec
|
|
7
6
|
# test ::items without arguments, render-only.
|
|
8
7
|
for_formats(
|
|
9
8
|
:hash => [Representable::Hash::Collection, [{"name"=>"Resist Stance"}, {"name"=>"Suffer"}]],
|
|
10
9
|
:json => [Representable::JSON::Collection, "[{\"name\":\"Resist Stance\"},{\"name\":\"Suffer\"}]"],
|
|
11
|
-
:xml => [
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
:xml => [
|
|
11
|
+
Representable::XML::Collection,
|
|
12
|
+
"<array><song><name>Resist Stance</name></song><song><name>Suffer</name></song></array>"
|
|
13
|
+
]
|
|
14
|
+
) do |format, mod, output, _input|
|
|
14
15
|
describe "[#{format}] lonely collection, render-only" do # TODO: introduce :representable option?
|
|
15
16
|
let(:format) { format }
|
|
16
17
|
|
|
@@ -28,8 +29,6 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
28
29
|
end
|
|
29
30
|
end
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
32
|
module SongRepresenter
|
|
34
33
|
include Representable::JSON
|
|
35
34
|
|
|
@@ -43,13 +42,12 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
43
42
|
let(:json) { "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]" }
|
|
44
43
|
|
|
45
44
|
describe "with contained objects" do
|
|
46
|
-
let(:representer)
|
|
45
|
+
let(:representer) do
|
|
47
46
|
Module.new do
|
|
48
47
|
include Representable::JSON::Collection
|
|
49
48
|
items :class => Song, :extend => SongRepresenter
|
|
50
49
|
end
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
end
|
|
53
51
|
|
|
54
52
|
it "renders array" do
|
|
55
53
|
assert_json json, songs.extend(representer).to_json
|
|
@@ -80,11 +78,11 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
80
78
|
end
|
|
81
79
|
|
|
82
80
|
describe "with contained text" do
|
|
83
|
-
let(:representer)
|
|
81
|
+
let(:representer) do
|
|
84
82
|
Module.new do
|
|
85
83
|
include Representable::JSON::Collection
|
|
86
84
|
end
|
|
87
|
-
|
|
85
|
+
end
|
|
88
86
|
let(:songs) { ["Days Go By", "Can't Take Them All"] }
|
|
89
87
|
let(:json) { "[\"Days Go By\",\"Can't Take Them All\"]" }
|
|
90
88
|
|
|
@@ -111,19 +109,17 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
111
109
|
items extend: ->(options) { options[:input] == 1 ? options[:user_options][:one] : options[:user_options][:two] }
|
|
112
110
|
end
|
|
113
111
|
|
|
114
|
-
|
|
115
|
-
it { _([1,2].extend(representer).to_hash(user_options: {one: One, two: Two})).must_equal(["One: 1", "Two: 2"]) }
|
|
112
|
+
it { _([1, 2].extend(representer).to_hash(user_options: {one: One, two: Two})).must_equal(["One: 1", "Two: 2"]) }
|
|
116
113
|
end
|
|
117
114
|
|
|
118
|
-
|
|
119
|
-
describe "JSON::Hash" do # TODO: move to HashTest.
|
|
115
|
+
describe "JSON::Hash" do # TODO: move to HashTest.
|
|
120
116
|
describe "with contained objects" do
|
|
121
|
-
let(:representer)
|
|
117
|
+
let(:representer) do
|
|
122
118
|
Module.new do
|
|
123
119
|
include Representable::JSON::Hash
|
|
124
120
|
values :class => Song, :extend => SongRepresenter
|
|
125
121
|
end
|
|
126
|
-
|
|
122
|
+
end
|
|
127
123
|
let(:json) { "{\"one\":{\"name\":\"Days Go By\"},\"two\":{\"name\":\"Can't Take Them All\"}}" }
|
|
128
124
|
let(:songs) { {"one" => Song.new("Days Go By"), "two" => Song.new("Can't Take Them All")} }
|
|
129
125
|
|
|
@@ -137,11 +133,19 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
137
133
|
end
|
|
138
134
|
|
|
139
135
|
it "respects :exclude" do
|
|
140
|
-
assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}",
|
|
136
|
+
assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}",
|
|
137
|
+
{
|
|
138
|
+
:one => Song.new("Days Go By"),
|
|
139
|
+
:two => Song.new("Can't Take Them All")
|
|
140
|
+
}.extend(representer).to_json(:exclude => [:one])
|
|
141
141
|
end
|
|
142
142
|
|
|
143
143
|
it "respects :include" do
|
|
144
|
-
assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}",
|
|
144
|
+
assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}",
|
|
145
|
+
{
|
|
146
|
+
:one => Song.new("Days Go By"),
|
|
147
|
+
:two => Song.new("Can't Take Them All")
|
|
148
|
+
}.extend(representer).to_json(:include => [:two])
|
|
145
149
|
end
|
|
146
150
|
end
|
|
147
151
|
|
|
@@ -163,7 +167,6 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
163
167
|
end
|
|
164
168
|
end
|
|
165
169
|
|
|
166
|
-
|
|
167
170
|
describe "with inline representer" do
|
|
168
171
|
representer!(:module => Representable::JSON::Hash) do
|
|
169
172
|
values :class => Song do
|
|
@@ -176,14 +179,13 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
176
179
|
end
|
|
177
180
|
end
|
|
178
181
|
|
|
179
|
-
|
|
180
182
|
describe "with scalar" do
|
|
181
|
-
let(:representer)
|
|
183
|
+
let(:representer) do
|
|
182
184
|
Module.new do
|
|
183
185
|
include Representable::JSON::Hash
|
|
184
186
|
end
|
|
185
|
-
|
|
186
|
-
let(:json)
|
|
187
|
+
end
|
|
188
|
+
let(:json) { %{{"one":1,"two":2}} }
|
|
187
189
|
let(:data) { {one: 2, two: 3} }
|
|
188
190
|
|
|
189
191
|
describe "#to_json" do
|
|
@@ -203,7 +205,6 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
203
205
|
end
|
|
204
206
|
end
|
|
205
207
|
|
|
206
|
-
|
|
207
208
|
describe "with contained text" do
|
|
208
209
|
before do
|
|
209
210
|
@songs_representer = Module.new do
|
|
@@ -212,19 +213,20 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
|
212
213
|
end
|
|
213
214
|
|
|
214
215
|
it "renders contained items #to_json" do
|
|
215
|
-
assert_json "[\"Days Go By\",\"Can't Take Them All\"]",
|
|
216
|
+
assert_json "[\"Days Go By\",\"Can't Take Them All\"]",
|
|
217
|
+
["Days Go By", "Can't Take Them All"].extend(@songs_representer).to_json
|
|
216
218
|
end
|
|
217
219
|
|
|
218
220
|
it "returns objects array from #from_json" do
|
|
219
|
-
assert_equal ["Days Go By", "Can't Take Them All"],
|
|
221
|
+
assert_equal ["Days Go By", "Can't Take Them All"],
|
|
222
|
+
[].extend(@songs_representer).from_json("[\"Days Go By\",\"Can't Take Them All\"]")
|
|
220
223
|
end
|
|
221
224
|
end
|
|
222
225
|
end
|
|
223
226
|
end
|
|
224
227
|
|
|
225
|
-
|
|
226
228
|
# describe "Hash::Collection with :include" do
|
|
227
|
-
class CollectionWithIncludeTest <
|
|
229
|
+
class CollectionWithIncludeTest < Minitest::Spec
|
|
228
230
|
Song = Struct.new(:id, :title)
|
|
229
231
|
|
|
230
232
|
representer!(decorator: true, module: Representable::Hash::Collection) do
|
|
@@ -234,6 +236,6 @@ class CollectionWithIncludeTest < MiniTest::Spec
|
|
|
234
236
|
end
|
|
235
237
|
end
|
|
236
238
|
|
|
237
|
-
it { _(representer.new([Song.new(1, "ACAB")]).to_hash).must_equal([{"id"=>1, "title"=>"ACAB"}]) }
|
|
239
|
+
it { _(representer.new([Song.new(1, "ACAB")]).to_hash).must_equal([{"id" => 1, "title" => "ACAB"}]) }
|
|
238
240
|
it { _(representer.new([Song.new(1, "ACAB")]).to_hash(include: [:title])).must_equal([{"title"=>"ACAB"}]) }
|
|
239
241
|
end
|
data/test/nested_test.rb
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "test_helper"
|
|
2
2
|
|
|
3
|
-
class NestedTest <
|
|
4
|
-
|
|
3
|
+
class NestedTest < Minitest::Spec
|
|
4
|
+
Album = Struct.new(:label, :owner, :amount)
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
:hash => [
|
|
6
|
+
for_formats(
|
|
7
|
+
:hash => [
|
|
8
|
+
Representable::Hash,
|
|
9
|
+
{"label" => {"label" => "Epitaph", "owner" => "Brett Gurewitz", "releases" => {"amount"=>19}}}
|
|
10
|
+
],
|
|
8
11
|
# :xml => [Representable::XML, "<open_struct></open_struct>"],
|
|
9
12
|
:yaml => [Representable::YAML, "---\nlabel:\n label: Epitaph\n owner: Brett Gurewitz\n releases:\n amount: 19\n"]
|
|
10
|
-
) do |format, mod, output,
|
|
11
|
-
|
|
13
|
+
) do |format, mod, output, _input|
|
|
12
14
|
[false, true].each do |is_decorator|
|
|
13
15
|
describe "::nested with (inline representer|decorator): #{is_decorator}" do
|
|
14
16
|
let(:format) { format }
|
|
@@ -34,9 +36,7 @@ class NestedTest < MiniTest::Spec
|
|
|
34
36
|
render(decorator).must_equal_document output
|
|
35
37
|
|
|
36
38
|
# do not use extend on the nested object. # FIXME: make this a proper test with two describes instead of this pseudo-meta stuff.
|
|
37
|
-
if is_decorator==true
|
|
38
|
-
_(album).wont_be_kind_of(Representable::Hash)
|
|
39
|
-
end
|
|
39
|
+
_(album).wont_be_kind_of(Representable::Hash) if is_decorator == true
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it "parses nested properties to Album instance" do
|
|
@@ -47,13 +47,12 @@ class NestedTest < MiniTest::Spec
|
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
|
|
51
50
|
describe "Decorator ::nested with extend:" do
|
|
52
51
|
let(:format) { format }
|
|
53
52
|
|
|
54
53
|
representer!(:name => :label_rpr) do
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
include mod
|
|
55
|
+
property :label
|
|
57
56
|
property :owner
|
|
58
57
|
|
|
59
58
|
nested :releases do # DISCUSS: do we need to test this?
|
|
@@ -75,17 +74,15 @@ class NestedTest < MiniTest::Spec
|
|
|
75
74
|
end
|
|
76
75
|
|
|
77
76
|
it "parses nested properties to Album instance" do
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
album = parse(representer.prepare(Album.new), output)
|
|
78
|
+
_(album.label).must_equal "Epitaph"
|
|
79
|
+
_(album.owner).must_equal "Brett Gurewitz"
|
|
81
80
|
_(album.amount).must_equal 19
|
|
82
81
|
end
|
|
83
82
|
end
|
|
84
83
|
end
|
|
85
84
|
|
|
86
|
-
|
|
87
85
|
describe "::nested without block but with inherit:" do
|
|
88
|
-
|
|
89
86
|
representer!(:name => :parent) do
|
|
90
87
|
include Representable::Hash
|
|
91
88
|
|
|
@@ -112,4 +109,4 @@ class NestedTest < MiniTest::Spec
|
|
|
112
109
|
# album.amount.must_equal 19
|
|
113
110
|
# end
|
|
114
111
|
end
|
|
115
|
-
end
|
|
112
|
+
end
|
data/test/object_test.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
require "test_helper"
|
|
2
2
|
require "representable/object"
|
|
3
3
|
|
|
4
|
-
class ObjectTest <
|
|
4
|
+
class ObjectTest < Minitest::Spec
|
|
5
5
|
Song = Struct.new(:title, :album)
|
|
6
6
|
Album = Struct.new(:name, :songs)
|
|
7
7
|
|
|
8
8
|
representer!(module: Representable::Object) do
|
|
9
9
|
property :title
|
|
10
10
|
|
|
11
|
-
property :album, instance:
|
|
11
|
+
property :album, instance: ->(options) { options[:fragment].name.upcase!; options[:fragment] } do
|
|
12
12
|
property :name
|
|
13
13
|
|
|
14
|
-
collection :songs, instance:
|
|
14
|
+
collection :songs, instance: ->(options) { options[:fragment].title.upcase!; options[:fragment] } do
|
|
15
15
|
property :title
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -42,10 +42,10 @@ class ObjectTest < MiniTest::Spec
|
|
|
42
42
|
representer!(module: Representable::Object) do
|
|
43
43
|
property :title
|
|
44
44
|
|
|
45
|
-
property :album, render_filter:
|
|
45
|
+
property :album, render_filter: ->(input, _options) { input.name = "Live"; input } do
|
|
46
46
|
property :name
|
|
47
47
|
|
|
48
|
-
collection :songs, render_filter:
|
|
48
|
+
collection :songs, render_filter: ->(input, _options) { input[0].title = 1; input } do
|
|
49
49
|
property :title
|
|
50
50
|
end
|
|
51
51
|
end
|
|
@@ -57,4 +57,4 @@ class ObjectTest < MiniTest::Spec
|
|
|
57
57
|
_(source.album.songs[0].title).must_equal 1
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
|
-
end
|
|
60
|
+
end
|
data/test/option_test.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require "test_helper"
|
|
4
4
|
|
|
5
|
-
class OptionTest <
|
|
5
|
+
class OptionTest < Minitest::Spec
|
|
6
6
|
class Callable
|
|
7
7
|
include Uber::Callable
|
|
8
8
|
def call(*); "callable" end
|
|
@@ -25,12 +25,14 @@ class OptionTest < MiniTest::Spec
|
|
|
25
25
|
|
|
26
26
|
describe ::Representable::Option do
|
|
27
27
|
it "supports all types of callables (method, proc, static etc)" do
|
|
28
|
-
_(album_representer.to_hash).must_equal(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
_(album_representer.to_hash).must_equal(
|
|
29
|
+
{
|
|
30
|
+
"static" => "static",
|
|
31
|
+
"symbol" => "symbol",
|
|
32
|
+
"proc" => "proc",
|
|
33
|
+
"callable" => "callable"
|
|
34
|
+
}
|
|
35
|
+
)
|
|
34
36
|
end
|
|
35
37
|
end
|
|
36
38
|
end
|
data/test/parse_pipeline_test.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
require "test_helper"
|
|
2
2
|
|
|
3
|
-
class ParsePipelineTest <
|
|
3
|
+
class ParsePipelineTest < Minitest::Spec
|
|
4
4
|
Album = Struct.new(:id, :artist, :songs)
|
|
5
5
|
Artist = Struct.new(:email)
|
|
6
6
|
Song = Struct.new(:title)
|
|
@@ -8,14 +8,14 @@ class ParsePipelineTest < MiniTest::Spec
|
|
|
8
8
|
describe "transforming nil to [] when parsing" do
|
|
9
9
|
representer!(decorator: true) do
|
|
10
10
|
collection :songs,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
parse_pipeline: ->(*) {
|
|
12
|
+
Representable::Pipeline.insert(
|
|
13
|
+
parse_functions, # original function list from Binding#parse_functions.
|
|
14
|
+
->(input, _options) { input.nil? ? [] : input }, # your new function (can be any callable object)..
|
|
15
|
+
replace: Representable::OverwriteOnNil # ..that replaces the original function.
|
|
16
|
+
)
|
|
17
|
+
},
|
|
18
|
+
class: Song do
|
|
19
19
|
property :title
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -31,7 +31,6 @@ class ParsePipelineTest < MiniTest::Spec
|
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
|
|
35
34
|
# tests [Collect[Instance, Prepare, Deserialize], Setter]
|
|
36
35
|
class Representer < Representable::Decorator
|
|
37
36
|
include Representable::Hash
|
|
@@ -40,23 +39,30 @@ class ParsePipelineTest < MiniTest::Spec
|
|
|
40
39
|
# property :email
|
|
41
40
|
# end
|
|
42
41
|
# DISCUSS: rename to populator_pipeline ?
|
|
43
|
-
collection :songs, parse_pipeline: ->(*) {
|
|
42
|
+
collection :songs, parse_pipeline: ->(*) {
|
|
43
|
+
[Collect[Instance, Prepare, Deserialize], Setter]
|
|
44
|
+
}, instance: :instance!, exec_context: :decorator, pass_options: true do
|
|
44
45
|
property :title
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
def instance!(*
|
|
48
|
+
def instance!(*_options)
|
|
48
49
|
Song.new
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
def songs=(array)
|
|
52
|
-
represented.songs=array
|
|
53
|
+
represented.songs = array
|
|
53
54
|
end
|
|
54
55
|
end
|
|
55
56
|
|
|
56
57
|
it do
|
|
57
58
|
skip "TODO: implement :parse_pipeline and :render_pipeline, and before/after/replace semantics"
|
|
58
59
|
album = Album.new
|
|
59
|
-
Representer.new(album).from_hash(
|
|
60
|
+
Representer.new(album).from_hash(
|
|
61
|
+
{
|
|
62
|
+
"artist" => {"email"=>"yo"},
|
|
63
|
+
"songs" => [{"title"=>"Affliction"}, {"title"=>"Dream Beater"}]
|
|
64
|
+
}
|
|
65
|
+
)
|
|
60
66
|
_(album.songs).must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
|
|
61
67
|
end
|
|
62
|
-
end
|
|
68
|
+
end
|