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/lonely_test.rb
CHANGED
@@ -12,7 +12,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
12
12
|
) do |format, mod, output, input|
|
13
13
|
|
14
14
|
describe "[#{format}] lonely collection, render-only" do # TODO: introduce :representable option?
|
15
|
-
let
|
15
|
+
let(:format) { format }
|
16
16
|
|
17
17
|
representer!(module: mod) do
|
18
18
|
items do
|
@@ -20,7 +20,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
let
|
23
|
+
let(:album) { [Song.new("Resist Stance"), Song.new("Suffer")].extend(representer) }
|
24
24
|
|
25
25
|
it "calls #to_hash on song instances, nothing else" do
|
26
26
|
render(album).must_equal_document(output)
|
@@ -36,14 +36,14 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
36
36
|
property :name
|
37
37
|
end
|
38
38
|
|
39
|
-
let
|
39
|
+
let(:decorator) { rpr = representer; Class.new(Representable::Decorator) { include Representable::Hash; include rpr } }
|
40
40
|
|
41
41
|
describe "JSON::Collection" do
|
42
|
-
let
|
43
|
-
let
|
42
|
+
let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
|
43
|
+
let(:json) { "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]" }
|
44
44
|
|
45
45
|
describe "with contained objects" do
|
46
|
-
let
|
46
|
+
let(:representer) {
|
47
47
|
Module.new do
|
48
48
|
include Representable::JSON::Collection
|
49
49
|
items :class => Song, :extend => SongRepresenter
|
@@ -60,11 +60,11 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "parses array" do
|
63
|
-
[].extend(representer).from_json(json).must_equal songs
|
63
|
+
_([].extend(representer).from_json(json)).must_equal songs
|
64
64
|
end
|
65
65
|
|
66
66
|
it "parses array with decorator" do
|
67
|
-
decorator.new([]).from_json(json).must_equal songs
|
67
|
+
_(decorator.new([]).from_json(json)).must_equal songs
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -75,25 +75,25 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
-
it { songs.extend(representer).to_json.must_equal json }
|
79
|
-
it { [].extend(representer).from_json(json).must_equal songs }
|
78
|
+
it { _(songs.extend(representer).to_json).must_equal json }
|
79
|
+
it { _([].extend(representer).from_json(json)).must_equal songs }
|
80
80
|
end
|
81
81
|
|
82
82
|
describe "with contained text" do
|
83
|
-
let
|
83
|
+
let(:representer) {
|
84
84
|
Module.new do
|
85
85
|
include Representable::JSON::Collection
|
86
86
|
end
|
87
87
|
}
|
88
|
-
let
|
89
|
-
let
|
88
|
+
let(:songs) { ["Days Go By", "Can't Take Them All"] }
|
89
|
+
let(:json) { "[\"Days Go By\",\"Can't Take Them All\"]" }
|
90
90
|
|
91
91
|
it "renders contained items #to_json" do
|
92
92
|
assert_json json, songs.extend(representer).to_json
|
93
93
|
end
|
94
94
|
|
95
95
|
it "returns objects array from #from_json" do
|
96
|
-
[].extend(representer).from_json(json).must_equal songs
|
96
|
+
_([].extend(representer).from_json(json)).must_equal songs
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
@@ -112,28 +112,28 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
112
112
|
end
|
113
113
|
|
114
114
|
|
115
|
-
it { [1,2].extend(representer).to_hash(user_options: {one: One, two: Two}).must_equal(["One: 1", "Two: 2"]) }
|
115
|
+
it { _([1,2].extend(representer).to_hash(user_options: {one: One, two: Two})).must_equal(["One: 1", "Two: 2"]) }
|
116
116
|
end
|
117
117
|
|
118
118
|
|
119
119
|
describe "JSON::Hash" do # TODO: move to HashTest.
|
120
120
|
describe "with contained objects" do
|
121
|
-
let
|
121
|
+
let(:representer) {
|
122
122
|
Module.new do
|
123
123
|
include Representable::JSON::Hash
|
124
124
|
values :class => Song, :extend => SongRepresenter
|
125
125
|
end
|
126
126
|
}
|
127
|
-
let
|
128
|
-
let
|
127
|
+
let(:json) { "{\"one\":{\"name\":\"Days Go By\"},\"two\":{\"name\":\"Can't Take Them All\"}}" }
|
128
|
+
let(:songs) { {"one" => Song.new("Days Go By"), "two" => Song.new("Can't Take Them All")} }
|
129
129
|
|
130
130
|
describe "#to_json" do
|
131
131
|
it "renders hash" do
|
132
|
-
songs.extend(representer).to_json.must_equal json
|
132
|
+
_(songs.extend(representer).to_json).must_equal json
|
133
133
|
end
|
134
134
|
|
135
135
|
it "renders hash with decorator" do
|
136
|
-
decorator.new(songs).to_json.must_equal json
|
136
|
+
_(decorator.new(songs).to_json).must_equal json
|
137
137
|
end
|
138
138
|
|
139
139
|
it "respects :exclude" do
|
@@ -147,11 +147,11 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
147
147
|
|
148
148
|
describe "#from_json" do
|
149
149
|
it "returns objects array" do
|
150
|
-
{}.extend(representer).from_json(json).must_equal songs
|
150
|
+
_({}.extend(representer).from_json(json)).must_equal songs
|
151
151
|
end
|
152
152
|
|
153
153
|
it "parses hash with decorator" do
|
154
|
-
decorator.new({}).from_json(json).must_equal songs
|
154
|
+
_(decorator.new({}).from_json(json)).must_equal songs
|
155
155
|
end
|
156
156
|
|
157
157
|
it "respects :exclude" do
|
@@ -171,23 +171,23 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
it { songs.extend(representer).to_json.must_equal json }
|
175
|
-
it { {}.extend(representer).from_json(json).must_equal songs }
|
174
|
+
it { _(songs.extend(representer).to_json).must_equal json }
|
175
|
+
it { _({}.extend(representer).from_json(json)).must_equal songs }
|
176
176
|
end
|
177
177
|
end
|
178
178
|
|
179
179
|
|
180
180
|
describe "with scalar" do
|
181
|
-
let
|
181
|
+
let(:representer) {
|
182
182
|
Module.new do
|
183
183
|
include Representable::JSON::Hash
|
184
184
|
end
|
185
185
|
}
|
186
|
-
let
|
187
|
-
let
|
186
|
+
let(:json) { %{{"one":1,"two":2}} }
|
187
|
+
let(:data) { {one: 2, two: 3} }
|
188
188
|
|
189
189
|
describe "#to_json" do
|
190
|
-
it { data.extend(representer).to_json.must_equal %{{"one":2,"two":3}} }
|
190
|
+
it { _(data.extend(representer).to_json).must_equal %{{"one":2,"two":3}} }
|
191
191
|
|
192
192
|
# it "respects :exclude" do
|
193
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])
|
@@ -199,7 +199,7 @@ class LonelyRepresenterTest < MiniTest::Spec
|
|
199
199
|
end
|
200
200
|
|
201
201
|
describe "#from_json" do # FIXME: what's the point of this?
|
202
|
-
it { data.extend(representer).from_hash(data).must_equal data }
|
202
|
+
it { _(data.extend(representer).from_hash(data)).must_equal data }
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
@@ -234,6 +234,6 @@ class CollectionWithIncludeTest < MiniTest::Spec
|
|
234
234
|
end
|
235
235
|
end
|
236
236
|
|
237
|
-
it { representer.new([Song.new(1, "ACAB")]).to_hash.must_equal([{"id"=>1, "title"=>"ACAB"}]) }
|
238
|
-
it { representer.new([Song.new(1, "ACAB")]).to_hash(include: [:title]).must_equal([{"title"=>"ACAB"}]) }
|
237
|
+
it { _(representer.new([Song.new(1, "ACAB")]).to_hash).must_equal([{"id"=>1, "title"=>"ACAB"}]) }
|
238
|
+
it { _(representer.new([Song.new(1, "ACAB")]).to_hash(include: [:title])).must_equal([{"title"=>"ACAB"}]) }
|
239
239
|
end
|
data/test/nested_test.rb
CHANGED
@@ -11,7 +11,7 @@ class NestedTest < MiniTest::Spec
|
|
11
11
|
|
12
12
|
[false, true].each do |is_decorator|
|
13
13
|
describe "::nested with (inline representer|decorator): #{is_decorator}" do
|
14
|
-
let
|
14
|
+
let(:format) { format }
|
15
15
|
|
16
16
|
representer!(:module => mod, :decorator => is_decorator) do
|
17
17
|
nested :label do
|
@@ -27,29 +27,29 @@ class NestedTest < MiniTest::Spec
|
|
27
27
|
# self.representation_wrap = :album if format == :xml
|
28
28
|
end
|
29
29
|
|
30
|
-
let
|
31
|
-
let
|
30
|
+
let(:album) { Album.new("Epitaph", "Brett Gurewitz", 19) }
|
31
|
+
let(:decorator) { representer.prepare(album) }
|
32
32
|
|
33
33
|
it "renders nested Album-properties in separate section" do
|
34
34
|
render(decorator).must_equal_document output
|
35
35
|
|
36
36
|
# do not use extend on the nested object. # FIXME: make this a proper test with two describes instead of this pseudo-meta stuff.
|
37
37
|
if is_decorator==true
|
38
|
-
album.wont_be_kind_of(Representable::Hash)
|
38
|
+
_(album).wont_be_kind_of(Representable::Hash)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
it "parses nested properties to Album instance" do
|
43
43
|
album = parse(representer.prepare(Album.new), output)
|
44
|
-
album.label.must_equal "Epitaph"
|
45
|
-
album.owner.must_equal "Brett Gurewitz"
|
44
|
+
_(album.label).must_equal "Epitaph"
|
45
|
+
_(album.owner).must_equal "Brett Gurewitz"
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
|
51
51
|
describe "Decorator ::nested with extend:" do
|
52
|
-
let
|
52
|
+
let(:format) { format }
|
53
53
|
|
54
54
|
representer!(:name => :label_rpr) do
|
55
55
|
include mod
|
@@ -67,7 +67,7 @@ class NestedTest < MiniTest::Spec
|
|
67
67
|
self.representation_wrap = :album if format == :xml
|
68
68
|
end
|
69
69
|
|
70
|
-
let
|
70
|
+
let(:album) { representer.prepare(Album.new("Epitaph", "Brett Gurewitz", 19)) }
|
71
71
|
|
72
72
|
# TODO: shared example with above.
|
73
73
|
it "renders nested Album-properties in separate section" do
|
@@ -76,9 +76,9 @@ class NestedTest < MiniTest::Spec
|
|
76
76
|
|
77
77
|
it "parses nested properties to Album instance" do
|
78
78
|
album = parse(representer.prepare(Album.new), output)
|
79
|
-
album.label.must_equal "Epitaph"
|
80
|
-
album.owner.must_equal "Brett Gurewitz"
|
81
|
-
album.amount.must_equal 19
|
79
|
+
_(album.label).must_equal "Epitaph"
|
80
|
+
_(album.owner).must_equal "Brett Gurewitz"
|
81
|
+
_(album.amount).must_equal 19
|
82
82
|
end
|
83
83
|
end
|
84
84
|
end
|
@@ -99,10 +99,10 @@ class NestedTest < MiniTest::Spec
|
|
99
99
|
nested :label, :inherit => true, :as => "Label"
|
100
100
|
end
|
101
101
|
|
102
|
-
let
|
102
|
+
let(:album) { representer.prepare(Album.new("Epitaph", "Brett Gurewitz", 19)) }
|
103
103
|
|
104
104
|
it "renders nested Album-properties in separate section" do
|
105
|
-
representer.prepare(album).to_hash.must_equal({"Label"=>{"owner"=>"Brett Gurewitz"}})
|
105
|
+
_(representer.prepare(album).to_hash).must_equal({"Label"=>{"owner"=>"Brett Gurewitz"}})
|
106
106
|
end
|
107
107
|
|
108
108
|
# it "parses nested properties to Album instance" do
|
data/test/object_test.rb
CHANGED
@@ -18,23 +18,23 @@ class ObjectTest < MiniTest::Spec
|
|
18
18
|
# TODO: collection
|
19
19
|
end
|
20
20
|
|
21
|
-
let
|
22
|
-
let
|
21
|
+
let(:source) { Song.new("The King Is Dead", Album.new("Ruiner", [Song.new("In Vino Veritas II")])) }
|
22
|
+
let(:target) { Song.new }
|
23
23
|
|
24
24
|
it do
|
25
25
|
representer.prepare(target).from_object(source)
|
26
26
|
|
27
|
-
target.title.must_equal "The King Is Dead"
|
28
|
-
target.album.name.must_equal "RUINER"
|
29
|
-
target.album.songs[0].title.must_equal "IN VINO VERITAS II"
|
27
|
+
_(target.title).must_equal "The King Is Dead"
|
28
|
+
_(target.album.name).must_equal "RUINER"
|
29
|
+
_(target.album.songs[0].title).must_equal "IN VINO VERITAS II"
|
30
30
|
end
|
31
31
|
|
32
32
|
# ignore nested object when nil
|
33
33
|
it do
|
34
34
|
representer.prepare(Song.new("The King Is Dead")).from_object(Song.new)
|
35
35
|
|
36
|
-
target.title.
|
37
|
-
target.album.
|
36
|
+
_(target.title).must_be_nil # scalar property gets overridden when nil.
|
37
|
+
_(target.album).must_be_nil # nested property stays nil.
|
38
38
|
end
|
39
39
|
|
40
40
|
# to_object
|
@@ -53,8 +53,8 @@ class ObjectTest < MiniTest::Spec
|
|
53
53
|
|
54
54
|
it do
|
55
55
|
representer.prepare(source).to_object
|
56
|
-
source.album.name.must_equal "Live"
|
57
|
-
source.album.songs[0].title.must_equal 1
|
56
|
+
_(source.album.name).must_equal "Live"
|
57
|
+
_(source.album.songs[0].title).must_equal 1
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
data/test/option_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class OptionTest < MiniTest::Spec
|
6
|
+
class Callable
|
7
|
+
include Uber::Callable
|
8
|
+
def call(*); "callable" end
|
9
|
+
end
|
10
|
+
|
11
|
+
class MyRepresenter < Representable::Decorator
|
12
|
+
include Representable::JSON
|
13
|
+
|
14
|
+
property :static, getter: "static"
|
15
|
+
property :symbol, getter: :symbol
|
16
|
+
property :proc, getter: ->(*) { "proc" }
|
17
|
+
property :callable, getter: Callable.new
|
18
|
+
end
|
19
|
+
|
20
|
+
Album = Struct.new(:static) do
|
21
|
+
def symbol(*); "symbol" end
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:album_representer) { MyRepresenter.new(Album.new) }
|
25
|
+
|
26
|
+
describe ::Representable::Option do
|
27
|
+
it "supports all types of callables (method, proc, static etc)" do
|
28
|
+
_(album_representer.to_hash).must_equal({
|
29
|
+
"static" => "static",
|
30
|
+
"symbol" => "symbol",
|
31
|
+
"proc" => "proc",
|
32
|
+
"callable" => "callable",
|
33
|
+
})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/test/parse_pipeline_test.rb
CHANGED
@@ -22,12 +22,12 @@ class ParsePipelineTest < MiniTest::Spec
|
|
22
22
|
|
23
23
|
it do
|
24
24
|
representer.new(album = Album.new).from_hash("songs"=>nil)
|
25
|
-
album.songs.must_equal []
|
25
|
+
_(album.songs).must_equal []
|
26
26
|
end
|
27
27
|
|
28
28
|
it do
|
29
29
|
representer.new(album = Album.new).from_hash("songs"=>[{"title" => "Business Conduct"}])
|
30
|
-
album.songs.must_equal [Song.new("Business Conduct")]
|
30
|
+
_(album.songs).must_equal [Song.new("Business Conduct")]
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -45,7 +45,6 @@ class ParsePipelineTest < MiniTest::Spec
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def instance!(*options)
|
48
|
-
puts "@@@@@ #{options.inspect}"
|
49
48
|
Song.new
|
50
49
|
end
|
51
50
|
|
@@ -58,7 +57,6 @@ class ParsePipelineTest < MiniTest::Spec
|
|
58
57
|
skip "TODO: implement :parse_pipeline and :render_pipeline, and before/after/replace semantics"
|
59
58
|
album = Album.new
|
60
59
|
Representer.new(album).from_hash({"artist"=>{"email"=>"yo"}, "songs"=>[{"title"=>"Affliction"}, {"title"=>"Dream Beater"}]})
|
61
|
-
album.songs.must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
|
62
|
-
puts album.inspect
|
60
|
+
_(album.songs).must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
|
63
61
|
end
|
64
62
|
end
|
data/test/pipeline_test.rb
CHANGED
@@ -24,15 +24,15 @@ class PipelineTest < MiniTest::Spec
|
|
24
24
|
AssignFragment = ->(input, options) { options[:fragment] = input }
|
25
25
|
|
26
26
|
it "linear" do
|
27
|
-
P[SkipParse, Setter].("doc", {fragment: 1}).must_equal "Setter(doc)"
|
27
|
+
_(P[SkipParse, Setter].("doc", {fragment: 1})).must_equal "Setter(doc)"
|
28
28
|
|
29
29
|
|
30
30
|
# parse style.
|
31
|
-
P[AssignFragment, SkipParse, CreateObject, Prepare].("Bla", {}).must_equal "Prepare(#<OpenStruct>)"
|
31
|
+
_(P[AssignFragment, SkipParse, CreateObject, Prepare].("Bla", {})).must_equal "Prepare(#<OpenStruct>)"
|
32
32
|
|
33
33
|
|
34
34
|
# render style.
|
35
|
-
P[Getter, StopOnNil, SkipRender, Prepare, Setter].(nil, {}).
|
35
|
+
_(P[Getter, StopOnNil, SkipRender, Prepare, Setter].(nil, {})).
|
36
36
|
must_equal "Setter(Prepare(Yo))"
|
37
37
|
|
38
38
|
# pipeline = Representable::Pipeline[SkipParse , SetResult, ModifyResult]
|
@@ -46,8 +46,8 @@ class PipelineTest < MiniTest::Spec
|
|
46
46
|
|
47
47
|
|
48
48
|
pipeline = Representable::Pipeline[SkipParse, Stopping, Prepare]
|
49
|
-
pipeline.(nil, fragment: "oy!").must_equal "Prepare()"
|
50
|
-
pipeline.(nil, fragment: "stop!").must_equal Representable::Pipeline::Stop
|
49
|
+
_(pipeline.(nil, fragment: "oy!")).must_equal "Prepare()"
|
50
|
+
_(pipeline.(nil, fragment: "stop!")).must_equal Representable::Pipeline::Stop
|
51
51
|
end
|
52
52
|
|
53
53
|
describe "Collect" do
|
@@ -55,11 +55,11 @@ class PipelineTest < MiniTest::Spec
|
|
55
55
|
Add = ->(input, options) { "#{input}+" }
|
56
56
|
let(:pipeline) { R::Collect[Reverse, Add] }
|
57
57
|
|
58
|
-
it { pipeline.(["yo!", "oy!"], {}).must_equal ["!oy+", "!yo+"] }
|
58
|
+
it { _(pipeline.(["yo!", "oy!"], {})).must_equal ["!oy+", "!yo+"] }
|
59
59
|
|
60
60
|
describe "Pipeline with Collect" do
|
61
61
|
let(:pipeline) { P[Reverse, R::Collect[Reverse, Add]] }
|
62
|
-
it { pipeline.(["yo!", "oy!"], {}).must_equal ["!yo+", "!oy+"] }
|
62
|
+
it { _(pipeline.(["yo!", "oy!"], {})).must_equal ["!yo+", "!oy+"] }
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -68,7 +68,7 @@ class PipelineTest < MiniTest::Spec
|
|
68
68
|
|
69
69
|
######### scalar property
|
70
70
|
|
71
|
-
let
|
71
|
+
let(:title) {
|
72
72
|
dfn = R::Definition.new(:title)
|
73
73
|
|
74
74
|
R::Hash::Binding.new(dfn)
|
@@ -76,26 +76,26 @@ class PipelineTest < MiniTest::Spec
|
|
76
76
|
|
77
77
|
it "rendering scalar property" do
|
78
78
|
doc = {}
|
79
|
-
P[
|
79
|
+
_(P[
|
80
80
|
R::GetValue,
|
81
81
|
R::StopOnSkipable,
|
82
82
|
R::AssignName,
|
83
83
|
R::WriteFragment
|
84
|
-
].(nil, {represented: Song.new("Lime Green"), binding: title, doc: doc}).must_equal "Lime Green"
|
84
|
+
].(nil, {represented: Song.new("Lime Green"), binding: title, doc: doc})).must_equal "Lime Green"
|
85
85
|
|
86
|
-
doc.must_equal({"title"=>"Lime Green"})
|
86
|
+
_(doc).must_equal({"title"=>"Lime Green"})
|
87
87
|
end
|
88
88
|
|
89
89
|
it "parsing scalar property" do
|
90
|
-
P[
|
90
|
+
_(P[
|
91
91
|
R::AssignName,
|
92
92
|
R::ReadFragment,
|
93
93
|
R::StopOnNotFound,
|
94
94
|
R::OverwriteOnNil,
|
95
95
|
# R::SkipParse,
|
96
96
|
R::SetValue,
|
97
|
-
].extend(P::Debug).(doc={"title"=>"Eruption"}, {represented: song=Song.new("Lime Green"), binding: title, doc: doc}).must_equal "Eruption"
|
98
|
-
song.title.must_equal "Eruption"
|
97
|
+
].extend(P::Debug).(doc={"title"=>"Eruption"}, {represented: song=Song.new("Lime Green"), binding: title, doc: doc})).must_equal "Eruption"
|
98
|
+
_(song.title).must_equal "Eruption"
|
99
99
|
end
|
100
100
|
|
101
101
|
|
@@ -105,17 +105,17 @@ class PipelineTest < MiniTest::Spec
|
|
105
105
|
property :name
|
106
106
|
end
|
107
107
|
|
108
|
-
let
|
108
|
+
let(:artist) {
|
109
109
|
dfn = R::Definition.new(:artist, extend: ArtistRepresenter, class: Artist)
|
110
110
|
|
111
111
|
R::Hash::Binding.new(dfn)
|
112
112
|
}
|
113
113
|
|
114
|
-
let
|
114
|
+
let(:song_model) { Song.new("Lime Green", Artist.new("Diesel Boy")) }
|
115
115
|
|
116
116
|
it "rendering typed property" do
|
117
117
|
doc = {}
|
118
|
-
P[
|
118
|
+
_(P[
|
119
119
|
R::GetValue,
|
120
120
|
R::StopOnSkipable,
|
121
121
|
R::StopOnNil,
|
@@ -123,13 +123,13 @@ class PipelineTest < MiniTest::Spec
|
|
123
123
|
R::Serialize,
|
124
124
|
R::AssignName,
|
125
125
|
R::WriteFragment
|
126
|
-
].extend(P::Debug).(nil, {represented: song_model, binding: artist, doc: doc, options: {}}).must_equal({"name" => "Diesel Boy"})
|
126
|
+
].extend(P::Debug).(nil, {represented: song_model, binding: artist, doc: doc, options: {}})).must_equal({"name" => "Diesel Boy"})
|
127
127
|
|
128
|
-
doc.must_equal({"artist"=>{"name"=>"Diesel Boy"}})
|
128
|
+
_(doc).must_equal({"artist"=>{"name"=>"Diesel Boy"}})
|
129
129
|
end
|
130
130
|
|
131
131
|
it "parsing typed property" do
|
132
|
-
P[
|
132
|
+
_(P[
|
133
133
|
R::AssignName,
|
134
134
|
R::ReadFragment,
|
135
135
|
R::StopOnNotFound,
|
@@ -139,21 +139,21 @@ class PipelineTest < MiniTest::Spec
|
|
139
139
|
R::Decorate,
|
140
140
|
R::Deserialize,
|
141
141
|
R::SetValue,
|
142
|
-
].extend(P::Debug).(doc={"artist"=>{"name"=>"Doobie Brothers"}}, {represented: song_model, binding: artist, doc: doc, options: {}}).must_equal model=Artist.new("Doobie Brothers")
|
143
|
-
song_model.artist.must_equal model
|
142
|
+
].extend(P::Debug).(doc={"artist"=>{"name"=>"Doobie Brothers"}}, {represented: song_model, binding: artist, doc: doc, options: {}})).must_equal model=Artist.new("Doobie Brothers")
|
143
|
+
_(song_model.artist).must_equal model
|
144
144
|
end
|
145
145
|
|
146
146
|
|
147
147
|
######### collection :ratings
|
148
148
|
|
149
|
-
let
|
149
|
+
let(:ratings) {
|
150
150
|
dfn = R::Definition.new(:ratings, collection: true, skip_render: ->(*) { false })
|
151
151
|
|
152
152
|
R::Hash::Binding::Collection.new(dfn)
|
153
153
|
}
|
154
154
|
it "render scalar collection" do
|
155
155
|
doc = {}
|
156
|
-
P[
|
156
|
+
_(P[
|
157
157
|
R::GetValue,
|
158
158
|
R::StopOnSkipable,
|
159
159
|
R::Collect[
|
@@ -161,20 +161,20 @@ class PipelineTest < MiniTest::Spec
|
|
161
161
|
],
|
162
162
|
R::AssignName,
|
163
163
|
R::WriteFragment
|
164
|
-
].extend(P::Debug).(nil, {represented: Album.new([1,2,3]), binding: ratings, doc: doc, options: {}}).must_equal([1,2,3])
|
164
|
+
].extend(P::Debug).(nil, {represented: Album.new([1,2,3]), binding: ratings, doc: doc, options: {}})).must_equal([1,2,3])
|
165
165
|
|
166
|
-
doc.must_equal({"ratings"=>[1,2,3]})
|
166
|
+
_(doc).must_equal({"ratings"=>[1,2,3]})
|
167
167
|
end
|
168
168
|
|
169
169
|
######### collection :songs, extend: SongRepresenter
|
170
|
-
let
|
170
|
+
let(:artists) {
|
171
171
|
dfn = R::Definition.new(:artists, collection: true, extend: ArtistRepresenter, class: Artist)
|
172
172
|
|
173
173
|
R::Hash::Binding::Collection.new(dfn)
|
174
174
|
}
|
175
175
|
it "render typed collection" do
|
176
176
|
doc = {}
|
177
|
-
P[
|
177
|
+
_(P[
|
178
178
|
R::GetValue,
|
179
179
|
R::StopOnSkipable,
|
180
180
|
R::Collect[
|
@@ -183,16 +183,16 @@ class PipelineTest < MiniTest::Spec
|
|
183
183
|
],
|
184
184
|
R::AssignName,
|
185
185
|
R::WriteFragment
|
186
|
-
].extend(P::Debug).(nil, {represented: Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]), binding: artists, doc: doc, options: {}}).must_equal([{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}])
|
186
|
+
].extend(P::Debug).(nil, {represented: Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]), binding: artists, doc: doc, options: {}})).must_equal([{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}])
|
187
187
|
|
188
|
-
doc.must_equal({"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]})
|
188
|
+
_(doc).must_equal({"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]})
|
189
189
|
end
|
190
190
|
|
191
|
-
let
|
191
|
+
let(:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]) }
|
192
192
|
|
193
193
|
it "parse typed collection" do
|
194
194
|
doc = {"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]}
|
195
|
-
P[
|
195
|
+
_(P[
|
196
196
|
R::AssignName,
|
197
197
|
R::ReadFragment,
|
198
198
|
R::StopOnNotFound,
|
@@ -205,51 +205,51 @@ let (:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van H
|
|
205
205
|
R::Deserialize,
|
206
206
|
],
|
207
207
|
R::SetValue,
|
208
|
-
].extend(P::Debug).(doc, {represented: album_model, binding: artists, doc: doc, options: {}}).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
208
|
+
].extend(P::Debug).(doc, {represented: album_model, binding: artists, doc: doc, options: {}})).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
209
209
|
|
210
|
-
album_model.artists.must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
210
|
+
_(album_model.artists).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
211
211
|
end
|
212
212
|
|
213
213
|
# TODO: test with arrays, too, not "only" Pipeline instances.
|
214
214
|
describe "#Insert Pipeline[], Function, replace: OldFunction" do
|
215
|
-
let
|
215
|
+
let(:pipeline) { P[R::GetValue, R::StopOnSkipable, R::StopOnNil] }
|
216
216
|
|
217
217
|
it "returns Pipeline instance when passing in Pipeline instance" do
|
218
|
-
P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable).must_be_instance_of(R::Pipeline)
|
218
|
+
_(P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable)).must_be_instance_of(R::Pipeline)
|
219
219
|
end
|
220
220
|
|
221
221
|
it "replaces if exists" do
|
222
222
|
# pipeline.insert!(R::Default, replace: R::StopOnSkipable)
|
223
|
-
P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable).must_equal P[R::GetValue, R::Default, R::StopOnNil]
|
224
|
-
pipeline.must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
223
|
+
_(P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable)).must_equal P[R::GetValue, R::Default, R::StopOnNil]
|
224
|
+
_(pipeline).must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
225
225
|
end
|
226
226
|
|
227
227
|
it "replaces Function instance" do
|
228
228
|
pipeline = P[R::Prepare, R::StopOnSkipable, R::StopOnNil]
|
229
|
-
P::Insert.(pipeline, R::Default, replace: R::Prepare).must_equal P[R::Default, R::StopOnSkipable, R::StopOnNil]
|
230
|
-
pipeline.must_equal P[R::Prepare, R::StopOnSkipable, R::StopOnNil]
|
229
|
+
_(P::Insert.(pipeline, R::Default, replace: R::Prepare)).must_equal P[R::Default, R::StopOnSkipable, R::StopOnNil]
|
230
|
+
_(pipeline).must_equal P[R::Prepare, R::StopOnSkipable, R::StopOnNil]
|
231
231
|
end
|
232
232
|
|
233
233
|
it "does not replace when not existing" do
|
234
234
|
P::Insert.(pipeline, R::Default, replace: R::Prepare)
|
235
|
-
pipeline.must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
235
|
+
_(pipeline).must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
236
236
|
end
|
237
237
|
|
238
238
|
it "applies on nested Collect" do
|
239
239
|
pipeline = P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil]
|
240
240
|
|
241
|
-
P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable).extend(P::Debug).inspect.must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
242
|
-
pipeline.must_equal P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil]
|
241
|
+
_(P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable).extend(P::Debug).inspect).must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
242
|
+
_(pipeline).must_equal P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil]
|
243
243
|
|
244
244
|
|
245
|
-
P::Insert.(pipeline, R::Default, replace: R::StopOnNil).extend(P::Debug).inspect.must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], Default]"
|
245
|
+
_(P::Insert.(pipeline, R::Default, replace: R::StopOnNil).extend(P::Debug).inspect).must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], Default]"
|
246
246
|
end
|
247
247
|
|
248
248
|
it "applies on nested Collect with Function::CreateObject" do
|
249
249
|
pipeline = P[R::GetValue, R::Collect[R::GetValue, R::CreateObject], R::StopOnNil]
|
250
250
|
|
251
|
-
P::Insert.(pipeline, R::Default, replace: R::CreateObject).extend(P::Debug).inspect.must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
252
|
-
pipeline.must_equal P[R::GetValue, R::Collect[R::GetValue, R::CreateObject], R::StopOnNil]
|
251
|
+
_(P::Insert.(pipeline, R::Default, replace: R::CreateObject).extend(P::Debug).inspect).must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
252
|
+
_(pipeline).must_equal P[R::GetValue, R::Collect[R::GetValue, R::CreateObject], R::StopOnNil]
|
253
253
|
end
|
254
254
|
end
|
255
255
|
|
@@ -257,8 +257,8 @@ let (:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van H
|
|
257
257
|
let(:pipeline) { P[R::GetValue, R::StopOnNil] }
|
258
258
|
|
259
259
|
it do
|
260
|
-
P::Insert.(pipeline, R::GetValue, delete: true).extend(P::Debug).inspect.must_equal "Pipeline[StopOnNil]"
|
261
|
-
pipeline.extend(P::Debug).inspect.must_equal "Pipeline[GetValue, StopOnNil]"
|
260
|
+
_(P::Insert.(pipeline, R::GetValue, delete: true).extend(P::Debug).inspect).must_equal "Pipeline[StopOnNil]"
|
261
|
+
_(pipeline.extend(P::Debug).inspect).must_equal "Pipeline[GetValue, StopOnNil]"
|
262
262
|
end
|
263
263
|
end
|
264
264
|
|
@@ -266,8 +266,8 @@ let (:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van H
|
|
266
266
|
let(:pipeline) { P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil] }
|
267
267
|
|
268
268
|
it do
|
269
|
-
P::Insert.(pipeline, R::GetValue, delete: true).extend(P::Debug).inspect.must_equal "Pipeline[Collect[StopOnSkipable], StopOnNil]"
|
270
|
-
pipeline.extend(P::Debug).inspect.must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], StopOnNil]"
|
269
|
+
_(P::Insert.(pipeline, R::GetValue, delete: true).extend(P::Debug).inspect).must_equal "Pipeline[Collect[StopOnSkipable], StopOnNil]"
|
270
|
+
_(pipeline.extend(P::Debug).inspect).must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], StopOnNil]"
|
271
271
|
end
|
272
272
|
end
|
273
273
|
end
|