representable 2.4.0.rc3 → 2.4.0.rc4
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 +7 -2
- data/Rakefile +6 -0
- data/lib/representable.rb +21 -17
- data/lib/representable/binding.rb +3 -2
- data/lib/representable/definition.rb +1 -1
- data/lib/representable/deprecations.rb +31 -5
- data/lib/representable/deserializer.rb +24 -34
- data/lib/representable/hash/collection.rb +9 -2
- data/lib/representable/hash_methods.rb +2 -2
- data/lib/representable/parse_strategies.rb +6 -7
- data/lib/representable/pipeline.rb +12 -1
- data/lib/representable/pipeline_factories.rb +9 -2
- data/lib/representable/serializer.rb +3 -3
- data/lib/representable/version.rb +1 -1
- data/test-with-deprecations/as_test.rb +65 -0
- data/test-with-deprecations/benchmarking.rb +83 -0
- data/test-with-deprecations/binding_test.rb +46 -0
- data/test-with-deprecations/blaaaaaaaa_test.rb +69 -0
- data/test-with-deprecations/cached_test.rb +147 -0
- data/test-with-deprecations/class_test.rb +119 -0
- data/test-with-deprecations/coercion_test.rb +52 -0
- data/test-with-deprecations/config/inherit_test.rb +135 -0
- data/test-with-deprecations/config_test.rb +122 -0
- data/test-with-deprecations/decorator_scope_test.rb +28 -0
- data/test-with-deprecations/decorator_test.rb +96 -0
- data/test-with-deprecations/default_test.rb +34 -0
- data/test-with-deprecations/defaults_options_test.rb +93 -0
- data/test-with-deprecations/definition_test.rb +264 -0
- data/test-with-deprecations/example.rb +310 -0
- data/test-with-deprecations/examples/object.rb +31 -0
- data/test-with-deprecations/exec_context_test.rb +93 -0
- data/test-with-deprecations/features_test.rb +70 -0
- data/test-with-deprecations/filter_test.rb +57 -0
- data/test-with-deprecations/for_collection_test.rb +74 -0
- data/test-with-deprecations/generic_test.rb +116 -0
- data/test-with-deprecations/getter_setter_test.rb +21 -0
- data/test-with-deprecations/hash_bindings_test.rb +87 -0
- data/test-with-deprecations/hash_test.rb +160 -0
- data/test-with-deprecations/heritage_test.rb +62 -0
- data/test-with-deprecations/if_test.rb +79 -0
- data/test-with-deprecations/include_exclude_test.rb +88 -0
- data/test-with-deprecations/inherit_test.rb +159 -0
- data/test-with-deprecations/inline_test.rb +272 -0
- data/test-with-deprecations/instance_test.rb +266 -0
- data/test-with-deprecations/is_representable_test.rb +77 -0
- data/test-with-deprecations/json_test.rb +355 -0
- data/test-with-deprecations/lonely_test.rb +239 -0
- data/test-with-deprecations/mongoid_test.rb +31 -0
- data/test-with-deprecations/nested_test.rb +115 -0
- data/test-with-deprecations/object_test.rb +60 -0
- data/{test/---deserialize-pipeline_test.rb → test-with-deprecations/parse_pipeline_test.rb} +29 -2
- data/test-with-deprecations/parse_strategy_test.rb +279 -0
- data/{test → test-with-deprecations}/pass_options_test.rb +0 -0
- data/test-with-deprecations/pipeline_test.rb +277 -0
- data/test-with-deprecations/populator_test.rb +105 -0
- data/test-with-deprecations/prepare_test.rb +67 -0
- data/test-with-deprecations/private_options_test.rb +18 -0
- data/test-with-deprecations/reader_writer_test.rb +19 -0
- data/test-with-deprecations/realistic_benchmark.rb +115 -0
- data/test-with-deprecations/render_nil_test.rb +21 -0
- data/test-with-deprecations/represent_test.rb +88 -0
- data/test-with-deprecations/representable_test.rb +511 -0
- data/test-with-deprecations/schema_test.rb +148 -0
- data/test-with-deprecations/serialize_deserialize_test.rb +33 -0
- data/test-with-deprecations/skip_test.rb +81 -0
- data/test-with-deprecations/stringify_hash_test.rb +41 -0
- data/test-with-deprecations/test_helper.rb +135 -0
- data/test-with-deprecations/test_helper_test.rb +25 -0
- data/test-with-deprecations/uncategorized_test.rb +67 -0
- data/test-with-deprecations/user_options_test.rb +15 -0
- data/test-with-deprecations/wrap_test.rb +152 -0
- data/test-with-deprecations/xml_bindings_test.rb +62 -0
- data/test-with-deprecations/xml_test.rb +503 -0
- data/test-with-deprecations/yaml_test.rb +162 -0
- data/test/as_test.rb +3 -3
- data/test/cached_test.rb +2 -2
- data/test/class_test.rb +5 -5
- data/test/exec_context_test.rb +2 -2
- data/test/filter_test.rb +1 -1
- data/test/getter_setter_test.rb +4 -4
- data/test/if_test.rb +2 -2
- data/test/include_exclude_test.rb +88 -0
- data/test/instance_test.rb +15 -15
- data/test/lonely_test.rb +18 -2
- data/test/object_test.rb +4 -4
- data/test/parse_pipeline_test.rb +64 -0
- data/test/parse_strategy_test.rb +3 -3
- data/test/pipeline_test.rb +8 -12
- data/test/prepare_test.rb +2 -3
- data/test/reader_writer_test.rb +3 -3
- data/test/representable_test.rb +12 -48
- data/test/serialize_deserialize_test.rb +9 -9
- data/test/skip_test.rb +11 -11
- data/test/test_helper.rb +2 -0
- data/test/uncategorized_test.rb +10 -10
- data/test/user_options_test.rb +15 -0
- data/test/wrap_test.rb +1 -1
- metadata +65 -4
data/test/lonely_test.rb
CHANGED
@@ -112,7 +112,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
112
112
|
end
|
113
113
|
|
114
114
|
|
115
|
-
it { [1,2].extend(representer).to_hash(one: One, two: Two).must_equal(["One: 1", "Two: 2"]) }
|
115
|
+
it { [1,2].extend(representer).to_hash(user_options: {one: One, two: Two}).must_equal(["One: 1", "Two: 2"]) }
|
116
116
|
end
|
117
117
|
|
118
118
|
|
@@ -158,7 +158,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
158
158
|
assert_equal({"two" => Song.new("Can't Take Them All")}, {}.extend(representer).from_json(json, :exclude => [:one]))
|
159
159
|
end
|
160
160
|
|
161
|
-
it "
|
161
|
+
it "respects :include" do
|
162
162
|
assert_equal({"one" => Song.new("Days Go By")}, {}.extend(representer).from_json(json, :include => [:one]))
|
163
163
|
end
|
164
164
|
end
|
@@ -221,3 +221,19 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
end
|
224
|
+
|
225
|
+
|
226
|
+
# describe "Hash::Collection with :include" do
|
227
|
+
class CollectionWithIncludeTest < MiniTest::Spec
|
228
|
+
Song = Struct.new(:id, :title)
|
229
|
+
|
230
|
+
representer!(decorator: true, module: Representable::Hash::Collection) do
|
231
|
+
items do
|
232
|
+
property :id
|
233
|
+
property :title
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
it { representer.new([Song.new(1, "ACAB")]).to_hash.must_equal([{"id"=>1, "title"=>"ACAB"}]) }
|
238
|
+
it { representer.new([Song.new(1, "ACAB")]).to_hash(include: [:title]).must_equal([{"title"=>"ACAB"}]) }
|
239
|
+
end
|
data/test/object_test.rb
CHANGED
@@ -8,10 +8,10 @@ class ObjectTest < MiniTest::Spec
|
|
8
8
|
representer!(module: Representable::Object) do
|
9
9
|
property :title
|
10
10
|
|
11
|
-
property :album, instance: lambda { |
|
11
|
+
property :album, instance: lambda { |options| options[:fragment].name.upcase!; options[:fragment] } do
|
12
12
|
property :name
|
13
13
|
|
14
|
-
collection :songs, instance: lambda { |
|
14
|
+
collection :songs, instance: lambda { |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: lambda { |
|
45
|
+
property :album, render_filter: lambda { |input, options|input.name = "Live";input } do
|
46
46
|
property :name
|
47
47
|
|
48
|
-
collection :songs, render_filter: lambda { |
|
48
|
+
collection :songs, render_filter: lambda { |input, options|input[0].title = 1;input } do
|
49
49
|
property :title
|
50
50
|
end
|
51
51
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ParsePipelineTest < MiniTest::Spec
|
4
|
+
Album = Struct.new(:id, :artist, :songs)
|
5
|
+
Artist = Struct.new(:email)
|
6
|
+
Song = Struct.new(:title)
|
7
|
+
|
8
|
+
describe "transforming nil to [] when parsing" do
|
9
|
+
representer!(decorator: true) do
|
10
|
+
collection :songs,
|
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
|
+
property :title
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it do
|
24
|
+
representer.new(album = Album.new).from_hash("songs"=>nil)
|
25
|
+
album.songs.must_equal []
|
26
|
+
end
|
27
|
+
|
28
|
+
it do
|
29
|
+
representer.new(album = Album.new).from_hash("songs"=>[{"title" => "Business Conduct"}])
|
30
|
+
album.songs.must_equal [Song.new("Business Conduct")]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# tests [Collect[Instance, Prepare, Deserialize], Setter]
|
36
|
+
class Representer < Representable::Decorator
|
37
|
+
include Representable::Hash
|
38
|
+
|
39
|
+
# property :artist, populator: Uber::Options::Value.new(ArtistPopulator.new), pass_options:true do
|
40
|
+
# property :email
|
41
|
+
# end
|
42
|
+
# DISCUSS: rename to populator_pipeline ?
|
43
|
+
collection :songs, parse_pipeline: ->(*) { [Collect[Instance, Prepare, Deserialize], Setter] }, instance: :instance!, exec_context: :decorator, pass_options: true do
|
44
|
+
property :title
|
45
|
+
end
|
46
|
+
|
47
|
+
def instance!(*options)
|
48
|
+
puts "@@@@@ #{options.inspect}"
|
49
|
+
Song.new
|
50
|
+
end
|
51
|
+
|
52
|
+
def songs=(array)
|
53
|
+
represented.songs=array
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it do
|
58
|
+
skip "TODO: implement :parse_pipeline and :render_pipeline, and before/after/replace semantics"
|
59
|
+
album = Album.new
|
60
|
+
Representer.new(album).from_hash({"artist"=>{"email"=>"yo"}, "songs"=>[{"title"=>"Affliction"}, {"title"=>"Dream Beater"}]})
|
61
|
+
album.songs.must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
|
62
|
+
puts album.inspect
|
63
|
+
end
|
64
|
+
end
|
data/test/parse_strategy_test.rb
CHANGED
@@ -208,7 +208,7 @@ class ParseStrategyFindOrInstantiateTest < BaseTest
|
|
208
208
|
describe "property with dynamic :class" do
|
209
209
|
representer!(:inject => :song_representer) do
|
210
210
|
property :song, :parse_strategy => :find_or_instantiate, :extend => song_representer,
|
211
|
-
:class => lambda { |
|
211
|
+
:class => lambda { |options| options[:fragment]["class"] }
|
212
212
|
end
|
213
213
|
|
214
214
|
let (:album) { Struct.new(:song).new.extend(representer) }
|
@@ -241,7 +241,7 @@ class ParseStrategyLambdaTest < MiniTest::Spec
|
|
241
241
|
describe "property parse_strategy: lambda, representable: false" do
|
242
242
|
representer! do
|
243
243
|
property :title,
|
244
|
-
:instance => lambda { |
|
244
|
+
:instance => lambda { |options| options[:fragment].to_s }, # this will still call song.title= "8675309".
|
245
245
|
:representable => false # don't call object.from_hash
|
246
246
|
end
|
247
247
|
|
@@ -252,7 +252,7 @@ class ParseStrategyLambdaTest < MiniTest::Spec
|
|
252
252
|
|
253
253
|
describe "collection" do
|
254
254
|
representer!(:inject => :song_representer) do
|
255
|
-
collection :songs, :parse_strategy => lambda { |
|
255
|
+
collection :songs, :parse_strategy => lambda { |options|
|
256
256
|
songs << song = Song.new
|
257
257
|
song
|
258
258
|
}, :extend => song_representer
|
data/test/pipeline_test.rb
CHANGED
@@ -119,12 +119,11 @@ class PipelineTest < MiniTest::Spec
|
|
119
119
|
R::GetValue,
|
120
120
|
R::StopOnSkipable,
|
121
121
|
R::StopOnNil,
|
122
|
-
R::SkipRender,
|
123
122
|
R::Decorate,
|
124
123
|
R::Serialize,
|
125
124
|
R::AssignName,
|
126
125
|
R::WriteFragment
|
127
|
-
].extend(P::Debug).(nil, {represented: song_model, binding: artist, doc: doc,
|
126
|
+
].extend(P::Debug).(nil, {represented: song_model, binding: artist, doc: doc, options: {}}).must_equal({"name" => "Diesel Boy"})
|
128
127
|
|
129
128
|
doc.must_equal({"artist"=>{"name"=>"Diesel Boy"}})
|
130
129
|
end
|
@@ -135,13 +134,12 @@ class PipelineTest < MiniTest::Spec
|
|
135
134
|
R::ReadFragment,
|
136
135
|
R::StopOnNotFound,
|
137
136
|
R::OverwriteOnNil,
|
138
|
-
# R::SkipParse,
|
139
137
|
R::AssignFragment,
|
140
|
-
R::CreateObject,
|
138
|
+
R::CreateObject::Class,
|
141
139
|
R::Decorate,
|
142
140
|
R::Deserialize,
|
143
141
|
R::SetValue,
|
144
|
-
].extend(P::Debug).(doc={"artist"=>{"name"=>"Doobie Brothers"}}, {represented: song_model, binding: artist, doc: doc,
|
142
|
+
].extend(P::Debug).(doc={"artist"=>{"name"=>"Doobie Brothers"}}, {represented: song_model, binding: artist, doc: doc, options: {}}).must_equal model=Artist.new("Doobie Brothers")
|
145
143
|
song_model.artist.must_equal model
|
146
144
|
end
|
147
145
|
|
@@ -149,7 +147,7 @@ class PipelineTest < MiniTest::Spec
|
|
149
147
|
######### collection :ratings
|
150
148
|
|
151
149
|
let (:ratings) {
|
152
|
-
dfn = R::Definition.new(:ratings, collection: true)
|
150
|
+
dfn = R::Definition.new(:ratings, collection: true, skip_render: ->(*) { false })
|
153
151
|
|
154
152
|
R::Hash::Binding::Collection.new(dfn)
|
155
153
|
}
|
@@ -163,7 +161,7 @@ class PipelineTest < MiniTest::Spec
|
|
163
161
|
],
|
164
162
|
R::AssignName,
|
165
163
|
R::WriteFragment
|
166
|
-
].extend(P::Debug).(nil, {represented: Album.new([1,2,3]), binding: ratings, doc: doc}).must_equal([1,2,3])
|
164
|
+
].extend(P::Debug).(nil, {represented: Album.new([1,2,3]), binding: ratings, doc: doc, options: {}}).must_equal([1,2,3])
|
167
165
|
|
168
166
|
doc.must_equal({"ratings"=>[1,2,3]})
|
169
167
|
end
|
@@ -180,13 +178,12 @@ class PipelineTest < MiniTest::Spec
|
|
180
178
|
R::GetValue,
|
181
179
|
R::StopOnSkipable,
|
182
180
|
R::Collect[
|
183
|
-
R::SkipRender,
|
184
181
|
R::Decorate,
|
185
182
|
R::Serialize,
|
186
183
|
],
|
187
184
|
R::AssignName,
|
188
185
|
R::WriteFragment
|
189
|
-
].extend(P::Debug).(nil, {represented: Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]), binding: artists, doc: doc,
|
186
|
+
].extend(P::Debug).(nil, {represented: Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]), binding: artists, doc: doc, options: {}}).must_equal([{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}])
|
190
187
|
|
191
188
|
doc.must_equal({"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]})
|
192
189
|
end
|
@@ -203,13 +200,12 @@ let (:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van H
|
|
203
200
|
# R::SkipParse,
|
204
201
|
R::Collect[
|
205
202
|
R::AssignFragment,
|
206
|
-
R::
|
207
|
-
R::CreateObject,
|
203
|
+
R::CreateObject::Class,
|
208
204
|
R::Decorate,
|
209
205
|
R::Deserialize,
|
210
206
|
],
|
211
207
|
R::SetValue,
|
212
|
-
].extend(P::Debug).(doc, {represented: album_model, binding: artists, doc: doc,
|
208
|
+
].extend(P::Debug).(doc, {represented: album_model, binding: artists, doc: doc, options: {}}).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
213
209
|
|
214
210
|
album_model.artists.must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
215
211
|
end
|
data/test/prepare_test.rb
CHANGED
@@ -18,10 +18,9 @@ class PrepareTest < BaseTest
|
|
18
18
|
describe "#to_hash" do # TODO: introduce :representable option?
|
19
19
|
representer! do
|
20
20
|
property :song,
|
21
|
-
:prepare => lambda { |
|
21
|
+
:prepare => lambda { |options| options[:binding][:arbitrary].new(options[:input]) },
|
22
22
|
:arbitrary => PreparerClass,
|
23
23
|
:extend => true,
|
24
|
-
:pass_options => true,
|
25
24
|
:representable => false # don't call #to_hash.
|
26
25
|
end
|
27
26
|
|
@@ -47,7 +46,7 @@ class PrepareTest < BaseTest
|
|
47
46
|
describe "#from_hash" do
|
48
47
|
representer! do
|
49
48
|
property :song,
|
50
|
-
:prepare => lambda { |
|
49
|
+
:prepare => lambda { |options| options[:binding][:arbitrary].new(options[:input]) },
|
51
50
|
:arbitrary => PreparerClass,
|
52
51
|
#:extend => true, # TODO: typed: true would be better.
|
53
52
|
:instance => String.new, # pass_fragment
|
data/test/reader_writer_test.rb
CHANGED
@@ -3,14 +3,14 @@ require 'test_helper'
|
|
3
3
|
class ReaderWriterTest < BaseTest
|
4
4
|
representer! do
|
5
5
|
property :name,
|
6
|
-
:writer => lambda { |
|
7
|
-
:reader => lambda { |
|
6
|
+
:writer => lambda { |options| options[:doc]["title"] = "#{options[:user_options][:nr]}) #{options[:input]}" },
|
7
|
+
:reader => lambda { |options| self.name = options[:doc]["title"].split(") ").last }
|
8
8
|
end
|
9
9
|
|
10
10
|
subject { OpenStruct.new(:name => "Disorder And Disarray").extend(representer) }
|
11
11
|
|
12
12
|
it "uses :writer when rendering" do
|
13
|
-
subject.to_hash(:nr
|
13
|
+
subject.to_hash(user_options: {nr: 14}).must_equal({"title" => "14) Disorder And Disarray"})
|
14
14
|
end
|
15
15
|
|
16
16
|
it "uses :reader when parsing" do
|
data/test/representable_test.rb
CHANGED
@@ -222,19 +222,6 @@ class RepresentableTest < MiniTest::Spec
|
|
222
222
|
assert_equal 2, @band.groupies
|
223
223
|
end
|
224
224
|
|
225
|
-
|
226
|
-
it "accepts :exclude option" do
|
227
|
-
@band.from_hash({"name"=>"No One's Choice", "groupies"=>2}, {:exclude => [:groupies]})
|
228
|
-
assert_equal "No One's Choice", @band.name
|
229
|
-
assert_equal nil, @band.groupies
|
230
|
-
end
|
231
|
-
|
232
|
-
it "accepts :include option" do
|
233
|
-
@band.from_hash({"name"=>"No One's Choice", "groupies"=>2}, :include => [:groupies])
|
234
|
-
assert_equal 2, @band.groupies
|
235
|
-
assert_equal nil, @band.name
|
236
|
-
end
|
237
|
-
|
238
225
|
it "ignores non-writeable properties" do
|
239
226
|
@band = Class.new(Band) { property :name; collection :founders, :writeable => false; attr_accessor :founders }.new
|
240
227
|
@band.from_hash("name" => "Iron Maiden", "groupies" => 2, "founders" => ["Steve Harris"])
|
@@ -292,23 +279,23 @@ class RepresentableTest < MiniTest::Spec
|
|
292
279
|
|
293
280
|
property :length, class: OpenStruct do
|
294
281
|
def to_hash(options)
|
295
|
-
{seconds: options[:nr]}
|
282
|
+
{seconds: options[:user_options][:nr]}
|
296
283
|
end
|
297
284
|
|
298
285
|
def from_hash(hash, options)
|
299
286
|
super.tap do
|
300
|
-
self.seconds = options[:nr]
|
287
|
+
self.seconds = options[:user_options][:nr]
|
301
288
|
end
|
302
289
|
end
|
303
290
|
end
|
304
291
|
|
305
292
|
def to_hash(options)
|
306
|
-
super.merge({"nr" => options[:nr]})
|
293
|
+
super.merge({"nr" => options[:user_options][:nr]})
|
307
294
|
end
|
308
295
|
|
309
296
|
def from_hash(data, options)
|
310
297
|
super.tap do
|
311
|
-
self.nr = options[:nr]
|
298
|
+
self.nr = options[:user_options][:nr]
|
312
299
|
end
|
313
300
|
end
|
314
301
|
end
|
@@ -316,11 +303,11 @@ class RepresentableTest < MiniTest::Spec
|
|
316
303
|
|
317
304
|
it "#to_hash propagates to nested objects" do
|
318
305
|
OpenStruct.new(track: OpenStruct.new(nr: 1, length: OpenStruct.new(seconds: nil))).extend(representer).extend(Representable::Debug).
|
319
|
-
to_hash(nr: 9).must_equal({"track"=>{"nr"=>9, "length"=>{seconds: 9}}})
|
306
|
+
to_hash(user_options: {nr: 9}).must_equal({"track"=>{"nr"=>9, "length"=>{seconds: 9}}})
|
320
307
|
end
|
321
308
|
|
322
309
|
it "#from_hash propagates to nested objects" do
|
323
|
-
song = OpenStruct.new.extend(representer).from_hash({"track"=>{"nr" => "replace me", "length"=>{"seconds"=>"replacing"}}}, :nr
|
310
|
+
song = OpenStruct.new.extend(representer).from_hash({"track"=>{"nr" => "replace me", "length"=>{"seconds"=>"replacing"}}}, user_options: {nr: 9})
|
324
311
|
song.track.nr.must_equal 9
|
325
312
|
song.track.length.seconds.must_equal 9
|
326
313
|
end
|
@@ -338,16 +325,6 @@ class RepresentableTest < MiniTest::Spec
|
|
338
325
|
assert_equal({"name"=>"No One's Choice", "groupies"=>2}, @band.to_hash)
|
339
326
|
end
|
340
327
|
|
341
|
-
it "accepts :exclude option" do
|
342
|
-
hash = @band.to_hash({:exclude => [:groupies]})
|
343
|
-
assert_equal({"name"=>"No One's Choice"}, hash)
|
344
|
-
end
|
345
|
-
|
346
|
-
it "accepts :include option" do
|
347
|
-
hash = @band.to_hash({:include => [:groupies]})
|
348
|
-
assert_equal({"groupies"=>2}, hash)
|
349
|
-
end
|
350
|
-
|
351
328
|
it "ignores non-readable properties" do
|
352
329
|
@band = Class.new(Band) { property :name; collection :founder_ids, :readable => false; attr_accessor :founder_ids }.new
|
353
330
|
@band.name = "Iron Maiden"
|
@@ -366,19 +343,6 @@ class RepresentableTest < MiniTest::Spec
|
|
366
343
|
@band.groupies = false
|
367
344
|
assert_equal({"name"=>"No One's Choice","groupies"=>false}, @band.to_hash)
|
368
345
|
end
|
369
|
-
|
370
|
-
it "does not propagate private options to nested objects" do
|
371
|
-
cover_rpr = Module.new do
|
372
|
-
include Representable::Hash
|
373
|
-
property :title
|
374
|
-
property :original, :extend => self
|
375
|
-
end
|
376
|
-
|
377
|
-
# FIXME: we should test all representable-options (:include, :exclude, ?)
|
378
|
-
|
379
|
-
Class.new(OpenStruct).new(:title => "Roxanne", :original => Class.new(OpenStruct).new(:title => "Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
380
|
-
to_hash(:include => [:original]).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
381
|
-
end
|
382
346
|
end
|
383
347
|
|
384
348
|
|
@@ -427,7 +391,7 @@ class RepresentableTest < MiniTest::Spec
|
|
427
391
|
|
428
392
|
describe "property with :extend" do
|
429
393
|
representer! do
|
430
|
-
property :name, :extend => lambda { |
|
394
|
+
property :name, :extend => lambda { |options| options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter }, :class => String
|
431
395
|
end
|
432
396
|
|
433
397
|
it "uses lambda when rendering" do
|
@@ -442,8 +406,8 @@ class RepresentableTest < MiniTest::Spec
|
|
442
406
|
|
443
407
|
describe "with :class lambda" do
|
444
408
|
representer! do
|
445
|
-
property :name, :extend => lambda { |
|
446
|
-
:class => lambda { |
|
409
|
+
property :name, :extend => lambda { |options| options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter },
|
410
|
+
:class => lambda { |options| options[:fragment] == "Still Failing?" ? String : UpcaseString }
|
447
411
|
end
|
448
412
|
|
449
413
|
it "creates instance from :class lambda when parsing" do
|
@@ -461,7 +425,7 @@ class RepresentableTest < MiniTest::Spec
|
|
461
425
|
|
462
426
|
describe "collection with :extend" do
|
463
427
|
representer! do
|
464
|
-
collection :songs, :extend => lambda { |
|
428
|
+
collection :songs, :extend => lambda { |options| options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter }, :class => String
|
465
429
|
end
|
466
430
|
|
467
431
|
it "uses lambda for each item when rendering" do
|
@@ -475,8 +439,8 @@ class RepresentableTest < MiniTest::Spec
|
|
475
439
|
|
476
440
|
describe "with :class lambda" do
|
477
441
|
representer! do
|
478
|
-
collection :songs, :extend => lambda { |
|
479
|
-
:class => lambda { |
|
442
|
+
collection :songs, :extend => lambda { |options| options[:input].is_a?(UpcaseString) ? UpcaseRepresenter : DowncaseRepresenter },
|
443
|
+
:class => lambda { |options| options[:input] == "Still Failing?" ? String : UpcaseString }
|
480
444
|
end
|
481
445
|
|
482
446
|
it "creates instance from :class lambda for each item when parsing" do
|
@@ -6,28 +6,28 @@ class SerializeDeserializeTest < BaseTest
|
|
6
6
|
describe "deserialize" do
|
7
7
|
representer! do
|
8
8
|
property :song,
|
9
|
-
:instance => lambda { |
|
10
|
-
:prepare => lambda { |
|
11
|
-
:deserialize => lambda { |
|
12
|
-
"#{
|
9
|
+
:instance => lambda { |options| options[:input].to_s.upcase },
|
10
|
+
:prepare => lambda { |options| options[:input] },
|
11
|
+
:deserialize => lambda { |options|
|
12
|
+
"#{options[:input]} #{options[:fragment]} #{options[:user_options].inspect}"
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
16
|
-
it { subject.from_hash({"song" => Object}, {:
|
16
|
+
it { subject.from_hash({"song" => Object}, user_options: {volume: 9}).song.must_equal "OBJECT Object {:volume=>9}" }
|
17
17
|
end
|
18
18
|
|
19
19
|
describe "serialize" do
|
20
20
|
representer! do
|
21
21
|
property :song,
|
22
22
|
:representable => true,
|
23
|
-
:prepare => lambda { |
|
24
|
-
:serialize => lambda { |
|
25
|
-
"#{
|
23
|
+
:prepare => lambda { |options| options[:fragment] },
|
24
|
+
:serialize => lambda { |options|
|
25
|
+
"#{options[:input]} #{options[:user_options].inspect}"
|
26
26
|
}
|
27
27
|
end
|
28
28
|
|
29
29
|
before { subject.song = "Arrested In Shanghai" }
|
30
30
|
|
31
|
-
it { subject.to_hash({:
|
31
|
+
it { subject.to_hash(user_options: {volume: 9}).must_equal({"song"=>"Arrested In Shanghai {:volume=>9}"}) }
|
32
32
|
end
|
33
33
|
end
|