representable 2.3.0 → 2.4.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +47 -0
- data/Gemfile +5 -0
- data/README.md +33 -0
- data/lib/representable.rb +60 -73
- data/lib/representable/binding.rb +37 -194
- data/lib/representable/cached.rb +10 -46
- data/lib/representable/coercion.rb +8 -8
- data/lib/representable/config.rb +15 -75
- data/lib/representable/debug.rb +41 -59
- data/lib/representable/declarative.rb +34 -53
- data/lib/representable/decorator.rb +11 -40
- data/lib/representable/definition.rb +14 -15
- data/lib/representable/deprecations.rb +90 -0
- data/lib/representable/deserializer.rb +87 -82
- data/lib/representable/for_collection.rb +5 -3
- data/lib/representable/hash.rb +5 -3
- data/lib/representable/hash/binding.rb +6 -15
- data/lib/representable/hash/collection.rb +10 -6
- data/lib/representable/hash_methods.rb +5 -5
- data/lib/representable/insert.rb +31 -0
- data/lib/representable/json.rb +7 -3
- data/lib/representable/json/hash.rb +1 -1
- data/lib/representable/object/binding.rb +5 -5
- data/lib/representable/parse_strategies.rb +37 -3
- data/lib/representable/pipeline.rb +37 -5
- data/lib/representable/pipeline_factories.rb +88 -0
- data/lib/representable/serializer.rb +38 -44
- data/lib/representable/version.rb +1 -1
- data/lib/representable/xml.rb +4 -0
- data/lib/representable/xml/binding.rb +25 -31
- data/lib/representable/xml/collection.rb +5 -3
- data/lib/representable/xml/hash.rb +7 -2
- data/lib/representable/yaml.rb +6 -3
- data/lib/representable/yaml/binding.rb +4 -4
- data/representable.gemspec +3 -3
- data/test/---deserialize-pipeline_test.rb +37 -0
- data/test/binding_test.rb +7 -7
- data/test/cached_test.rb +31 -19
- data/test/coercion_test.rb +2 -2
- data/test/config/inherit_test.rb +13 -12
- data/test/config_test.rb +12 -67
- data/test/decorator_test.rb +4 -5
- data/test/default_test.rb +34 -0
- data/test/defaults_options_test.rb +93 -0
- data/test/definition_test.rb +19 -39
- data/test/exec_context_test.rb +1 -1
- data/test/filter_test.rb +18 -20
- data/test/getter_setter_test.rb +1 -8
- data/test/hash_bindings_test.rb +13 -13
- data/test/heritage_test.rb +62 -0
- data/test/if_test.rb +1 -0
- data/test/inherit_test.rb +5 -3
- data/test/instance_test.rb +3 -4
- data/test/json_test.rb +3 -59
- data/test/lonely_test.rb +47 -3
- data/test/nested_test.rb +8 -2
- data/test/pipeline_test.rb +259 -0
- data/test/populator_test.rb +76 -0
- data/test/realistic_benchmark.rb +39 -7
- data/test/render_nil_test.rb +21 -0
- data/test/represent_test.rb +2 -2
- data/test/representable_test.rb +33 -103
- data/test/schema_test.rb +5 -15
- data/test/serialize_deserialize_test.rb +2 -2
- data/test/skip_test.rb +1 -1
- data/test/test_helper.rb +6 -0
- data/test/uncategorized_test.rb +67 -0
- data/test/xml_bindings_test.rb +6 -6
- data/test/xml_test.rb +6 -6
- metadata +33 -13
- data/lib/representable/apply.rb +0 -13
- data/lib/representable/inheritable.rb +0 -71
- data/lib/representable/mapper.rb +0 -83
- data/lib/representable/populator.rb +0 -56
- data/test/inheritable_test.rb +0 -97
data/test/exec_context_test.rb
CHANGED
@@ -86,7 +86,7 @@ class ExecContextTest < MiniTest::Spec
|
|
86
86
|
end
|
87
87
|
|
88
88
|
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
89
|
-
it { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
|
89
|
+
it("xxx") { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
data/test/filter_test.rb
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class FilterPipelineTest < MiniTest::Spec
|
4
|
-
let (:block1) { lambda { |
|
5
|
-
let (:block2) { lambda { |
|
4
|
+
let (:block1) { lambda { |input, options| "1: #{input}" } }
|
5
|
+
let (:block2) { lambda { |input, options| "2: #{input}" } }
|
6
6
|
|
7
7
|
subject { Representable::Pipeline[block1, block2] }
|
8
8
|
|
9
|
-
it { subject.call(
|
9
|
+
it { subject.call("Horowitz", {}).must_equal "2: 1: Horowitz" }
|
10
10
|
end
|
11
11
|
|
12
12
|
|
13
13
|
class FilterTest < MiniTest::Spec
|
14
14
|
representer! do
|
15
15
|
property :title
|
16
|
+
|
16
17
|
property :track,
|
17
|
-
:parse_filter => lambda { |
|
18
|
-
:render_filter => lambda { |val,
|
18
|
+
:parse_filter => lambda { |input, options| "#{input.downcase},#{options[:doc]}" },
|
19
|
+
:render_filter => lambda { |val, options| "#{val.upcase},#{options[:doc]},#{options[:user_options]}" }
|
19
20
|
end
|
20
21
|
|
21
22
|
# gets doc and options.
|
22
23
|
it {
|
23
24
|
song = OpenStruct.new.extend(representer).from_hash("title" => "VULCAN EARS", "track" => "Nine")
|
24
25
|
song.title.must_equal "VULCAN EARS"
|
25
|
-
song.track.must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}
|
26
|
+
song.track.must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
|
26
27
|
}
|
27
28
|
|
28
29
|
it { OpenStruct.new("title" => "vulcan ears", "track" => "Nine").extend(representer).to_hash.must_equal( {"title"=>"vulcan ears", "track"=>"NINE,{\"title\"=>\"vulcan ears\"},{}"}) }
|
@@ -32,14 +33,11 @@ class FilterTest < MiniTest::Spec
|
|
32
33
|
representer! do
|
33
34
|
property :track,
|
34
35
|
:parse_filter => [
|
35
|
-
lambda { |
|
36
|
-
lambda { |
|
36
|
+
lambda { |input, options| "#{input}-1" },
|
37
|
+
lambda { |input, options| "#{input}-2" }],
|
37
38
|
:render_filter => [
|
38
|
-
lambda { |val,
|
39
|
-
lambda { |val,
|
40
|
-
|
41
|
-
# definition[:parse_filter].instance_variable_get(:@value) << lambda { |val, doc, options| "#{val}-1" }
|
42
|
-
# property :track, :parse_filter => lambda { |val, doc, options| "#{val}-2" }, :inherit => true
|
39
|
+
lambda { |val, options| "#{val}-1" },
|
40
|
+
lambda { |val, options| "#{val}-2" }]
|
43
41
|
end
|
44
42
|
|
45
43
|
# order matters.
|
@@ -49,11 +47,11 @@ class FilterTest < MiniTest::Spec
|
|
49
47
|
end
|
50
48
|
|
51
49
|
|
52
|
-
class RenderFilterTest < MiniTest::Spec
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
50
|
+
# class RenderFilterTest < MiniTest::Spec
|
51
|
+
# representer! do
|
52
|
+
# property :track, :render_filter => [lambda { |val, options| "#{val}-1" } ]
|
53
|
+
# property :track, :render_filter => [lambda { |val, options| "#{val}-2" } ], :inherit => true
|
54
|
+
# end
|
57
55
|
|
58
|
-
|
59
|
-
end
|
56
|
+
# it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
|
57
|
+
# end
|
data/test/getter_setter_test.rb
CHANGED
@@ -14,15 +14,8 @@ class GetterSetterTest < BaseTest
|
|
14
14
|
subject.to_hash(:welcome => "Hi").must_equal({"name" => "Hi Mony Mony"})
|
15
15
|
end
|
16
16
|
|
17
|
-
it "does not call original reader when rendering" do
|
18
|
-
subject.instance_eval { def name; raise; end; self }.to_hash({})
|
19
|
-
end
|
20
|
-
|
21
17
|
it "uses :setter when parsing" do
|
18
|
+
subject.instance_eval { def name=(*); raise; end; self }
|
22
19
|
subject.from_hash({"name" => "Eyes Without A Face"}, :welcome => "Hello").song_name.must_equal "Hello Eyes Without A Face"
|
23
20
|
end
|
24
|
-
|
25
|
-
it "does not call original writer when parsing" do
|
26
|
-
subject.instance_eval { def name=(*); raise; end; self }.from_hash({"name"=>"Dana D And Talle T"}, {})
|
27
|
-
end
|
28
21
|
end
|
data/test/hash_bindings_test.rb
CHANGED
@@ -15,17 +15,17 @@ class HashBindingTest < MiniTest::Spec
|
|
15
15
|
describe "PropertyBinding" do
|
16
16
|
describe "#read" do
|
17
17
|
before do
|
18
|
-
@property = Representable::Hash::Binding.new(Representable::Definition.new(:song)
|
18
|
+
@property = Representable::Hash::Binding.new(Representable::Definition.new(:song))
|
19
19
|
end
|
20
20
|
|
21
21
|
it "returns fragment if present" do
|
22
|
-
assert_equal "Stick The Flag Up Your Goddamn Ass, You Sonofabitch", @property.read({"song" => "Stick The Flag Up Your Goddamn Ass, You Sonofabitch"})
|
23
|
-
assert_equal "", @property.read({"song" => ""})
|
24
|
-
assert_equal nil, @property.read({"song" => nil})
|
22
|
+
assert_equal "Stick The Flag Up Your Goddamn Ass, You Sonofabitch", @property.read({"song" => "Stick The Flag Up Your Goddamn Ass, You Sonofabitch"}, "song")
|
23
|
+
assert_equal "", @property.read({"song" => ""}, "song")
|
24
|
+
assert_equal nil, @property.read({"song" => nil}, "song")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "returns FRAGMENT_NOT_FOUND if not in document" do
|
28
|
-
assert_equal Representable::Binding::FragmentNotFound, @property.read({})
|
28
|
+
assert_equal Representable::Binding::FragmentNotFound, @property.read({}, "song")
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -35,16 +35,16 @@ class HashBindingTest < MiniTest::Spec
|
|
35
35
|
describe "CollectionBinding" do
|
36
36
|
describe "with plain text items" do
|
37
37
|
before do
|
38
|
-
@property = Representable::Hash::Binding::Collection.new(Representable::Definition.new(:songs, :collection => true)
|
38
|
+
@property = Representable::Hash::Binding::Collection.new(Representable::Definition.new(:songs, :collection => true))
|
39
39
|
end
|
40
40
|
|
41
41
|
it "extracts with #read" do
|
42
|
-
assert_equal ["The Gargoyle", "Bronx"], @property.read("songs" => ["The Gargoyle", "Bronx"])
|
42
|
+
assert_equal ["The Gargoyle", "Bronx"], @property.read({"songs" => ["The Gargoyle", "Bronx"]}, "songs")
|
43
43
|
end
|
44
44
|
|
45
45
|
it "inserts with #write" do
|
46
46
|
doc = {}
|
47
|
-
assert_equal(["The Gargoyle", "Bronx"], @property.write(doc, ["The Gargoyle", "Bronx"]))
|
47
|
+
assert_equal(["The Gargoyle", "Bronx"], @property.write(doc, ["The Gargoyle", "Bronx"], "songs"))
|
48
48
|
assert_equal({"songs"=>["The Gargoyle", "Bronx"]}, doc)
|
49
49
|
end
|
50
50
|
end
|
@@ -56,29 +56,29 @@ class HashBindingTest < MiniTest::Spec
|
|
56
56
|
describe "HashBinding" do
|
57
57
|
describe "with plain text items" do
|
58
58
|
before do
|
59
|
-
@property = Representable::Hash::Binding
|
59
|
+
@property = Representable::Hash::Binding.new(Representable::Definition.new(:songs, :hash => true))
|
60
60
|
end
|
61
61
|
|
62
62
|
it "extracts with #read" do
|
63
|
-
assert_equal({"first" => "The Gargoyle", "second" => "Bronx"} , @property.read("songs" => {"first" => "The Gargoyle", "second" => "Bronx"}))
|
63
|
+
assert_equal({"first" => "The Gargoyle", "second" => "Bronx"} , @property.read({"songs" => {"first" => "The Gargoyle", "second" => "Bronx"}}, "songs"))
|
64
64
|
end
|
65
65
|
|
66
66
|
it "inserts with #write" do
|
67
67
|
doc = {}
|
68
|
-
assert_equal({"first" => "The Gargoyle", "second" => "Bronx"}, @property.write(doc, {"first" => "The Gargoyle", "second" => "Bronx"}))
|
68
|
+
assert_equal({"first" => "The Gargoyle", "second" => "Bronx"}, @property.write(doc, {"first" => "The Gargoyle", "second" => "Bronx"}, "songs"))
|
69
69
|
assert_equal({"songs"=>{"first" => "The Gargoyle", "second" => "Bronx"}}, doc)
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
73
|
describe "with objects" do
|
74
74
|
before do
|
75
|
-
@property = Representable::Hash::Binding
|
75
|
+
@property = Representable::Hash::Binding.new(Representable::Definition.new(:songs, :hash => true, :class => Song, :extend => SongRepresenter))
|
76
76
|
end
|
77
77
|
|
78
78
|
it "doesn't change the represented hash in #write" do
|
79
79
|
song = Song.new("Better Than That")
|
80
80
|
hash = {"first" => song}
|
81
|
-
@property.write({}, hash)
|
81
|
+
@property.write({}, hash, "song")
|
82
82
|
assert_equal({"first" => song}, hash)
|
83
83
|
end
|
84
84
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class HeritageTest < Minitest::Spec
|
4
|
+
module Hello
|
5
|
+
def hello
|
6
|
+
puts "Hello!"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
module Ciao
|
10
|
+
def ciao
|
11
|
+
puts "Ciao!"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
class A < Representable::Decorator
|
17
|
+
include Representable::Hash
|
18
|
+
|
19
|
+
feature Hello
|
20
|
+
|
21
|
+
property :id do
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class B < A
|
26
|
+
feature Ciao # does NOT extend id, of course.
|
27
|
+
|
28
|
+
property :id, inherit: true do
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class C < A
|
34
|
+
property :id do end # overwrite old :id.
|
35
|
+
end
|
36
|
+
|
37
|
+
it do
|
38
|
+
# puts A.heritage.inspect
|
39
|
+
# puts B.heritage.inspect
|
40
|
+
|
41
|
+
puts B.representable_attrs.get(:id)[:extend].(nil).new(nil).hello
|
42
|
+
puts B.representable_attrs.get(:id)[:extend].(nil).new(nil).ciao
|
43
|
+
|
44
|
+
# feature Hello must be "inherited" from A and included in new C properties, too.
|
45
|
+
puts C.representable_attrs.get(:id)[:extend].(nil).new(nil).hello
|
46
|
+
end
|
47
|
+
|
48
|
+
module M
|
49
|
+
include Representable
|
50
|
+
feature Hello
|
51
|
+
end
|
52
|
+
|
53
|
+
module N
|
54
|
+
include Representable
|
55
|
+
include M
|
56
|
+
feature Ciao
|
57
|
+
end
|
58
|
+
|
59
|
+
it do
|
60
|
+
Object.new.extend(N).hello
|
61
|
+
end
|
62
|
+
end
|
data/test/if_test.rb
CHANGED
data/test/inherit_test.rb
CHANGED
@@ -55,8 +55,8 @@ class InheritTest < MiniTest::Spec
|
|
55
55
|
representer! do
|
56
56
|
include SongRepresenter
|
57
57
|
|
58
|
-
property :name, :
|
59
|
-
# that doesn't make sense.
|
58
|
+
property :name, inherit: true, as: :name do # inherit module, only.
|
59
|
+
# that doesn't make sense. but it should simply inherit the old nested properties.
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
@@ -91,7 +91,7 @@ class InheritTest < MiniTest::Spec
|
|
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
93
|
definition[:representable]. must_equal true
|
94
|
-
definition
|
94
|
+
definition.name.must_equal "track" # was "no".
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -110,7 +110,9 @@ class InheritTest < MiniTest::Spec
|
|
110
110
|
|
111
111
|
let (:inheriting) {
|
112
112
|
class InheritingDecorator < representer
|
113
|
+
include Representable::Debug
|
113
114
|
property :hit, :inherit => true do
|
115
|
+
include Representable::Debug
|
114
116
|
property :length
|
115
117
|
end
|
116
118
|
self
|
data/test/instance_test.rb
CHANGED
@@ -36,7 +36,7 @@ class InstanceTest < BaseTest
|
|
36
36
|
:extend => song_representer
|
37
37
|
end
|
38
38
|
|
39
|
-
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
39
|
+
it( "xxx") { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
40
40
|
from_hash("song" => {"id" => 1}).song.must_equal Song.new(1, "The Answer Is Still No") }
|
41
41
|
|
42
42
|
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
@@ -48,7 +48,6 @@ class InstanceTest < BaseTest
|
|
48
48
|
representer!(:inject => :song_representer) do
|
49
49
|
collection :songs,
|
50
50
|
:instance => lambda { |fragment, i, *args|
|
51
|
-
|
52
51
|
fragment["id"] == songs[i].id ? songs[i] : Song.find(fragment["id"])
|
53
52
|
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
54
53
|
|
@@ -142,7 +141,7 @@ class InstanceTest < BaseTest
|
|
142
141
|
:setter => lambda { |*| }
|
143
142
|
end
|
144
143
|
|
145
|
-
it {
|
144
|
+
it("hooray") {
|
146
145
|
album= Struct.new(:songs).new(songs = [
|
147
146
|
Song.new(1, "The Answer Is Still No"),
|
148
147
|
Song.new(2, "Invncble")])
|
@@ -250,7 +249,7 @@ class InstanceTest < BaseTest
|
|
250
249
|
describe "new syntax for instance: true" do
|
251
250
|
representer!(:inject => :song_representer) do
|
252
251
|
property :song, :pass_options => true,
|
253
|
-
:extend => song_representer, :instance => lambda { |fragment, args| args.binding.get }
|
252
|
+
:extend => song_representer, :instance => lambda { |fragment, args| args.binding.get(represented: args.represented) }
|
254
253
|
end
|
255
254
|
|
256
255
|
it "uses Binding#get instead of creating an instance, but deprecates" do
|
data/test/json_test.rb
CHANGED
@@ -88,25 +88,13 @@ module JsonTest
|
|
88
88
|
|
89
89
|
describe "#build_for" do
|
90
90
|
it "returns TextBinding" do
|
91
|
-
assert_kind_of Representable::Hash::Binding, Representable::Hash::Binding.build_for(Def.new(:band)
|
92
|
-
end
|
93
|
-
|
94
|
-
it "returns HashBinding" do
|
95
|
-
assert_kind_of Representable::Hash::Binding::Hash, Representable::Hash::Binding.build_for(Def.new(:band, :hash => true), nil)
|
91
|
+
assert_kind_of Representable::Hash::Binding, Representable::Hash::Binding.build_for(Def.new(:band))
|
96
92
|
end
|
97
93
|
|
98
94
|
it "returns CollectionBinding" do
|
99
|
-
assert_kind_of Representable::Hash::Binding::Collection, Representable::Hash::Binding.build_for(Def.new(:band, :collection => true)
|
95
|
+
assert_kind_of Representable::Hash::Binding::Collection, Representable::Hash::Binding.build_for(Def.new(:band, :collection => true))
|
100
96
|
end
|
101
97
|
end
|
102
|
-
|
103
|
-
# describe "#representable_bindings_for" do
|
104
|
-
# it "returns bindings for each property" do
|
105
|
-
# bins = @band.send(:representable_bindings_for, Representable::JSON::Binding, {})
|
106
|
-
# assert_equal 2, bins.size
|
107
|
-
# assert_equal "name", bins.first.name
|
108
|
-
# end
|
109
|
-
# end
|
110
98
|
end
|
111
99
|
|
112
100
|
|
@@ -242,50 +230,6 @@ module JsonTest
|
|
242
230
|
end
|
243
231
|
end
|
244
232
|
|
245
|
-
describe ":default => :value" do
|
246
|
-
before do
|
247
|
-
@Album = Class.new do
|
248
|
-
include Representable::JSON
|
249
|
-
property :name, :default => "30 Years Live"
|
250
|
-
attr_accessor :name
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
describe "#from_json" do
|
255
|
-
it "uses default when property nil in doc" do
|
256
|
-
album = @Album.new.from_json({}.to_json)
|
257
|
-
assert_equal "30 Years Live", album.name
|
258
|
-
end
|
259
|
-
|
260
|
-
it "uses value from doc when present" do
|
261
|
-
album = @Album.new.from_json({:name => "Live At The Wireless"}.to_json)
|
262
|
-
assert_equal "Live At The Wireless", album.name
|
263
|
-
end
|
264
|
-
|
265
|
-
it "uses value from doc when empty string" do
|
266
|
-
album = @Album.new.from_json({:name => ""}.to_json)
|
267
|
-
assert_equal "", album.name
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
describe "#to_json" do
|
272
|
-
it "uses default when not available in object" do
|
273
|
-
assert_json "{\"name\":\"30 Years Live\"}", @Album.new.to_json
|
274
|
-
end
|
275
|
-
|
276
|
-
it "uses value from represented object when present" do
|
277
|
-
album = @Album.new
|
278
|
-
album.name = "Live At The Wireless"
|
279
|
-
assert_json "{\"name\":\"Live At The Wireless\"}", album.to_json
|
280
|
-
end
|
281
|
-
|
282
|
-
it "uses value from represented object when emtpy string" do
|
283
|
-
album = @Album.new
|
284
|
-
album.name = ""
|
285
|
-
assert_json "{\"name\":\"\"}", album.to_json
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
289
233
|
end
|
290
234
|
|
291
235
|
|
@@ -302,7 +246,7 @@ end
|
|
302
246
|
assert_equal ["Out in the cold", "Microphone"], cd.songs
|
303
247
|
end
|
304
248
|
|
305
|
-
it "#to_json serializes correctly" do
|
249
|
+
it "zzz#to_json serializes correctly" do
|
306
250
|
cd = CD.new
|
307
251
|
cd.songs = ["Out in the cold", "Microphone"]
|
308
252
|
|
data/test/lonely_test.rb
CHANGED
@@ -36,7 +36,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
36
36
|
property :name
|
37
37
|
end
|
38
38
|
|
39
|
-
let (:decorator) { rpr = representer; Class.new(Representable::Decorator) { include rpr } }
|
39
|
+
let (:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::Hash; include rpr } }
|
40
40
|
|
41
41
|
describe "JSON::Collection" do
|
42
42
|
let (:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
|
@@ -55,7 +55,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
55
55
|
assert_json json, songs.extend(representer).to_json
|
56
56
|
end
|
57
57
|
|
58
|
-
it "renders array with decorator
|
58
|
+
it "renders array with decorator" do
|
59
59
|
assert_json json, decorator.new(songs).to_json
|
60
60
|
end
|
61
61
|
|
@@ -98,6 +98,23 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
describe "Hash::Collection with dynamic options" do
|
102
|
+
class One < Representable::Decorator
|
103
|
+
def to_hash(*); "One: #{represented}"; end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Two < Representable::Decorator
|
107
|
+
def to_hash(*); "Two: #{represented}"; end
|
108
|
+
end
|
109
|
+
|
110
|
+
representer!(module: Representable::Hash::Collection) do
|
111
|
+
items extend: ->(options) { options[:input] == 1 ? options[:user_options][:one] : options[:user_options][:two] }
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
it { [1,2].extend(representer).to_hash(one: One, two: Two).must_equal(["One: 1", "Two: 2"]) }
|
116
|
+
end
|
117
|
+
|
101
118
|
|
102
119
|
describe "JSON::Hash" do # TODO: move to HashTest.
|
103
120
|
describe "with contained objects" do
|
@@ -141,7 +158,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
141
158
|
assert_equal({"two" => Song.new("Can't Take Them All")}, {}.extend(representer).from_json(json, :exclude => [:one]))
|
142
159
|
end
|
143
160
|
|
144
|
-
it "
|
161
|
+
it "xxxrespects :include" do
|
145
162
|
assert_equal({"one" => Song.new("Days Go By")}, {}.extend(representer).from_json(json, :include => [:one]))
|
146
163
|
end
|
147
164
|
end
|
@@ -160,6 +177,33 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
160
177
|
end
|
161
178
|
|
162
179
|
|
180
|
+
describe "with scalar" do
|
181
|
+
let (:representer) {
|
182
|
+
Module.new do
|
183
|
+
include Representable::JSON::Hash
|
184
|
+
end
|
185
|
+
}
|
186
|
+
let (:json) { %{{"one":1,"two":2}} }
|
187
|
+
let (:data) { {one: 2, two: 3} }
|
188
|
+
|
189
|
+
describe "#to_json" do
|
190
|
+
it { data.extend(representer).to_json.must_equal %{{"one":2,"two":3}} }
|
191
|
+
|
192
|
+
# it "respects :exclude" do
|
193
|
+
# assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:exclude => [:one])
|
194
|
+
# end
|
195
|
+
|
196
|
+
# it "respects :include" do
|
197
|
+
# assert_json "{\"two\":{\"name\":\"Can't Take Them All\"}}", {:one => Song.new("Days Go By"), :two => Song.new("Can't Take Them All")}.extend(representer).to_json(:include => [:two])
|
198
|
+
# end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#from_json" do # FIXME: what's the point of this?
|
202
|
+
it { data.extend(representer).from_hash(data).must_equal data }
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
|
163
207
|
describe "with contained text" do
|
164
208
|
before do
|
165
209
|
@songs_representer = Module.new do
|