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/config_test.rb
CHANGED
@@ -7,7 +7,7 @@ class ConfigTest < MiniTest::Spec
|
|
7
7
|
|
8
8
|
describe "wrapping" do
|
9
9
|
it "returns false per default" do
|
10
|
-
|
10
|
+
assert_nil subject.wrap_for("Punk", nil)
|
11
11
|
end
|
12
12
|
|
13
13
|
# it "infers a printable class name if set to true" do
|
@@ -23,13 +23,13 @@ class ConfigTest < MiniTest::Spec
|
|
23
23
|
|
24
24
|
describe "#[]" do
|
25
25
|
# does return nil for non-existent
|
26
|
-
it { subject[:hello].
|
26
|
+
it { _(subject[:hello]).must_be_nil }
|
27
27
|
end
|
28
28
|
|
29
29
|
# describe "#[]" do
|
30
30
|
# before { subject.add(:title, {:me => true}) }
|
31
31
|
|
32
|
-
# it { subject[:unknown].
|
32
|
+
# it { subject[:unknown].must_be_nil }
|
33
33
|
# it { subject.get(:title)[:me].must_equal true }
|
34
34
|
# it { subject["title"][:me].must_equal true }
|
35
35
|
# end
|
@@ -44,23 +44,23 @@ class ConfigTest < MiniTest::Spec
|
|
44
44
|
# #add returns Definition.`
|
45
45
|
subject = Representable::Config.new(Representable::Definition).add(:title, {:me => true})
|
46
46
|
|
47
|
-
subject.must_be_kind_of Representable::Definition
|
48
|
-
subject[:me].must_equal true
|
47
|
+
_(subject).must_be_kind_of Representable::Definition
|
48
|
+
_(subject[:me]).must_equal true
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
before { subject.add(:title, {:me => true}) }
|
53
53
|
|
54
54
|
# must be kind of Definition
|
55
|
-
it { subject.size.must_equal 1 }
|
56
|
-
it { subject.get(:title).name.must_equal "title" }
|
57
|
-
it { subject.get(:title)[:me].must_equal true }
|
55
|
+
it { _(subject.size).must_equal 1 }
|
56
|
+
it { _(subject.get(:title).name).must_equal "title" }
|
57
|
+
it { _(subject.get(:title)[:me]).must_equal true }
|
58
58
|
|
59
59
|
# this is actually tested in context in inherit_test.
|
60
60
|
it "overrides former definition" do
|
61
61
|
subject.add(:title, {:peer => Module})
|
62
|
-
subject.get(:title)[:me].
|
63
|
-
subject.get(:title)[:peer].must_equal Module
|
62
|
+
_(subject.get(:title)[:me]).must_be_nil
|
63
|
+
_(subject.get(:title)[:peer]).must_equal Module
|
64
64
|
end
|
65
65
|
|
66
66
|
describe "inherit: true" do
|
@@ -69,8 +69,8 @@ class ConfigTest < MiniTest::Spec
|
|
69
69
|
subject.add(:title, {:peer => Module, :inherit => true})
|
70
70
|
}
|
71
71
|
|
72
|
-
it { subject.get(:title)[:me].must_equal true }
|
73
|
-
it { subject.get(:title)[:peer].must_equal Module }
|
72
|
+
it { _(subject.get(:title)[:me]).must_equal true }
|
73
|
+
it { _(subject.get(:title)[:peer]).must_equal Module }
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -81,10 +81,10 @@ class ConfigTest < MiniTest::Spec
|
|
81
81
|
it do
|
82
82
|
subject.add(:title, {:me => true})
|
83
83
|
subject.add(:genre, {})
|
84
|
-
subject.get(:genre).must_be_kind_of Representable::Definition
|
84
|
+
_(subject.get(:genre)).must_be_kind_of Representable::Definition
|
85
85
|
|
86
86
|
subject.remove(:genre)
|
87
|
-
subject.get(:genre).
|
87
|
+
_(subject.get(:genre)).must_be_nil
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -95,16 +95,16 @@ class ConfigTest < MiniTest::Spec
|
|
95
95
|
it "what" do
|
96
96
|
definitions = []
|
97
97
|
subject.each { |dfn| definitions << dfn }
|
98
|
-
definitions.size.must_equal 1
|
99
|
-
definitions[0][:me].must_equal true
|
98
|
+
_(definitions.size).must_equal 1
|
99
|
+
_(definitions[0][:me]).must_equal true
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
103
|
describe "#options" do
|
104
|
-
it { subject.options.must_equal({}) }
|
104
|
+
it { _(subject.options).must_equal({}) }
|
105
105
|
it do
|
106
106
|
subject.options[:namespacing] = true
|
107
|
-
subject.options[:namespacing].must_equal true
|
107
|
+
_(subject.options[:namespacing]).must_equal true
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
@@ -115,8 +115,8 @@ class ConfigTest < MiniTest::Spec
|
|
115
115
|
title = subject.add(:title, {})
|
116
116
|
length = subject.add(:length, {})
|
117
117
|
|
118
|
-
subject.get(:title).must_equal title
|
119
|
-
subject.get(:length).must_equal length
|
118
|
+
_(subject.get(:title)).must_equal title
|
119
|
+
_(subject.get(:length)).must_equal length
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
@@ -6,7 +6,7 @@ class DecoratorScopeTest < MiniTest::Spec
|
|
6
6
|
property :title, :getter => lambda { |*| title_from_representer }, :decorator_scope => true
|
7
7
|
end
|
8
8
|
|
9
|
-
let
|
9
|
+
let(:representer_with_method) {
|
10
10
|
Module.new do
|
11
11
|
include Representable::Hash
|
12
12
|
property :title, :decorator_scope => true
|
@@ -15,14 +15,14 @@ class DecoratorScopeTest < MiniTest::Spec
|
|
15
15
|
}
|
16
16
|
|
17
17
|
it "executes lambdas in represented context" do
|
18
|
-
Class.new do
|
18
|
+
_(Class.new do
|
19
19
|
def title_from_representer
|
20
20
|
"Sounds Of Silence"
|
21
21
|
end
|
22
|
-
end.new.extend(representer).to_hash.must_equal({"title"=>"Sounds Of Silence"})
|
22
|
+
end.new.extend(representer).to_hash).must_equal({"title"=>"Sounds Of Silence"})
|
23
23
|
end
|
24
24
|
|
25
25
|
it "executes method in represented context" do
|
26
|
-
Object.new.extend(representer_with_method).to_hash.must_equal({"title"=>"Crystal Planet"})
|
26
|
+
_(Object.new.extend(representer_with_method).to_hash).must_equal({"title"=>"Crystal Planet"})
|
27
27
|
end
|
28
28
|
end
|
data/test/decorator_test.rb
CHANGED
@@ -12,46 +12,59 @@ class DecoratorTest < MiniTest::Spec
|
|
12
12
|
collection :songs, :class => Song, :extend => SongRepresentation
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
class RatingRepresentation < Representable::Decorator
|
16
|
+
include Representable::JSON
|
17
|
+
|
18
|
+
property :system
|
19
|
+
property :value
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:song) { Song.new("Mama, I'm Coming Home") }
|
23
|
+
let(:album) { Album.new([song]) }
|
24
|
+
|
25
|
+
let(:rating) { OpenStruct.new(system: 'MPAA', value: 'R') }
|
17
26
|
|
18
27
|
describe "inheritance" do
|
19
|
-
let
|
28
|
+
let(:inherited_decorator) do
|
20
29
|
Class.new(AlbumRepresentation) do
|
21
30
|
property :best_song
|
22
31
|
end.new(Album.new([song], "Stand Up"))
|
23
32
|
end
|
24
33
|
|
25
|
-
it { inherited_decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}], "best_song"=>"Stand Up"}) }
|
34
|
+
it { _(inherited_decorator.to_hash).must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}], "best_song"=>"Stand Up"}) }
|
26
35
|
end
|
27
36
|
|
28
|
-
let
|
37
|
+
let(:decorator) { AlbumRepresentation.new(album) }
|
38
|
+
|
39
|
+
let(:rating_decorator) { RatingRepresentation.new(rating) }
|
29
40
|
|
30
41
|
it "renders" do
|
31
|
-
decorator.to_hash.must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
|
32
|
-
album.wont_respond_to :to_hash
|
33
|
-
song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
|
42
|
+
_(decorator.to_hash).must_equal({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})
|
43
|
+
_(album).wont_respond_to :to_hash
|
44
|
+
_(song).wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
|
34
45
|
# no @representable_attrs in decorated objects
|
35
|
-
song.
|
46
|
+
_(song).wont_be(:instance_variable_defined?, :@representable_attrs)
|
47
|
+
|
48
|
+
_(rating_decorator.to_hash).must_equal({"system" => "MPAA", "value" => "R"})
|
36
49
|
end
|
37
50
|
|
38
51
|
describe "#from_hash" do
|
39
52
|
it "returns represented" do
|
40
|
-
decorator.from_hash({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]}).must_equal album
|
53
|
+
_(decorator.from_hash({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})).must_equal album
|
41
54
|
end
|
42
55
|
|
43
56
|
it "parses" do
|
44
57
|
decorator.from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
|
45
|
-
album.songs.first.must_be_kind_of Song
|
46
|
-
album.songs.must_equal [Song.new("Atomic Garden")]
|
47
|
-
album.wont_respond_to :to_hash
|
48
|
-
song.wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
|
58
|
+
_(album.songs.first).must_be_kind_of Song
|
59
|
+
_(album.songs).must_equal [Song.new("Atomic Garden")]
|
60
|
+
_(album).wont_respond_to :to_hash
|
61
|
+
_(song).wont_respond_to :to_hash # DISCUSS: weak test, how to assert blank slate?
|
49
62
|
end
|
50
63
|
end
|
51
64
|
|
52
65
|
describe "#decorated" do
|
53
66
|
it "is aliased to #represented" do
|
54
|
-
AlbumRepresentation.prepare(album).decorated.must_equal album
|
67
|
+
_(AlbumRepresentation.prepare(album).decorated).must_equal album
|
55
68
|
end
|
56
69
|
end
|
57
70
|
|
@@ -67,8 +80,8 @@ class DecoratorTest < MiniTest::Spec
|
|
67
80
|
representer.new(album).from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
|
68
81
|
|
69
82
|
# no @representable_attrs in decorated objects
|
70
|
-
song.
|
71
|
-
album.
|
83
|
+
_(song).wont_be(:instance_variable_defined?, :@representable_attrs)
|
84
|
+
_(album).wont_be(:instance_variable_defined?, :@representable_attrs)
|
72
85
|
end
|
73
86
|
end
|
74
87
|
end
|
@@ -89,8 +102,8 @@ class InheritanceWithDecoratorTest < MiniTest::Spec
|
|
89
102
|
end
|
90
103
|
|
91
104
|
it do
|
92
|
-
Twin.representer_class.definitions.size.must_equal 0
|
93
|
-
Album.representer_class.definitions.size.must_equal 1
|
94
|
-
Song.representer_class.definitions.size.must_equal 0
|
105
|
+
_(Twin.representer_class.definitions.size).must_equal 0
|
106
|
+
_(Album.representer_class.definitions.size).must_equal 1
|
107
|
+
_(Song.representer_class.definitions.size).must_equal 0
|
95
108
|
end
|
96
109
|
end
|
data/test/default_test.rb
CHANGED
@@ -9,26 +9,26 @@ class DefaultTest < MiniTest::Spec
|
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#from_hash" do
|
12
|
-
let
|
12
|
+
let(:song) { Song.new.extend(representer) }
|
13
13
|
|
14
|
-
it { song.from_hash({}).must_equal Song.new(nil, "Huber Breeze") }
|
14
|
+
it { _(song.from_hash({})).must_equal Song.new(nil, "Huber Breeze") }
|
15
15
|
# default doesn't apply when empty string.
|
16
|
-
it { song.from_hash({"title"=>""}).must_equal Song.new(nil, "") }
|
17
|
-
it { song.from_hash({"title"=>nil}).must_equal Song.new(nil, nil) }
|
18
|
-
it { song.from_hash({"title"=>"Blindfold"}).must_equal Song.new(nil, "Blindfold") }
|
16
|
+
it { _(song.from_hash({"title"=>""})).must_equal Song.new(nil, "") }
|
17
|
+
it { _(song.from_hash({"title"=>nil})).must_equal Song.new(nil, nil) }
|
18
|
+
it { _(song.from_hash({"title"=>"Blindfold"})).must_equal Song.new(nil, "Blindfold") }
|
19
19
|
end
|
20
20
|
|
21
21
|
describe "#to_json" do
|
22
22
|
it "uses :default when not available from object" do
|
23
|
-
Song.new.extend(representer).to_hash.must_equal({"title"=>"Huber Breeze"})
|
23
|
+
_(Song.new.extend(representer).to_hash).must_equal({"title"=>"Huber Breeze"})
|
24
24
|
end
|
25
25
|
|
26
26
|
it "uses value from represented object when present" do
|
27
|
-
Song.new(nil, "After The War").extend(representer).to_hash.must_equal({"title"=>"After The War"})
|
27
|
+
_(Song.new(nil, "After The War").extend(representer).to_hash).must_equal({"title"=>"After The War"})
|
28
28
|
end
|
29
29
|
|
30
30
|
it "uses value from represented object when emtpy string" do
|
31
|
-
Song.new(nil, "").extend(representer).to_hash.must_equal({"title"=>""})
|
31
|
+
_(Song.new(nil, "").extend(representer).to_hash).must_equal({"title"=>""})
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class DefaultsOptionsTest < BaseTest
|
4
|
-
let
|
5
|
-
let
|
6
|
-
let
|
4
|
+
let(:format) { :hash }
|
5
|
+
let(:song) { Struct.new(:title, :author_name, :song_volume, :description).new("Revolution", "Some author", 20, nil) }
|
6
|
+
let(:prepared) { representer.prepare song }
|
7
7
|
|
8
8
|
describe "hash options combined with dynamic options" do
|
9
9
|
representer! do
|
data/test/definition_test.rb
CHANGED
@@ -6,42 +6,40 @@ class DefinitionTest < MiniTest::Spec
|
|
6
6
|
# TODO: test that we DON'T clone options, that must happen in
|
7
7
|
describe "#initialize" do
|
8
8
|
it do
|
9
|
-
opts = nil
|
10
|
-
|
11
9
|
# new yields the defaultized options HASH.
|
12
10
|
definition = Definition.new(:song, :extend => Module) do |options|
|
13
11
|
options[:awesome] = true
|
14
12
|
options[:parse_filter] << 1
|
15
13
|
|
16
14
|
# default variables
|
17
|
-
options[:as].
|
18
|
-
options[:extend].must_equal Module
|
15
|
+
_(options[:as]).must_be_nil
|
16
|
+
_(options[:extend]).must_equal Module
|
19
17
|
end
|
20
|
-
definition.name.must_equal "song"
|
18
|
+
_(definition.name).must_equal "song"
|
21
19
|
|
22
20
|
#
|
23
|
-
definition[:awesome].must_equal true
|
24
|
-
definition[:parse_filter].must_equal Representable::Pipeline[1]
|
25
|
-
definition[:render_filter].must_equal Representable::Pipeline[]
|
21
|
+
_(definition[:awesome]).must_equal true
|
22
|
+
_(definition[:parse_filter]).must_equal Representable::Pipeline[1]
|
23
|
+
_(definition[:render_filter]).must_equal Representable::Pipeline[]
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
27
|
describe "#[]" do
|
30
|
-
let
|
28
|
+
let(:definition) { Definition.new(:song) }
|
31
29
|
# default is nil.
|
32
|
-
it { definition[:bla].
|
30
|
+
it { _(definition[:bla]).must_be_nil }
|
33
31
|
end
|
34
32
|
|
35
33
|
# merge!
|
36
34
|
describe "#merge!" do
|
37
|
-
let
|
35
|
+
let(:definition) { Definition.new(:song, :whatever => true) }
|
38
36
|
|
39
37
|
# merges new options.
|
40
|
-
it { definition.merge!(:something => true)[:something].must_equal true }
|
38
|
+
it { _(definition.merge!(:something => true)[:something]).must_equal true }
|
41
39
|
# doesn't override original options.
|
42
|
-
it { definition.merge!({:something => true})[:whatever].must_equal true }
|
40
|
+
it { _(definition.merge!({:something => true})[:whatever]).must_equal true }
|
43
41
|
# override original when passed in #merge!.
|
44
|
-
it { definition.merge!({:whatever => false})[:whatever].must_equal false }
|
42
|
+
it { _(definition.merge!({:whatever => false})[:whatever]).must_equal false }
|
45
43
|
|
46
44
|
# with block
|
47
45
|
it do
|
@@ -54,47 +52,47 @@ class DefinitionTest < MiniTest::Spec
|
|
54
52
|
# options[:extend].must_equal Module
|
55
53
|
end
|
56
54
|
|
57
|
-
definition[:awesome].must_equal true
|
58
|
-
definition[:something].must_equal true
|
59
|
-
definition[:render_filter].must_equal Representable::Pipeline[1]
|
60
|
-
definition[:parse_filter].must_equal Representable::Pipeline[]
|
55
|
+
_(definition[:awesome]).must_equal true
|
56
|
+
_(definition[:something]).must_equal true
|
57
|
+
_(definition[:render_filter]).must_equal Representable::Pipeline[1]
|
58
|
+
_(definition[:parse_filter]).must_equal Representable::Pipeline[]
|
61
59
|
end
|
62
60
|
|
63
61
|
describe "with :parse_filter" do
|
64
|
-
let
|
62
|
+
let(:definition) { Definition.new(:title, :parse_filter => 1) }
|
65
63
|
|
66
64
|
# merges :parse_filter and :render_filter.
|
67
65
|
it do
|
68
66
|
merged = definition.merge!(:parse_filter => 2)[:parse_filter]
|
69
67
|
|
70
|
-
merged.must_be_kind_of Representable::Pipeline
|
71
|
-
merged.size.must_equal 2
|
68
|
+
_(merged).must_be_kind_of Representable::Pipeline
|
69
|
+
_(merged.size).must_equal 2
|
72
70
|
end
|
73
71
|
|
74
72
|
# :parse_filter can also be array.
|
75
|
-
it { definition.merge!(:parse_filter => [2, 3])[:parse_filter].size.must_equal 3 }
|
73
|
+
it { _(definition.merge!(:parse_filter => [2, 3])[:parse_filter].size).must_equal 3 }
|
76
74
|
end
|
77
75
|
|
78
76
|
# does not change arguments
|
79
77
|
it do
|
80
78
|
Definition.new(:title).merge!(options = {:whatever => 1})
|
81
|
-
options.must_equal(:whatever => 1)
|
79
|
+
_(options).must_equal(:whatever => 1)
|
82
80
|
end
|
83
81
|
end
|
84
82
|
|
85
83
|
|
86
84
|
# delete!
|
87
85
|
describe "#delete!" do
|
88
|
-
let
|
86
|
+
let(:definition) { Definition.new(:song, serialize: "remove me!") }
|
89
87
|
|
90
|
-
before { definition[:serialize].
|
88
|
+
before { _(definition[:serialize].(nil)).must_equal "remove me!" }
|
91
89
|
|
92
|
-
it { definition.delete!(:serialize)[:serialize].
|
90
|
+
it { _(definition.delete!(:serialize)[:serialize]).must_be_nil }
|
93
91
|
end
|
94
92
|
|
95
93
|
# #inspect
|
96
94
|
describe "#inspect" do
|
97
|
-
it { Definition.new(:songs).inspect.must_equal "#<Representable::Definition ==>songs @options={:name=>\"songs\", :parse_filter=>[], :render_filter=>[]}>" }
|
95
|
+
it { _(Definition.new(:songs).inspect).must_equal "#<Representable::Definition ==>songs @options={:name=>\"songs\", :parse_filter=>[], :render_filter=>[]}>" }
|
98
96
|
end
|
99
97
|
|
100
98
|
|
@@ -104,13 +102,13 @@ class DefinitionTest < MiniTest::Spec
|
|
104
102
|
end
|
105
103
|
|
106
104
|
it "responds to #representer_module" do
|
107
|
-
|
105
|
+
assert_nil Representable::Definition.new(:song).representer_module
|
108
106
|
assert_equal Hash, Representable::Definition.new(:song, :extend => Hash).representer_module
|
109
107
|
end
|
110
108
|
|
111
109
|
describe "#typed?" do
|
112
110
|
it "is false per default" do
|
113
|
-
|
111
|
+
refute @def.typed?
|
114
112
|
end
|
115
113
|
|
116
114
|
it "is true when :class is present" do
|
@@ -129,7 +127,7 @@ class DefinitionTest < MiniTest::Spec
|
|
129
127
|
|
130
128
|
describe "#representable?" do
|
131
129
|
it { assert Definition.new(:song, :representable => true).representable? }
|
132
|
-
it { Definition.new(:song, :representable => true, :extend => Object).representable
|
130
|
+
it { _(Definition.new(:song, :representable => true, :extend => Object).representable?).must_equal true }
|
133
131
|
it { refute Definition.new(:song, :representable => false, :extend => Object).representable? }
|
134
132
|
it { assert Definition.new(:song, :extend => Object).representable? }
|
135
133
|
it { refute Definition.new(:song).representable? }
|
@@ -152,16 +150,16 @@ class DefinitionTest < MiniTest::Spec
|
|
152
150
|
it do
|
153
151
|
dfn = Representable::Definition.new(:songs, nested: Module)
|
154
152
|
assert dfn.typed?
|
155
|
-
dfn[:extend].(nil).must_equal Module
|
153
|
+
_(dfn[:extend].(nil)).must_equal Module
|
156
154
|
end
|
157
155
|
end
|
158
156
|
|
159
157
|
|
160
158
|
describe "#clone" do
|
161
|
-
subject { Representable::Definition.new(:title, :volume => 9, :clonable =>
|
159
|
+
subject { Representable::Definition.new(:title, :volume => 9, :clonable => ::Representable::Option(1)) }
|
162
160
|
|
163
|
-
it { subject.clone.must_be_kind_of Representable::Definition }
|
164
|
-
it { subject.clone[:clonable].
|
161
|
+
it { _(subject.clone).must_be_kind_of Representable::Definition }
|
162
|
+
it { _(subject.clone[:clonable].(nil)).must_equal 1 }
|
165
163
|
|
166
164
|
it "clones @options" do
|
167
165
|
@def.merge!(:volume => 9)
|
@@ -177,11 +175,11 @@ class DefinitionTest < MiniTest::Spec
|
|
177
175
|
|
178
176
|
describe "#has_default?" do
|
179
177
|
it "returns false if no :default set" do
|
180
|
-
|
178
|
+
refute Representable::Definition.new(:song).has_default?
|
181
179
|
end
|
182
180
|
|
183
181
|
it "returns true if :default set" do
|
184
|
-
|
182
|
+
assert Representable::Definition.new(:song, :default => nil).has_default?
|
185
183
|
end
|
186
184
|
end
|
187
185
|
|
@@ -192,14 +190,14 @@ class DefinitionTest < MiniTest::Spec
|
|
192
190
|
end
|
193
191
|
|
194
192
|
it "returns false when :binding is not set" do
|
195
|
-
|
193
|
+
refute Representable::Definition.new(:songs)[:binding]
|
196
194
|
end
|
197
195
|
end
|
198
196
|
|
199
197
|
describe "#create_binding" do
|
200
198
|
it "executes the block (without special context)" do
|
201
199
|
definition = Representable::Definition.new(:title, :binding => lambda { |*args| @binding = Representable::Binding.new(*args) })
|
202
|
-
definition.create_binding.must_equal @binding
|
200
|
+
_(definition.create_binding).must_equal @binding
|
203
201
|
end
|
204
202
|
end
|
205
203
|
|
@@ -217,7 +215,7 @@ class DefinitionTest < MiniTest::Spec
|
|
217
215
|
describe ":default => value" do
|
218
216
|
it "responds to #default" do
|
219
217
|
@def = Representable::Definition.new(:song)
|
220
|
-
|
218
|
+
assert_nil @def[:default]
|
221
219
|
end
|
222
220
|
|
223
221
|
it "accepts a default value" do
|
@@ -233,7 +231,7 @@ class DefinitionTest < MiniTest::Spec
|
|
233
231
|
|
234
232
|
it "responds to #hash?" do
|
235
233
|
assert @def.hash?
|
236
|
-
|
234
|
+
refute Representable::Definition.new(:songs).hash?
|
237
235
|
end
|
238
236
|
end
|
239
237
|
|
data/test/examples/object.rb
CHANGED
@@ -7,8 +7,6 @@ require "pp"
|
|
7
7
|
source = OpenStruct.new(name: "30 Years Live", songs: [
|
8
8
|
OpenStruct.new(id: 1, title: "Dear Beloved"), OpenStruct.new(id: 2, title: "Fuck Armageddon")])
|
9
9
|
|
10
|
-
pp source
|
11
|
-
|
12
10
|
require "representable/object"
|
13
11
|
|
14
12
|
class AlbumRepresenter < Representable::Decorator
|
@@ -26,6 +24,4 @@ Song = Struct.new(:title)
|
|
26
24
|
|
27
25
|
target = Album.new
|
28
26
|
|
29
|
-
AlbumRepresenter.new(target).from_object(source)
|
30
|
-
|
31
|
-
pp target
|
27
|
+
AlbumRepresenter.new(target).from_object(source)
|
data/test/exec_context_test.rb
CHANGED
@@ -7,8 +7,8 @@ class ExecContextTest < MiniTest::Spec
|
|
7
7
|
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
8
8
|
) do |format, mod, input, output|
|
9
9
|
|
10
|
-
let
|
11
|
-
let
|
10
|
+
let(:song) { representer.prepare(Song.new("Timing")) }
|
11
|
+
let(:format) { format }
|
12
12
|
|
13
13
|
|
14
14
|
describe "exec_context: nil" do
|
@@ -17,7 +17,7 @@ class ExecContextTest < MiniTest::Spec
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it { render(song).must_equal_document output }
|
20
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
20
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
21
21
|
end
|
22
22
|
|
23
23
|
|
@@ -27,7 +27,7 @@ class ExecContextTest < MiniTest::Spec
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it { render(song).must_equal_document output }
|
30
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
30
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
31
31
|
end
|
32
32
|
|
33
33
|
|
@@ -41,7 +41,7 @@ class ExecContextTest < MiniTest::Spec
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
44
|
-
it { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
|
44
|
+
it { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
45
45
|
end
|
46
46
|
|
47
47
|
|
@@ -53,7 +53,7 @@ class ExecContextTest < MiniTest::Spec
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it { render(song).must_equal_document output }
|
56
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
56
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
57
57
|
end
|
58
58
|
|
59
59
|
|
@@ -71,7 +71,7 @@ class ExecContextTest < MiniTest::Spec
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it { render(song).must_equal_document({Representable::Decorator=>"Timebomb"}) }
|
74
|
-
it { parse(song, {Representable::Decorator=>"Listless"}).name.must_equal "Listless" }
|
74
|
+
it { _(parse(song, {Representable::Decorator=>"Listless"}).name).must_equal "Listless" }
|
75
75
|
end
|
76
76
|
|
77
77
|
|
@@ -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("xxx") { 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/features_test.rb
CHANGED
@@ -20,23 +20,23 @@ class FeaturesTest < MiniTest::Spec
|
|
20
20
|
end
|
21
21
|
}
|
22
22
|
|
23
|
-
let
|
23
|
+
let(:song) { OpenStruct.new(:details => Object.new) }
|
24
24
|
|
25
25
|
describe "Module" do
|
26
26
|
representer! do
|
27
|
-
instance_exec
|
27
|
+
instance_exec(&definition)
|
28
28
|
end
|
29
29
|
|
30
|
-
it { song.extend(representer).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
30
|
+
it { _(song.extend(representer).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
31
31
|
end
|
32
32
|
|
33
33
|
|
34
34
|
describe "Decorator" do
|
35
35
|
representer!(:decorator => true) do
|
36
|
-
instance_exec
|
36
|
+
instance_exec(&definition)
|
37
37
|
end
|
38
38
|
|
39
|
-
it { representer.new(song).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
39
|
+
it { _(representer.new(song).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -65,6 +65,6 @@ class FeatureInclusionOrderTest < MiniTest::Spec
|
|
65
65
|
end
|
66
66
|
|
67
67
|
it do
|
68
|
-
representer.new(OpenStruct.new(song: Object)).to_hash.must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
|
68
|
+
_(representer.new(OpenStruct.new(song: Object)).to_hash).must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
|
69
69
|
end
|
70
70
|
end
|
data/test/filter_test.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class FilterPipelineTest < MiniTest::Spec
|
4
|
-
let
|
5
|
-
let
|
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("Horowitz", {}).must_equal "2: 1: Horowitz" }
|
9
|
+
it { _(subject.call("Horowitz", {})).must_equal "2: 1: Horowitz" }
|
10
10
|
end
|
11
11
|
|
12
12
|
|
@@ -22,11 +22,11 @@ class FilterTest < MiniTest::Spec
|
|
22
22
|
# gets doc and options.
|
23
23
|
it {
|
24
24
|
song = OpenStruct.new.extend(representer).from_hash("title" => "VULCAN EARS", "track" => "Nine")
|
25
|
-
song.title.must_equal "VULCAN EARS"
|
26
|
-
song.track.must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
|
25
|
+
_(song.title).must_equal "VULCAN EARS"
|
26
|
+
_(song.track).must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
|
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\"},{}"}) }
|
29
|
+
it { _(OpenStruct.new("title" => "vulcan ears", "track" => "Nine").extend(representer).to_hash).must_equal( {"title"=>"vulcan ears", "track"=>"NINE,{\"title\"=>\"vulcan ears\"},{}"}) }
|
30
30
|
|
31
31
|
|
32
32
|
describe "#parse_filter" do
|
@@ -41,8 +41,8 @@ class FilterTest < MiniTest::Spec
|
|
41
41
|
end
|
42
42
|
|
43
43
|
# order matters.
|
44
|
-
it { OpenStruct.new.extend(representer).from_hash("track" => "Nine").track.must_equal "Nine-1-2" }
|
45
|
-
it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
|
44
|
+
it { _(OpenStruct.new.extend(representer).from_hash("track" => "Nine").track).must_equal "Nine-1-2" }
|
45
|
+
it { _(OpenStruct.new("track" => "Nine").extend(representer).to_hash).must_equal({"track"=>"Nine-1-2"}) }
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|