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/for_collection_test.rb
CHANGED
@@ -7,8 +7,8 @@ class ForCollectionTest < MiniTest::Spec
|
|
7
7
|
property :name
|
8
8
|
end
|
9
9
|
|
10
|
-
let
|
11
|
-
let
|
10
|
+
let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
|
11
|
+
let(:json) { "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]" }
|
12
12
|
|
13
13
|
|
14
14
|
# Module.for_collection
|
@@ -20,9 +20,9 @@ class ForCollectionTest < MiniTest::Spec
|
|
20
20
|
) do |format, mod, output, input|
|
21
21
|
|
22
22
|
describe "Module::for_collection [#{format}]" do
|
23
|
-
let
|
23
|
+
let(:format) { format }
|
24
24
|
|
25
|
-
let
|
25
|
+
let(:representer) {
|
26
26
|
Module.new do
|
27
27
|
include mod
|
28
28
|
property :name#, :as => :title
|
@@ -36,13 +36,13 @@ class ForCollectionTest < MiniTest::Spec
|
|
36
36
|
it { render(songs.extend(representer.for_collection)).must_equal_document output }
|
37
37
|
it { render(representer.for_collection.prepare(songs)).must_equal_document output }
|
38
38
|
# parsing needs the class set, at least
|
39
|
-
it { parse([].extend(representer.for_collection), input).must_equal songs }
|
39
|
+
it { _(parse([].extend(representer.for_collection), input)).must_equal songs }
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "Module::for_collection without configuration [#{format}]" do
|
43
|
-
let
|
43
|
+
let(:format) { format }
|
44
44
|
|
45
|
-
let
|
45
|
+
let(:representer) {
|
46
46
|
Module.new do
|
47
47
|
include mod
|
48
48
|
property :name
|
@@ -55,8 +55,8 @@ class ForCollectionTest < MiniTest::Spec
|
|
55
55
|
|
56
56
|
|
57
57
|
describe "Decorator::for_collection [#{format}]" do
|
58
|
-
let
|
59
|
-
let
|
58
|
+
let(:format) { format }
|
59
|
+
let(:representer) {
|
60
60
|
Class.new(Representable::Decorator) do
|
61
61
|
include mod
|
62
62
|
property :name
|
@@ -66,7 +66,7 @@ class ForCollectionTest < MiniTest::Spec
|
|
66
66
|
}
|
67
67
|
|
68
68
|
it { render(representer.for_collection.new(songs)).must_equal_document output }
|
69
|
-
it { parse(representer.for_collection.new([]), input).must_equal songs }
|
69
|
+
it { _(parse(representer.for_collection.new([]), input)).must_equal songs }
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
data/test/generic_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
4
|
-
let
|
5
|
-
let
|
6
|
-
let
|
7
|
-
let
|
4
|
+
let(:new_album) { OpenStruct.new.extend(representer) }
|
5
|
+
let(:album) { OpenStruct.new(:songs => ["Fuck Armageddon"]).extend(representer) }
|
6
|
+
let(:song) { OpenStruct.new(:title => "Resist Stance") }
|
7
|
+
let(:song_representer) { Module.new do include Representable::Hash; property :title end }
|
8
8
|
|
9
9
|
|
10
10
|
describe "::collection" do
|
@@ -14,13 +14,13 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
14
14
|
|
15
15
|
it "doesn't initialize property" do
|
16
16
|
new_album.from_hash({})
|
17
|
-
new_album.songs.
|
17
|
+
_(new_album.songs).must_be_nil
|
18
18
|
end
|
19
19
|
|
20
20
|
it "leaves properties untouched" do
|
21
21
|
album.from_hash({})
|
22
22
|
# TODO: test property.
|
23
|
-
album.songs.must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
23
|
+
_(album.songs).must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
24
24
|
end
|
25
25
|
|
26
26
|
|
@@ -32,14 +32,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
32
32
|
) do |format, mod, output, input|
|
33
33
|
|
34
34
|
describe "nil collections" do
|
35
|
-
let
|
35
|
+
let(:format) { format }
|
36
36
|
|
37
37
|
representer!(:module => mod) do
|
38
38
|
collection :songs
|
39
39
|
self.representation_wrap = :album if format == :xml
|
40
40
|
end
|
41
41
|
|
42
|
-
let
|
42
|
+
let(:album) { Album.new.extend(representer) }
|
43
43
|
|
44
44
|
it "doesn't render collection in #{format}" do
|
45
45
|
render(album).must_equal_document output
|
@@ -55,14 +55,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
55
55
|
) do |format, mod, output, input|
|
56
56
|
|
57
57
|
describe "empty collections" do
|
58
|
-
let
|
58
|
+
let(:format) { format }
|
59
59
|
|
60
60
|
representer!(:module => mod) do
|
61
61
|
collection :songs
|
62
62
|
self.representation_wrap = :album if format == :xml
|
63
63
|
end
|
64
64
|
|
65
|
-
let
|
65
|
+
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
66
66
|
|
67
67
|
it "renders empty collection in #{format}" do
|
68
68
|
render(album).must_equal_document output
|
@@ -79,14 +79,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
79
79
|
) do |format, mod, output, input|
|
80
80
|
|
81
81
|
describe "render_empty [#{format}]" do
|
82
|
-
let
|
82
|
+
let(:format) { format }
|
83
83
|
|
84
84
|
representer!(:module => mod) do
|
85
85
|
collection :songs, :render_empty => false
|
86
86
|
self.representation_wrap = :album if format == :xml
|
87
87
|
end
|
88
88
|
|
89
|
-
let
|
89
|
+
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
90
90
|
|
91
91
|
it { render(album).must_equal_document output }
|
92
92
|
end
|
@@ -109,7 +109,7 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
109
109
|
end
|
110
110
|
|
111
111
|
it "doesn't change represented object" do
|
112
|
-
song.extend(representer).send("from_#{format}", input).title.must_equal "Resist Stance"
|
112
|
+
_(song.extend(representer).send("from_#{format}", input).title).must_equal "Resist Stance"
|
113
113
|
end
|
114
114
|
end
|
115
115
|
end
|
data/test/getter_setter_test.rb
CHANGED
@@ -3,19 +3,19 @@ require 'test_helper'
|
|
3
3
|
class GetterSetterTest < BaseTest
|
4
4
|
representer! do
|
5
5
|
property :name, # key under :name.
|
6
|
-
:getter => lambda { |
|
7
|
-
:setter => lambda { |
|
6
|
+
:getter => lambda { |user_options:, **| "#{user_options[:welcome]} #{song_name}" },
|
7
|
+
:setter => lambda { |user_options:, input:, **| self.song_name = "#{user_options[:welcome]} #{input}" }
|
8
8
|
end
|
9
9
|
|
10
10
|
subject { Struct.new(:song_name).new("Mony Mony").extend(representer) }
|
11
11
|
|
12
12
|
it "uses :getter when rendering" do
|
13
13
|
subject.instance_eval { def name; raise; end }
|
14
|
-
subject.to_hash(user_options: {welcome: "Hi"}).must_equal({"name" => "Hi Mony Mony"})
|
14
|
+
_(subject.to_hash(user_options: {welcome: "Hi"})).must_equal({"name" => "Hi Mony Mony"})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "uses :setter when parsing" do
|
18
18
|
subject.instance_eval { def name=(*); raise; end; self }
|
19
|
-
subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name.must_equal "Hello Eyes Without A Face"
|
19
|
+
_(subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name).must_equal "Hello Eyes Without A Face"
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
data/test/hash_bindings_test.rb
CHANGED
@@ -21,7 +21,7 @@ class HashBindingTest < MiniTest::Spec
|
|
21
21
|
it "returns fragment if present" do
|
22
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
23
|
assert_equal "", @property.read({"song" => ""}, "song")
|
24
|
-
|
24
|
+
assert_nil @property.read({"song" => nil}, "song")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "returns FRAGMENT_NOT_FOUND if not in document" do
|
data/test/hash_test.rb
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
class HashPublicMethodsTest < Minitest::Spec
|
4
|
+
#---
|
5
|
+
# from_hash
|
6
|
+
class BandRepresenter < Representable::Decorator
|
7
|
+
include Representable::Hash
|
8
|
+
property :id
|
9
|
+
property :name
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:data) { {"id"=>1,"name"=>"Rancid"} }
|
13
|
+
|
14
|
+
it { _(BandRepresenter.new(Band.new).from_hash(data)[:id, :name]).must_equal [1, "Rancid"] }
|
15
|
+
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal [1, "Rancid"] }
|
16
|
+
|
17
|
+
#---
|
18
|
+
# to_hash
|
19
|
+
let(:band) { Band.new(1, "Rancid") }
|
20
|
+
|
21
|
+
it { _(BandRepresenter.new(band).to_hash).must_equal data }
|
22
|
+
it { _(BandRepresenter.new(band).render).must_equal data }
|
23
|
+
end
|
24
|
+
|
3
25
|
class HashWithScalarPropertyTest < MiniTest::Spec
|
4
26
|
Album = Struct.new(:title)
|
5
27
|
|
@@ -7,24 +29,24 @@ class HashWithScalarPropertyTest < MiniTest::Spec
|
|
7
29
|
property :title
|
8
30
|
end
|
9
31
|
|
10
|
-
let
|
32
|
+
let(:album) { Album.new("Liar") }
|
11
33
|
|
12
34
|
describe "#to_hash" do
|
13
35
|
it "renders plain property" do
|
14
|
-
album.extend(representer).to_hash.must_equal("title" => "Liar")
|
36
|
+
_(album.extend(representer).to_hash).must_equal("title" => "Liar")
|
15
37
|
end
|
16
38
|
end
|
17
39
|
|
18
40
|
|
19
41
|
describe "#from_hash" do
|
20
42
|
it "parses plain property" do
|
21
|
-
album.extend(representer).from_hash("title" => "This Song Is Recycled").title.must_equal "This Song Is Recycled"
|
43
|
+
_(album.extend(representer).from_hash("title" => "This Song Is Recycled").title).must_equal "This Song Is Recycled"
|
22
44
|
end
|
23
45
|
|
24
46
|
# Fixes issue #115
|
25
47
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
26
48
|
album = Album.new
|
27
|
-
album.title.
|
49
|
+
_(album.title).must_be_nil
|
28
50
|
album.extend(representer).from_hash("title" => nil)
|
29
51
|
end
|
30
52
|
end
|
@@ -40,39 +62,39 @@ class HashWithTypedPropertyTest < MiniTest::Spec
|
|
40
62
|
end
|
41
63
|
end
|
42
64
|
|
43
|
-
let
|
65
|
+
let(:album) { Album.new(Song.new("Liar")) }
|
44
66
|
|
45
67
|
describe "#to_hash" do
|
46
68
|
it "renders embedded typed property" do
|
47
|
-
album.extend(representer).to_hash.must_equal("best_song" => {"name" => "Liar"})
|
69
|
+
_(album.extend(representer).to_hash).must_equal("best_song" => {"name" => "Liar"})
|
48
70
|
end
|
49
71
|
end
|
50
72
|
|
51
73
|
describe "#from_hash" do
|
52
74
|
it "parses embedded typed property" do
|
53
75
|
album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
|
54
|
-
album.best_song.name.must_equal "Go With Me"
|
76
|
+
_(album.best_song.name).must_equal "Go With Me"
|
55
77
|
end
|
56
78
|
|
57
79
|
# nested nil removes nested object.
|
58
80
|
it do
|
59
81
|
album = Album.new(Song.new("Pre-medicated Murder"))
|
60
82
|
album.extend(representer).from_hash("best_song" => nil)
|
61
|
-
album.best_song.
|
83
|
+
_(album.best_song).must_be_nil
|
62
84
|
end
|
63
85
|
|
64
86
|
# nested blank hash creates blank object when not populated.
|
65
87
|
it do
|
66
88
|
album = Album.new#(Song.new("Pre-medicated Murder"))
|
67
89
|
album.extend(representer).from_hash("best_song" => {})
|
68
|
-
album.best_song.name.
|
90
|
+
_(album.best_song.name).must_be_nil
|
69
91
|
end
|
70
92
|
|
71
93
|
# Fixes issue #115
|
72
94
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
73
95
|
album = Album.new
|
74
96
|
album.extend(representer).from_hash("best_song" => nil)
|
75
|
-
album.best_song.
|
97
|
+
_(album.best_song).must_be_nil
|
76
98
|
end
|
77
99
|
end
|
78
100
|
end
|
@@ -85,10 +107,10 @@ class HashWithTypedPropertyAndAs < MiniTest::Spec
|
|
85
107
|
end
|
86
108
|
end
|
87
109
|
|
88
|
-
let
|
110
|
+
let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }
|
89
111
|
|
90
|
-
it { album.to_hash.must_equal("hit" => {"name" => "Liar"}) }
|
91
|
-
it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
112
|
+
it { _(album.to_hash).must_equal("hit" => {"name" => "Liar"}) }
|
113
|
+
it { _(album.from_hash("hit" => {"name" => "Go With Me"})).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
92
114
|
end
|
93
115
|
# # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
|
94
116
|
# # hash_song = Module.new do
|
@@ -97,13 +119,13 @@ end
|
|
97
119
|
# # property :name
|
98
120
|
# # end
|
99
121
|
|
100
|
-
# # let
|
122
|
+
# # let(:hash_album) { Module.new do
|
101
123
|
# # include Representable::XML
|
102
124
|
# # self.representation_wrap = :album
|
103
125
|
# # property :song, :extend => hash_song, :class => Song, :as => :hit
|
104
126
|
# # end }
|
105
127
|
|
106
|
-
# # let
|
128
|
+
# # let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(hash_album) }
|
107
129
|
|
108
130
|
# # it { album.to_xml.must_equal_xml("<album><hit><name>Liar</name></hit></album>") }
|
109
131
|
# # #it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
@@ -122,18 +144,18 @@ class HashWithTypedCollectionTest < MiniTest::Spec
|
|
122
144
|
end
|
123
145
|
end
|
124
146
|
|
125
|
-
let
|
147
|
+
let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
|
126
148
|
|
127
149
|
describe "#to_hash" do
|
128
150
|
it "renders collection of typed property" do
|
129
|
-
album.extend(representer).to_hash.must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
151
|
+
_(album.extend(representer).to_hash).must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
130
152
|
end
|
131
153
|
end
|
132
154
|
|
133
155
|
describe "#from_hash" do
|
134
156
|
it "parses collection of typed property" do
|
135
|
-
album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
136
|
-
{"name" => "Three Way Dance", "track" => 5}]).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
157
|
+
_(album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
158
|
+
{"name" => "Three Way Dance", "track" => 5}])).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
137
159
|
end
|
138
160
|
end
|
139
161
|
end
|
@@ -142,19 +164,19 @@ class HashWithScalarCollectionTest < MiniTest::Spec
|
|
142
164
|
Album = Struct.new(:songs)
|
143
165
|
representer! { collection :songs }
|
144
166
|
|
145
|
-
let
|
167
|
+
let(:album) { Album.new(["Jackhammer", "Terrible Man"]) }
|
146
168
|
|
147
169
|
|
148
170
|
describe "#to_hash" do
|
149
171
|
it "renders a block style list per default" do
|
150
|
-
album.extend(representer).to_hash.must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
172
|
+
_(album.extend(representer).to_hash).must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
151
173
|
end
|
152
174
|
end
|
153
175
|
|
154
176
|
|
155
177
|
describe "#from_hash" do
|
156
178
|
it "parses a block style list" do
|
157
|
-
album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"]).must_equal Album.new(["Off Key Melody", "Sinking"])
|
179
|
+
_(album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"])).must_equal Album.new(["Off Key Melody", "Sinking"])
|
158
180
|
end
|
159
181
|
end
|
160
|
-
end
|
182
|
+
end
|
data/test/heritage_test.rb
CHANGED
@@ -3,16 +3,16 @@ require "test_helper"
|
|
3
3
|
class HeritageTest < Minitest::Spec
|
4
4
|
module Hello
|
5
5
|
def hello
|
6
|
-
|
6
|
+
"Hello!"
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
|
+
module Ciao
|
10
11
|
def ciao
|
11
|
-
|
12
|
+
"Ciao!"
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
16
|
class A < Representable::Decorator
|
17
17
|
include Representable::Hash
|
18
18
|
|
@@ -34,15 +34,16 @@ class HeritageTest < Minitest::Spec
|
|
34
34
|
property :id do end # overwrite old :id.
|
35
35
|
end
|
36
36
|
|
37
|
-
it do
|
38
|
-
|
39
|
-
|
37
|
+
it "B must inherit Hello! feature from A" do
|
38
|
+
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
41
|
+
it "B must have Ciao from module (feauture) Ciao" do
|
42
|
+
_(B.representable_attrs.get(:id)[:extend].(nil).new(nil).ciao).must_equal "Ciao!"
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
-
|
45
|
+
it "C must inherit Hello! feature from A" do
|
46
|
+
_(C.representable_attrs.get(:id)[:extend].(nil).new(nil).hello).must_equal "Hello!"
|
46
47
|
end
|
47
48
|
|
48
49
|
module M
|
@@ -56,7 +57,9 @@ class HeritageTest < Minitest::Spec
|
|
56
57
|
feature Ciao
|
57
58
|
end
|
58
59
|
|
59
|
-
|
60
|
-
|
60
|
+
let(:obj_extending_N) { Object.new.extend(N) }
|
61
|
+
|
62
|
+
it "obj should inherit from N, and N from M" do
|
63
|
+
_(obj_extending_N.hello).must_equal "Hello!"
|
61
64
|
end
|
62
65
|
end
|
data/test/if_test.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class IfTest < MiniTest::Spec
|
4
|
-
let
|
4
|
+
let(:band_class) { Class.new do
|
5
5
|
include Representable::Hash
|
6
6
|
attr_accessor :fame
|
7
7
|
self
|
@@ -18,14 +18,14 @@ class IfTest < MiniTest::Spec
|
|
18
18
|
band_class.class_eval { property :fame, :if => lambda { |*| false } }
|
19
19
|
band = band_class.new
|
20
20
|
band.from_hash({"fame"=>"oh yes"})
|
21
|
-
|
21
|
+
assert_nil band.fame
|
22
22
|
end
|
23
23
|
|
24
24
|
it "ignores property when :exclude'ed even when condition is true" do
|
25
25
|
band_class.class_eval { property :fame, :if => lambda { |*| true } }
|
26
26
|
band = band_class.new
|
27
27
|
band.from_hash({"fame"=>"oh yes"}, {:exclude => [:fame]})
|
28
|
-
|
28
|
+
assert_nil band.fame
|
29
29
|
end
|
30
30
|
|
31
31
|
it "executes block in instance context" do
|
@@ -44,20 +44,20 @@ class IfTest < MiniTest::Spec
|
|
44
44
|
subject { OpenStruct.new(:signed_contract => false, :label => "Fat") }
|
45
45
|
|
46
46
|
it "skips when false" do
|
47
|
-
subject.extend(representer).to_hash.must_equal({})
|
47
|
+
_(subject.extend(representer).to_hash).must_equal({})
|
48
48
|
end
|
49
49
|
|
50
50
|
it "represents when true" do
|
51
51
|
subject.signed_contract= true
|
52
|
-
subject.extend(representer).to_hash.must_equal({"label"=>"Fat"})
|
52
|
+
_(subject.extend(representer).to_hash).must_equal({"label"=>"Fat"})
|
53
53
|
end
|
54
54
|
|
55
55
|
it "works with decorator" do
|
56
56
|
rpr = representer
|
57
|
-
Class.new(Representable::Decorator) do
|
57
|
+
_(Class.new(Representable::Decorator) do
|
58
58
|
include Representable::Hash
|
59
59
|
include rpr
|
60
|
-
end.new(subject).to_hash.must_equal({})
|
60
|
+
end.new(subject).to_hash).must_equal({})
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -69,11 +69,11 @@ class IfTest < MiniTest::Spec
|
|
69
69
|
subject { OpenStruct.new(:name => "Outbound").extend(representer) }
|
70
70
|
|
71
71
|
it "works without specifying options" do
|
72
|
-
subject.to_hash.must_equal({})
|
72
|
+
_(subject.to_hash).must_equal({})
|
73
73
|
end
|
74
74
|
|
75
75
|
it "passes user options to block" do
|
76
|
-
subject.to_hash(user_options: { include_name: true }).must_equal({"name" => "Outbound"})
|
76
|
+
_(subject.to_hash(user_options: { include_name: true })).must_equal({"name" => "Outbound"})
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -17,22 +17,22 @@ class IncludeExcludeTest < Minitest::Spec
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
let
|
21
|
-
let
|
20
|
+
let(:song) { Song.new("Listless", Artist.new("7yearsbadluck", 1 )) }
|
21
|
+
let(:decorator) { representer.new(song) }
|
22
22
|
|
23
23
|
describe "#from_hash" do
|
24
24
|
it "accepts :exclude option" do
|
25
25
|
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, exclude: [:title])
|
26
26
|
|
27
|
-
song.title.must_equal "Listless"
|
28
|
-
song.artist.must_equal Artist.new(nil, 2)
|
27
|
+
_(song.title).must_equal "Listless"
|
28
|
+
_(song.artist).must_equal Artist.new(nil, 2)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "accepts :include option" do
|
32
32
|
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, include: [:title])
|
33
33
|
|
34
|
-
song.title.must_equal "Don't Smile In Trouble"
|
35
|
-
song.artist.must_equal Artist.new("7yearsbadluck", 1)
|
34
|
+
_(song.title).must_equal "Don't Smile In Trouble"
|
35
|
+
_(song.artist).must_equal Artist.new("7yearsbadluck", 1)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "accepts nested :exclude/:include option" do
|
@@ -44,30 +44,30 @@ class IncludeExcludeTest < Minitest::Spec
|
|
44
44
|
}
|
45
45
|
)
|
46
46
|
|
47
|
-
song.title.must_equal "Listless"
|
48
|
-
song.artist.must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
47
|
+
_(song.title).must_equal "Listless"
|
48
|
+
_(song.artist).must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
describe "#to_hash" do
|
53
53
|
it "accepts :exclude option" do
|
54
|
-
decorator.to_hash(exclude: [:title]).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
|
54
|
+
_(decorator.to_hash(exclude: [:title])).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
|
55
55
|
end
|
56
56
|
|
57
57
|
it "accepts :include option" do
|
58
|
-
decorator.to_hash(include: [:title]).must_equal({"title"=>"Listless"})
|
58
|
+
_(decorator.to_hash(include: [:title])).must_equal({"title"=>"Listless"})
|
59
59
|
end
|
60
60
|
|
61
61
|
it "accepts nested :exclude/:include option" do
|
62
62
|
decorator = representer.new(Song.new("Listless", Artist.new("7yearsbadluck", 1, [Song.new("C.O.A.B.I.E.T.L.")])))
|
63
63
|
|
64
|
-
decorator.to_hash(
|
64
|
+
_(decorator.to_hash(
|
65
65
|
exclude: [:title],
|
66
66
|
artist: {
|
67
67
|
exclude: [:id],
|
68
68
|
songs: { include: [:title] }
|
69
69
|
}
|
70
|
-
).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
70
|
+
)).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -82,7 +82,7 @@ class IncludeExcludeTest < Minitest::Spec
|
|
82
82
|
|
83
83
|
# FIXME: we should test all representable-options (:include, :exclude, ?)
|
84
84
|
|
85
|
-
Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
86
|
-
to_hash(:include => [:original]).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
85
|
+
_(Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
86
|
+
to_hash(:include => [:original])).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
87
87
|
end
|
88
88
|
end
|