representable 3.0.0 → 3.2.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 +5 -5
- data/.github/workflows/ci.yml +23 -0
- data/CHANGES.md +33 -0
- data/Gemfile +4 -12
- data/LICENSE +1 -1
- data/README.md +12 -13
- data/Rakefile +1 -6
- data/TODO +1 -3
- data/TODO-4.0.md +72 -0
- data/lib/representable/binding.rb +32 -12
- data/lib/representable/cached.rb +1 -1
- data/lib/representable/coercion.rb +8 -6
- data/lib/representable/config.rb +13 -3
- data/lib/representable/debug.rb +23 -15
- data/lib/representable/declarative.rb +12 -7
- data/lib/representable/decorator.rb +1 -1
- data/lib/representable/definition.rb +7 -3
- data/lib/representable/deserializer.rb +5 -4
- data/lib/representable/for_collection.rb +1 -1
- data/lib/representable/hash/allow_symbols.rb +9 -11
- data/lib/representable/hash/binding.rb +1 -0
- data/lib/representable/hash/collection.rb +4 -2
- data/lib/representable/hash.rb +9 -2
- data/lib/representable/hash_methods.rb +3 -2
- data/lib/representable/insert.rb +1 -1
- data/lib/representable/json/collection.rb +3 -0
- data/lib/representable/json.rb +8 -7
- data/lib/representable/object/binding.rb +5 -1
- data/lib/representable/object.rb +1 -1
- data/lib/representable/option.rb +19 -0
- data/lib/representable/pipeline.rb +3 -2
- data/lib/representable/pipeline_factories.rb +4 -2
- data/lib/representable/populator.rb +1 -1
- data/lib/representable/represent.rb +1 -0
- data/lib/representable/serializer.rb +3 -2
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml/binding.rb +19 -13
- data/lib/representable/xml/namespace.rb +122 -0
- data/lib/representable/xml.rb +12 -10
- data/lib/representable/yaml/binding.rb +1 -0
- data/lib/representable/yaml.rb +6 -2
- data/lib/representable.rb +19 -25
- data/representable.gemspec +8 -9
- data/test/as_test.rb +7 -7
- data/test/binding_test.rb +14 -14
- data/test/cached_test.rb +59 -49
- data/test/class_test.rb +9 -9
- data/test/coercion_test.rb +33 -22
- data/test/config/inherit_test.rb +14 -14
- data/test/config_test.rb +20 -20
- data/test/decorator_scope_test.rb +4 -4
- data/test/decorator_test.rb +33 -20
- data/test/default_test.rb +8 -8
- data/test/defaults_options_test.rb +3 -3
- data/test/definition_test.rb +38 -40
- data/test/{example.rb → examples/example.rb} +0 -1
- data/test/examples/object.rb +1 -5
- data/test/exec_context_test.rb +8 -8
- data/test/features_test.rb +6 -6
- data/test/filter_test.rb +8 -8
- data/test/for_collection_test.rb +10 -10
- data/test/generic_test.rb +13 -13
- data/test/getter_setter_test.rb +5 -5
- data/test/hash_bindings_test.rb +1 -1
- data/test/hash_test.rb +45 -23
- data/test/heritage_test.rb +16 -13
- data/test/if_test.rb +9 -9
- data/test/include_exclude_test.rb +14 -14
- data/test/inherit_test.rb +18 -18
- data/test/inline_test.rb +24 -24
- data/test/instance_test.rb +31 -31
- data/test/is_representable_test.rb +10 -10
- data/test/json_test.rb +29 -7
- data/test/lonely_test.rb +31 -31
- data/test/nested_test.rb +13 -13
- data/test/object_test.rb +9 -9
- data/test/option_test.rb +36 -0
- data/test/parse_pipeline_test.rb +3 -5
- data/test/pipeline_test.rb +50 -50
- data/test/populator_test.rb +18 -18
- data/test/prepare_test.rb +4 -4
- data/test/private_options_test.rb +2 -2
- data/test/reader_writer_test.rb +2 -2
- data/test/render_nil_test.rb +2 -2
- data/test/represent_test.rb +14 -14
- data/test/representable_test.rb +34 -36
- data/test/schema_test.rb +8 -11
- data/test/serialize_deserialize_test.rb +2 -2
- data/test/skip_test.rb +14 -14
- data/test/stringify_hash_test.rb +3 -3
- data/test/test_helper.rb +26 -14
- data/test/uncategorized_test.rb +10 -10
- data/test/user_options_test.rb +4 -4
- data/test/wrap_test.rb +19 -19
- data/test/xml_bindings_test.rb +0 -4
- data/test/xml_namespace_test.rb +186 -0
- data/test/xml_test.rb +103 -43
- data/test/yaml_test.rb +51 -26
- metadata +42 -35
- data/.travis.yml +0 -7
- data/lib/representable/TODO.getting_serious +0 -11
- data/lib/representable/autoload.rb +0 -10
- data/test/mongoid_test.rb +0 -31
data/test/inherit_test.rb
CHANGED
@@ -10,7 +10,7 @@ class InheritTest < MiniTest::Spec
|
|
10
10
|
property :track, :as => :no
|
11
11
|
end
|
12
12
|
|
13
|
-
let
|
13
|
+
let(:song) { Song.new(Struct.new(:string).new("Roxanne"), 1) }
|
14
14
|
|
15
15
|
describe ":inherit plain property" do
|
16
16
|
representer! do
|
@@ -19,8 +19,8 @@ class InheritTest < MiniTest::Spec
|
|
19
19
|
property :track, :inherit => true, :getter => lambda { |*| "n/a" }
|
20
20
|
end
|
21
21
|
|
22
|
-
it { SongRepresenter.prepare(song).to_hash.must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>1}) }
|
23
|
-
it { representer.prepare(song).to_hash.must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>"n/a"}) } # as: inherited.
|
22
|
+
it { _(SongRepresenter.prepare(song).to_hash).must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>1}) }
|
23
|
+
it { _(representer.prepare(song).to_hash).must_equal({"title"=>{"str"=>"Roxanne"}, "no"=>"n/a"}) } # as: inherited.
|
24
24
|
end
|
25
25
|
|
26
26
|
describe ":inherit with empty inline representer" do
|
@@ -32,23 +32,23 @@ class InheritTest < MiniTest::Spec
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
35
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
36
36
|
# the block doesn't override the inline representer.
|
37
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
37
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
38
38
|
end
|
39
39
|
|
40
40
|
describe ":inherit with overriding inline representer" do
|
41
41
|
representer! do
|
42
42
|
include SongRepresenter
|
43
43
|
|
44
|
-
|
44
|
+
# passing block
|
45
45
|
property :name, :inherit => true do # inherit as: title
|
46
46
|
property :string, :as => :s
|
47
47
|
property :length
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
it { representer.prepare( Song.new(Struct.new(:string, :length).new("Believe It", 10), 1)).to_hash.must_equal({"title"=>{"s"=>"Believe It","length"=>10}, "no"=>1}) }
|
51
|
+
it { _(representer.prepare( Song.new(Struct.new(:string, :length).new("Believe It", 10), 1)).to_hash).must_equal({"title"=>{"s"=>"Believe It","length"=>10}, "no"=>1}) }
|
52
52
|
end
|
53
53
|
|
54
54
|
describe ":inherit with empty inline and options" do
|
@@ -60,8 +60,8 @@ class InheritTest < MiniTest::Spec
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
64
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
63
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
64
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
65
65
|
end
|
66
66
|
|
67
67
|
describe ":inherit with inline without block but options" do
|
@@ -71,8 +71,8 @@ class InheritTest < MiniTest::Spec
|
|
71
71
|
property :name, :inherit => true, :as => :name # FIXME: add :getter or something else dynamic since this is double-wrapped.
|
72
72
|
end
|
73
73
|
|
74
|
-
it { SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
75
|
-
it { representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash.must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
74
|
+
it { _(SongRepresenter.prepare(Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"title"=>{"str"=>"Believe It"}, "no"=>1}) }
|
75
|
+
it { _(representer.prepare( Song.new(Struct.new(:string).new("Believe It"), 1)).to_hash).must_equal({"name"=>{"str"=>"Believe It"}, "no"=>1}) }
|
76
76
|
end
|
77
77
|
|
78
78
|
|
@@ -86,12 +86,12 @@ class InheritTest < MiniTest::Spec
|
|
86
86
|
end
|
87
87
|
|
88
88
|
it "replaces inherited property" do
|
89
|
-
representer.representable_attrs.size.must_equal 2
|
89
|
+
_(representer.representable_attrs.size).must_equal 2
|
90
90
|
|
91
91
|
definition = representer.representable_attrs.get(:track) # TODO: find a better way to assert Definition identity.
|
92
92
|
# definition.keys.size.must_equal 2
|
93
|
-
definition[:representable]. must_equal true
|
94
|
-
definition.name.must_equal "track" # was "no".
|
93
|
+
_(definition[:representable]). must_equal true
|
94
|
+
_(definition.name).must_equal "track" # was "no".
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -108,7 +108,7 @@ class InheritTest < MiniTest::Spec
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
let
|
111
|
+
let(:inheriting) {
|
112
112
|
class InheritingDecorator < representer
|
113
113
|
include Representable::Debug
|
114
114
|
property :hit, :inherit => true do
|
@@ -119,12 +119,12 @@ class InheritTest < MiniTest::Spec
|
|
119
119
|
end
|
120
120
|
}
|
121
121
|
|
122
|
-
it { representer.new(OpenStruct.new(hit: OpenStruct.new(title: "I WILL BE OVERRIDDEN", :length => "2:59"))).to_hash.must_equal(
|
122
|
+
it { _(representer.new(OpenStruct.new(hit: OpenStruct.new(title: "I WILL BE OVERRIDDEN", :length => "2:59"))).to_hash).must_equal(
|
123
123
|
{"hit"=>{"title"=>"Cheap Transistor Radio"}}) }
|
124
124
|
|
125
125
|
# inheriting decorator inherits inline representer class (InlineRepresenter#title).
|
126
126
|
# inheriting decorator adds :length.
|
127
|
-
it { inheriting.new(OpenStruct.new(:hit => OpenStruct.new(:title => "Hole In Your Soul", :length => "2:59"))).to_hash.must_equal(
|
127
|
+
it { _(inheriting.new(OpenStruct.new(:hit => OpenStruct.new(:title => "Hole In Your Soul", :length => "2:59"))).to_hash).must_equal(
|
128
128
|
{"hit"=>{"title"=>"Cheap Transistor Radio", "length"=>"2:59"}}) }
|
129
129
|
end
|
130
130
|
|
@@ -135,7 +135,7 @@ class InheritTest < MiniTest::Spec
|
|
135
135
|
property :name, :inherit => true
|
136
136
|
end
|
137
137
|
|
138
|
-
it { representer.prepare(Song.new("The Beginning")).to_hash.must_equal({"name"=>"The Beginning"})}
|
138
|
+
it { _(representer.prepare(Song.new("The Beginning")).to_hash).must_equal({"name"=>"The Beginning"})}
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
data/test/inline_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class InlineTest < MiniTest::Spec
|
4
|
-
let
|
5
|
-
let
|
4
|
+
let(:song) { Song.new("Alive") }
|
5
|
+
let(:request) { representer.prepare(OpenStruct.new(:song => song)) }
|
6
6
|
|
7
7
|
{
|
8
8
|
:hash => [Representable::Hash, {"song"=>{"name"=>"Alive"}}, {"song"=>{"name"=>"You've Taken Everything"}}],
|
@@ -19,10 +19,10 @@ class InlineTest < MiniTest::Spec
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
let
|
22
|
+
let(:format) { format }
|
23
23
|
|
24
24
|
it { render(request).must_equal_document output }
|
25
|
-
it { parse(request, input).song.name.must_equal "You've Taken Everything"}
|
25
|
+
it { _(parse(request, input).song.name).must_equal "You've Taken Everything"}
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -36,7 +36,7 @@ class InlineTest < MiniTest::Spec
|
|
36
36
|
collection_options ||= {}
|
37
37
|
|
38
38
|
describe "[#{format}] collection with :class" do
|
39
|
-
let
|
39
|
+
let(:request) { representer.prepare(OpenStruct.new(:songs => [song])) }
|
40
40
|
|
41
41
|
representer!(:module => mod) do
|
42
42
|
collection :songs, collection_options.merge(:class => Song) do
|
@@ -44,10 +44,10 @@ class InlineTest < MiniTest::Spec
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
let
|
47
|
+
let(:format) { format } # FIXME: why do we have to define this?
|
48
48
|
|
49
49
|
it { render(request).must_equal_document output }
|
50
|
-
it { parse(request, input).songs.first.name.must_equal "You've Taken Everything"}
|
50
|
+
it { _(parse(request, input).songs.first.name).must_equal "You've Taken Everything"}
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -58,7 +58,7 @@ class InlineTest < MiniTest::Spec
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
it { request.to_hash.must_equal({"song"=>{"name"=>"Alive"}}) }
|
61
|
+
it { _(request.to_hash).must_equal({"song"=>{"name"=>"Alive"}}) }
|
62
62
|
end
|
63
63
|
|
64
64
|
|
@@ -76,14 +76,14 @@ class InlineTest < MiniTest::Spec
|
|
76
76
|
end
|
77
77
|
|
78
78
|
it "doesn't change represented object" do
|
79
|
-
request.send("from_#{format}", input).song.must_equal song
|
79
|
+
_(request.send("from_#{format}", input).song).must_equal song
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
84
|
|
85
85
|
describe "inheriting from outer representer" do
|
86
|
-
let
|
86
|
+
let(:request) { Struct.new(:song, :requester).new(song, "Josephine") }
|
87
87
|
|
88
88
|
[false, true].each do |is_decorator| # test for module and decorator.
|
89
89
|
representer!(:decorator => is_decorator) do
|
@@ -94,10 +94,10 @@ class InlineTest < MiniTest::Spec
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
-
let
|
97
|
+
let(:decorator) { representer.prepare(request) }
|
98
98
|
|
99
|
-
it { decorator.to_hash.must_equal({"requester"=>"Josephine", "song"=>{"name"=>"Alive"}}) }
|
100
|
-
it { decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name.must_equal "You've Taken Everything"}
|
99
|
+
it { _(decorator.to_hash).must_equal({"requester"=>"Josephine", "song"=>{"name"=>"Alive"}}) }
|
100
|
+
it { _(decorator.from_hash({"song"=>{"name"=>"You've Taken Everything"}}).song.name).must_equal "You've Taken Everything"}
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
@@ -110,7 +110,7 @@ class InlineTest < MiniTest::Spec
|
|
110
110
|
|
111
111
|
it "uses an inline decorator and doesn't alter represented" do
|
112
112
|
representer.prepare(Struct.new(:song).new(song)).to_hash
|
113
|
-
song.wont_be_kind_of Representable
|
113
|
+
_(song).wont_be_kind_of Representable
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
@@ -128,7 +128,7 @@ class InlineTest < MiniTest::Spec
|
|
128
128
|
# end
|
129
129
|
|
130
130
|
# describe ":getter with inline representer" do
|
131
|
-
# let
|
131
|
+
# let(:format) { format }
|
132
132
|
|
133
133
|
# representer!(:module => mod) do
|
134
134
|
# self.representation_wrap = :album
|
@@ -136,7 +136,7 @@ class InlineTest < MiniTest::Spec
|
|
136
136
|
# property :artist, :getter => lambda { |args| represented }, :extend => ArtistRepresenter
|
137
137
|
# end
|
138
138
|
|
139
|
-
# let
|
139
|
+
# let(:album) { OpenStruct.new(:label => "Epitaph").extend(representer) }
|
140
140
|
|
141
141
|
# it "renders nested Album-properties in separate section" do
|
142
142
|
# render(album).must_equal_document output
|
@@ -157,7 +157,7 @@ class InlineTest < MiniTest::Spec
|
|
157
157
|
end
|
158
158
|
|
159
159
|
describe ":getter with :decorator" do
|
160
|
-
let
|
160
|
+
let(:format) { format }
|
161
161
|
|
162
162
|
representer!(:module => mod) do
|
163
163
|
self.representation_wrap = "album"
|
@@ -165,7 +165,7 @@ class InlineTest < MiniTest::Spec
|
|
165
165
|
property :artist, :getter => lambda { |args| represented }, :decorator => ArtistDecorator
|
166
166
|
end
|
167
167
|
|
168
|
-
let
|
168
|
+
let(:album) { OpenStruct.new(:label => "Epitaph").extend(representer) }
|
169
169
|
|
170
170
|
it "renders nested Album-properties in separate section" do
|
171
171
|
render(album).must_equal_document output
|
@@ -182,7 +182,7 @@ class InlineTest < MiniTest::Spec
|
|
182
182
|
}) do |format, mod, output|
|
183
183
|
|
184
184
|
describe "helper method within inline representer [#{format}]" do
|
185
|
-
let
|
185
|
+
let(:format) { format }
|
186
186
|
|
187
187
|
representer!(:module => mod, :decorator => true) do
|
188
188
|
self.representation_wrap = :request if format == :xml
|
@@ -199,7 +199,7 @@ class InlineTest < MiniTest::Spec
|
|
199
199
|
end
|
200
200
|
end
|
201
201
|
|
202
|
-
let
|
202
|
+
let(:request) { representer.prepare(OpenStruct.new(:song => Song.new("Alive"))) }
|
203
203
|
|
204
204
|
it do
|
205
205
|
render(request).must_equal_document output
|
@@ -221,8 +221,8 @@ class InlineTest < MiniTest::Spec
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
224
|
-
it do OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer).
|
225
|
-
to_hash.
|
224
|
+
it do _(OpenStruct.new(:song => OpenStruct.new(:title => "The Fever And The Sound", :artist => "Strung Out")).extend(representer).
|
225
|
+
to_hash).
|
226
226
|
must_equal({"song"=>{"artist"=>"Strung Out", "title"=>"The Fever And The Sound"}})
|
227
227
|
end
|
228
228
|
end
|
@@ -246,7 +246,7 @@ class InlineTest < MiniTest::Spec
|
|
246
246
|
end
|
247
247
|
end
|
248
248
|
|
249
|
-
it { Object.new.extend(Mod).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
|
249
|
+
it { _(Object.new.extend(Mod).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
250
250
|
end
|
251
251
|
|
252
252
|
# define method inline with Decorator
|
@@ -267,6 +267,6 @@ class InlineTest < MiniTest::Spec
|
|
267
267
|
end
|
268
268
|
end
|
269
269
|
|
270
|
-
it { dec.new(Object.new).to_hash.must_equal("song"=>{"duration"=>"6:53"}) }
|
270
|
+
it { _(dec.new(Object.new).to_hash).must_equal("song"=>{"duration"=>"6:53"}) }
|
271
271
|
end
|
272
272
|
end
|
data/test/instance_test.rb
CHANGED
@@ -22,7 +22,7 @@ class InstanceTest < BaseTest
|
|
22
22
|
end
|
23
23
|
|
24
24
|
song = OpenStruct.new.extend(representer).from_hash({"title" => object})
|
25
|
-
song.title.must_equal object
|
25
|
+
_(song.title).must_equal object
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -36,11 +36,11 @@ class InstanceTest < BaseTest
|
|
36
36
|
:extend => song_representer
|
37
37
|
end
|
38
38
|
|
39
|
-
it( "xxx") { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
40
|
-
from_hash("song" => {"id" => 1}).song.must_equal Song.new(1, "The Answer Is Still No") }
|
39
|
+
it( "xxx") { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
40
|
+
from_hash("song" => {"id" => 1}).song).must_equal Song.new(1, "The Answer Is Still No") }
|
41
41
|
|
42
|
-
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
43
|
-
from_hash("song" => {"id" => 2}).song.must_equal Song.new(2, "Invincible") }
|
42
|
+
it { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
43
|
+
from_hash("song" => {"id" => 2}).song).must_equal Song.new(2, "Invincible") }
|
44
44
|
end
|
45
45
|
|
46
46
|
|
@@ -55,13 +55,13 @@ class InstanceTest < BaseTest
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it {
|
58
|
-
album= Struct.new(:songs).new(
|
58
|
+
album= Struct.new(:songs).new([
|
59
59
|
Song.new(1, "The Answer Is Still No"),
|
60
60
|
Song.new(2, "")])
|
61
61
|
|
62
|
-
album.
|
62
|
+
_(album.
|
63
63
|
extend(representer).
|
64
|
-
from_hash("songs" => [{"id" => 2},{"id" => 2, "title"=>"The Answer Is Still No"}]).songs.must_equal [
|
64
|
+
from_hash("songs" => [{"id" => 2},{"id" => 2, "title"=>"The Answer Is Still No"}]).songs).must_equal [
|
65
65
|
Song.new(2, "Invincible"), Song.new(2, "The Answer Is Still No")]
|
66
66
|
}
|
67
67
|
end
|
@@ -71,8 +71,8 @@ class InstanceTest < BaseTest
|
|
71
71
|
property :song, :instance => lambda { |options| Struct.new(:args, :id).new([options[:fragment], options[:user_options]]) }, :extend => song_representer
|
72
72
|
end
|
73
73
|
|
74
|
-
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
75
|
-
from_hash({"song" => {"id" => 1}}, user_options: { volume: 1 }).song.args.must_equal([{"id"=>1}, {:volume=>1}]) }
|
74
|
+
it { _(OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
75
|
+
from_hash({"song" => {"id" => 1}}, user_options: { volume: 1 }).song.args).must_equal([{"id"=>1}, {:volume=>1}]) }
|
76
76
|
end
|
77
77
|
|
78
78
|
# TODO: raise and test instance:{nil}
|
@@ -81,7 +81,7 @@ class InstanceTest < BaseTest
|
|
81
81
|
# property :song, :instance => lambda { |*| nil }, :extend => song_representer
|
82
82
|
# end
|
83
83
|
|
84
|
-
# let
|
84
|
+
# let(:hit) { hit = OpenStruct.new(:song => song).extend(representer) }
|
85
85
|
|
86
86
|
# it "calls #to_hash on song instance, nothing else" do
|
87
87
|
# hit.to_hash.must_equal("song"=>{"title"=>"Resist Stance"})
|
@@ -117,13 +117,13 @@ class InstanceTest < BaseTest
|
|
117
117
|
extend(representer).
|
118
118
|
from_hash("songs" => [{"title" => "The Answer Is Still No"}, {"title" => "Invincible"}])
|
119
119
|
|
120
|
-
album.songs.must_equal [
|
120
|
+
_(album.songs).must_equal [
|
121
121
|
Song.new(1, "The Answer Is Still No"),
|
122
122
|
Song.new(2, "Invincible")]
|
123
123
|
|
124
|
-
songs.object_id.must_equal album.songs.object_id
|
125
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
126
|
-
songs[1].object_id.must_equal album.songs[1].object_id
|
124
|
+
_(songs.object_id).must_equal album.songs.object_id
|
125
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
126
|
+
_(songs[1].object_id).must_equal album.songs[1].object_id
|
127
127
|
}
|
128
128
|
end
|
129
129
|
|
@@ -146,17 +146,17 @@ class InstanceTest < BaseTest
|
|
146
146
|
Song.new(1, "The Answer Is Still No"),
|
147
147
|
Song.new(2, "Invncble")])
|
148
148
|
|
149
|
-
album.
|
149
|
+
_(album.
|
150
150
|
extend(representer).
|
151
151
|
from_hash("songs" => [{"id" => 2, "title" => "Invincible"}]).
|
152
|
-
songs.must_equal [
|
152
|
+
songs).must_equal [
|
153
153
|
Song.new(1, "The Answer Is Still No"),
|
154
154
|
Song.new(2, "Invincible")]
|
155
155
|
# TODO: check elements object_id!
|
156
156
|
|
157
|
-
songs.object_id.must_equal album.songs.object_id
|
158
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
159
|
-
songs[1].object_id.must_equal album.songs[1].object_id
|
157
|
+
_(songs.object_id).must_equal album.songs.object_id
|
158
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
159
|
+
_(songs[1].object_id).must_equal album.songs[1].object_id
|
160
160
|
}
|
161
161
|
end
|
162
162
|
|
@@ -178,15 +178,15 @@ class InstanceTest < BaseTest
|
|
178
178
|
album= Struct.new(:songs).new(songs = [
|
179
179
|
Song.new(1, "The Answer Is Still No")])
|
180
180
|
|
181
|
-
album.
|
181
|
+
_(album.
|
182
182
|
extend(representer).
|
183
183
|
from_hash("songs" => [{"title" => "Invincible"}]).
|
184
|
-
songs.must_equal [
|
184
|
+
songs).must_equal [
|
185
185
|
Song.new(1, "The Answer Is Still No"),
|
186
186
|
Song.new(2, "Invincible")]
|
187
187
|
|
188
|
-
songs.object_id.must_equal album.songs.object_id
|
189
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
188
|
+
_(songs.object_id).must_equal album.songs.object_id
|
189
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
190
190
|
}
|
191
191
|
end
|
192
192
|
|
@@ -212,15 +212,15 @@ class InstanceTest < BaseTest
|
|
212
212
|
Song.new(1, "The Answer Is Still No"),
|
213
213
|
Song.new(2, "Invincible")])
|
214
214
|
|
215
|
-
album.
|
215
|
+
_(album.
|
216
216
|
extend(representer).
|
217
217
|
from_hash("songs" => [{"replace_id"=>2, "id" => 3, "title" => "Soulmate"}]).
|
218
|
-
songs.must_equal [
|
218
|
+
songs).must_equal [
|
219
219
|
Song.new(1, "The Answer Is Still No"),
|
220
220
|
Song.new(3, "Soulmate")]
|
221
221
|
|
222
|
-
songs.object_id.must_equal album.songs.object_id
|
223
|
-
songs[0].object_id.must_equal album.songs[0].object_id
|
222
|
+
_(songs.object_id).must_equal album.songs.object_id
|
223
|
+
_(songs[0].object_id).must_equal album.songs[0].object_id
|
224
224
|
}
|
225
225
|
end
|
226
226
|
|
@@ -235,13 +235,13 @@ class InstanceTest < BaseTest
|
|
235
235
|
album= Struct.new(:songs).new(songs = [
|
236
236
|
Song.new(1, "The Answer Is Still No")])
|
237
237
|
|
238
|
-
album.
|
238
|
+
_(album.
|
239
239
|
extend(representer).
|
240
240
|
from_hash("songs" => [{"title" => "Invincible"}]).
|
241
|
-
songs.must_equal [
|
241
|
+
songs).must_equal [
|
242
242
|
Song.new(nil, "Invincible")]
|
243
243
|
|
244
|
-
songs.object_id.wont_equal album.songs.object_id
|
244
|
+
_(songs.object_id).wont_equal album.songs.object_id
|
245
245
|
}
|
246
246
|
end
|
247
247
|
end
|
@@ -9,9 +9,9 @@ class IsRepresentableTest < BaseTest
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "does extend but doesn't call #to_hash" do
|
12
|
-
Struct.new(:song).new(song = Object.new).extend(representer).
|
13
|
-
to_hash.must_equal("song" => song)
|
14
|
-
song.must_be_kind_of Representable::Hash
|
12
|
+
_(Struct.new(:song).new(song = Object.new).extend(representer).
|
13
|
+
to_hash).must_equal("song" => song)
|
14
|
+
_(song).must_be_kind_of Representable::Hash
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -30,9 +30,9 @@ class IsRepresentableTest < BaseTest
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
Struct.new(:song).new(song).extend(representer).
|
34
|
-
to_hash.must_equal("song" => 1)
|
35
|
-
song.wont_be_kind_of Representable::Hash
|
33
|
+
_(Struct.new(:song).new(song).extend(representer).
|
34
|
+
to_hash).must_equal("song" => 1)
|
35
|
+
_(song).wont_be_kind_of Representable::Hash
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -48,8 +48,8 @@ class IsRepresentableTest < BaseTest
|
|
48
48
|
hit = Struct.new(:song).new.extend(representer).
|
49
49
|
from_hash("song" => 1)
|
50
50
|
|
51
|
-
hit.song.must_equal OpenStruct.new
|
52
|
-
hit.song.must_be_kind_of Representable::Hash
|
51
|
+
_(hit.song).must_equal OpenStruct.new
|
52
|
+
_(hit.song).must_be_kind_of Representable::Hash
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -70,8 +70,8 @@ class IsRepresentableTest < BaseTest
|
|
70
70
|
hit = Struct.new(:song).new.extend(representer).
|
71
71
|
from_hash("song" => "Sonata No.2")
|
72
72
|
|
73
|
-
hit.song.must_equal "Piano?"
|
74
|
-
hit.song.wont_be_kind_of Representable::Hash
|
73
|
+
_(hit.song).must_equal "Piano?"
|
74
|
+
_(hit.song).wont_be_kind_of Representable::Hash
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
data/test/json_test.rb
CHANGED
@@ -1,10 +1,34 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module JsonTest
|
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
|
+
|
14
|
+
let(:json) { '{"id":1,"name":"Rancid"}' }
|
15
|
+
|
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
|
+
|
19
|
+
#---
|
20
|
+
# to_json
|
21
|
+
let(:band) { Band.new(1, "Rancid") }
|
22
|
+
|
23
|
+
it { _(BandRepresenter.new(band).to_json).must_equal json }
|
24
|
+
it { _(BandRepresenter.new(band).render).must_equal json }
|
25
|
+
end
|
26
|
+
|
4
27
|
class APITest < MiniTest::Spec
|
5
28
|
Json = Representable::JSON
|
6
29
|
Def = Representable::Definition
|
7
30
|
|
31
|
+
|
8
32
|
describe "JSON module" do
|
9
33
|
before do
|
10
34
|
@Band = Class.new do
|
@@ -229,9 +253,7 @@ module JsonTest
|
|
229
253
|
assert_json '{"songName":"22 Acacia Avenue"}', song.to_json
|
230
254
|
end
|
231
255
|
end
|
232
|
-
|
233
|
-
end
|
234
|
-
|
256
|
+
end
|
235
257
|
|
236
258
|
class CollectionTest < MiniTest::Spec
|
237
259
|
describe "collection :name" do
|
@@ -334,20 +356,20 @@ end
|
|
334
356
|
end
|
335
357
|
|
336
358
|
it "renders" do
|
337
|
-
OpenStruct.new(:songs => {"7" => Song.new("Contemplation")}).extend(representer).to_hash.must_equal("songs"=>{"7"=>{"name"=>"Contemplation"}})
|
359
|
+
_(OpenStruct.new(:songs => {"7" => Song.new("Contemplation")}).extend(representer).to_hash).must_equal("songs"=>{"7"=>{"name"=>"Contemplation"}})
|
338
360
|
end
|
339
361
|
|
340
362
|
describe "parsing" do
|
341
363
|
subject { OpenStruct.new.extend(representer) }
|
342
|
-
let
|
364
|
+
let(:hsh) { {"7"=>{"name"=>"Contemplation"}} }
|
343
365
|
|
344
366
|
it "parses incoming hash" do
|
345
|
-
subject.from_hash("songs"=>hsh).songs.must_equal({"7"=>Song.new("Contemplation")})
|
367
|
+
_(subject.from_hash("songs"=>hsh).songs).must_equal({"7"=>Song.new("Contemplation")})
|
346
368
|
end
|
347
369
|
|
348
370
|
it "doesn't modify the incoming hash" do
|
349
371
|
subject.from_hash("songs"=> incoming_hash = hsh.dup)
|
350
|
-
hsh.must_equal incoming_hash
|
372
|
+
_(hsh).must_equal incoming_hash
|
351
373
|
end
|
352
374
|
end
|
353
375
|
end
|