representable 3.3.1 → 3.4.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 +4 -4
- data/CHANGES.md +6 -0
- data/Gemfile +0 -1
- data/README.md +1 -7
- data/lib/representable/json.rb +11 -14
- data/lib/representable/version.rb +1 -1
- data/representable.gemspec +1 -2
- metadata +1 -60
- data/test/as_test.rb +0 -60
- data/test/benchmarking.rb +0 -78
- data/test/binding_test.rb +0 -54
- data/test/cached_test.rb +0 -177
- data/test/class_test.rb +0 -132
- data/test/coercion_test.rb +0 -73
- data/test/config/inherit_test.rb +0 -130
- data/test/config_test.rb +0 -120
- data/test/decorator_scope_test.rb +0 -30
- data/test/decorator_test.rb +0 -109
- data/test/default_test.rb +0 -34
- data/test/defaults_options_test.rb +0 -121
- data/test/definition_test.rb +0 -253
- data/test/examples/example.rb +0 -314
- data/test/examples/object.rb +0 -28
- data/test/exec_context_test.rb +0 -88
- data/test/features_test.rb +0 -89
- data/test/filter_test.rb +0 -66
- data/test/for_collection_test.rb +0 -71
- data/test/generic_test.rb +0 -109
- data/test/getter_setter_test.rb +0 -26
- data/test/hash_bindings_test.rb +0 -96
- data/test/hash_test.rb +0 -197
- data/test/heritage_test.rb +0 -64
- data/test/if_test.rb +0 -83
- data/test/include_exclude_test.rb +0 -96
- data/test/inherit_test.rb +0 -246
- data/test/inline_test.rb +0 -281
- data/test/instance_test.rb +0 -289
- data/test/is_representable_test.rb +0 -79
- data/test/json_test.rb +0 -379
- data/test/lonely_test.rb +0 -241
- data/test/nested_test.rb +0 -112
- data/test/object_test.rb +0 -60
- data/test/option_test.rb +0 -38
- data/test/parse_pipeline_test.rb +0 -68
- data/test/pipeline_test.rb +0 -309
- data/test/populator_test.rb +0 -108
- data/test/prepare_test.rb +0 -64
- data/test/private_options_test.rb +0 -19
- data/test/reader_writer_test.rb +0 -19
- data/test/realistic_benchmark.rb +0 -96
- data/test/render_nil_test.rb +0 -21
- data/test/represent_test.rb +0 -84
- data/test/representable_test.rb +0 -526
- data/test/schema_test.rb +0 -175
- data/test/serialize_deserialize_test.rb +0 -33
- data/test/skip_test.rb +0 -92
- data/test/stringify_hash_test.rb +0 -39
- data/test/test_helper.rb +0 -153
- data/test/test_helper_test.rb +0 -25
- data/test/uncategorized_test.rb +0 -72
- data/test/user_options_test.rb +0 -24
- data/test/wrap_test.rb +0 -162
- data/test/xml_assertions.rb +0 -29
- data/test/xml_bindings_test.rb +0 -55
- data/test/xml_namespace_test.rb +0 -193
- data/test/xml_test.rb +0 -584
- data/test/yaml_test.rb +0 -209
data/test/nested_test.rb
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class NestedTest < Minitest::Spec
|
|
4
|
-
Album = Struct.new(:label, :owner, :amount)
|
|
5
|
-
|
|
6
|
-
for_formats(
|
|
7
|
-
:hash => [
|
|
8
|
-
Representable::Hash,
|
|
9
|
-
{"label" => {"label" => "Epitaph", "owner" => "Brett Gurewitz", "releases" => {"amount"=>19}}}
|
|
10
|
-
],
|
|
11
|
-
# :xml => [Representable::XML, "<open_struct></open_struct>"],
|
|
12
|
-
:yaml => [Representable::YAML, "---\nlabel:\n label: Epitaph\n owner: Brett Gurewitz\n releases:\n amount: 19\n"]
|
|
13
|
-
) do |format, mod, output, _input|
|
|
14
|
-
[false, true].each do |is_decorator|
|
|
15
|
-
describe "::nested with (inline representer|decorator): #{is_decorator}" do
|
|
16
|
-
let(:format) { format }
|
|
17
|
-
|
|
18
|
-
representer!(:module => mod, :decorator => is_decorator) do
|
|
19
|
-
nested :label do
|
|
20
|
-
property :label
|
|
21
|
-
property :owner
|
|
22
|
-
|
|
23
|
-
# self.representation_wrap = nil if format == :xml
|
|
24
|
-
nested :releases do
|
|
25
|
-
property :amount
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# self.representation_wrap = :album if format == :xml
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
let(:album) { Album.new("Epitaph", "Brett Gurewitz", 19) }
|
|
33
|
-
let(:decorator) { representer.prepare(album) }
|
|
34
|
-
|
|
35
|
-
it "renders nested Album-properties in separate section" do
|
|
36
|
-
render(decorator).must_equal_document output
|
|
37
|
-
|
|
38
|
-
# do not use extend on the nested object. # FIXME: make this a proper test with two describes instead of this pseudo-meta stuff.
|
|
39
|
-
_(album).wont_be_kind_of(Representable::Hash) if is_decorator == true
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it "parses nested properties to Album instance" do
|
|
43
|
-
album = parse(representer.prepare(Album.new), output)
|
|
44
|
-
_(album.label).must_equal "Epitaph"
|
|
45
|
-
_(album.owner).must_equal "Brett Gurewitz"
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
describe "Decorator ::nested with extend:" do
|
|
51
|
-
let(:format) { format }
|
|
52
|
-
|
|
53
|
-
representer!(:name => :label_rpr) do
|
|
54
|
-
include mod
|
|
55
|
-
property :label
|
|
56
|
-
property :owner
|
|
57
|
-
|
|
58
|
-
nested :releases do # DISCUSS: do we need to test this?
|
|
59
|
-
property :amount
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
representer!(:module => mod, :decorator => true, :inject => :label_rpr) do
|
|
64
|
-
nested :label, :extend => label_rpr
|
|
65
|
-
|
|
66
|
-
self.representation_wrap = :album if format == :xml
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
let(:album) { representer.prepare(Album.new("Epitaph", "Brett Gurewitz", 19)) }
|
|
70
|
-
|
|
71
|
-
# TODO: shared example with above.
|
|
72
|
-
it "renders nested Album-properties in separate section" do
|
|
73
|
-
render(album).must_equal_document output
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it "parses nested properties to Album instance" do
|
|
77
|
-
album = parse(representer.prepare(Album.new), output)
|
|
78
|
-
_(album.label).must_equal "Epitaph"
|
|
79
|
-
_(album.owner).must_equal "Brett Gurewitz"
|
|
80
|
-
_(album.amount).must_equal 19
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
describe "::nested without block but with inherit:" do
|
|
86
|
-
representer!(:name => :parent) do
|
|
87
|
-
include Representable::Hash
|
|
88
|
-
|
|
89
|
-
nested :label do
|
|
90
|
-
property :owner
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
representer!(:module => Representable::Hash, :inject => :parent) do
|
|
95
|
-
include parent
|
|
96
|
-
nested :label, :inherit => true, :as => "Label"
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
let(:album) { representer.prepare(Album.new("Epitaph", "Brett Gurewitz", 19)) }
|
|
100
|
-
|
|
101
|
-
it "renders nested Album-properties in separate section" do
|
|
102
|
-
_(representer.prepare(album).to_hash).must_equal({"Label"=>{"owner"=>"Brett Gurewitz"}})
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# it "parses nested properties to Album instance" do
|
|
106
|
-
# album = parse(representer.prepare(Album.new), output)
|
|
107
|
-
# album.label.must_equal "Epitaph"
|
|
108
|
-
# album.owner.must_equal "Brett Gurewitz"
|
|
109
|
-
# album.amount.must_equal 19
|
|
110
|
-
# end
|
|
111
|
-
end
|
|
112
|
-
end
|
data/test/object_test.rb
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
require "representable/object"
|
|
3
|
-
|
|
4
|
-
class ObjectTest < Minitest::Spec
|
|
5
|
-
Song = Struct.new(:title, :album)
|
|
6
|
-
Album = Struct.new(:name, :songs)
|
|
7
|
-
|
|
8
|
-
representer!(module: Representable::Object) do
|
|
9
|
-
property :title
|
|
10
|
-
|
|
11
|
-
property :album, instance: ->(options) { options[:fragment].name.upcase!; options[:fragment] } do
|
|
12
|
-
property :name
|
|
13
|
-
|
|
14
|
-
collection :songs, instance: ->(options) { options[:fragment].title.upcase!; options[:fragment] } do
|
|
15
|
-
property :title
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
# TODO: collection
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
let(:source) { Song.new("The King Is Dead", Album.new("Ruiner", [Song.new("In Vino Veritas II")])) }
|
|
22
|
-
let(:target) { Song.new }
|
|
23
|
-
|
|
24
|
-
it do
|
|
25
|
-
representer.prepare(target).from_object(source)
|
|
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"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# ignore nested object when nil
|
|
33
|
-
it do
|
|
34
|
-
representer.prepare(Song.new("The King Is Dead")).from_object(Song.new)
|
|
35
|
-
|
|
36
|
-
_(target.title).must_be_nil # scalar property gets overridden when nil.
|
|
37
|
-
_(target.album).must_be_nil # nested property stays nil.
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# to_object
|
|
41
|
-
describe "#to_object" do
|
|
42
|
-
representer!(module: Representable::Object) do
|
|
43
|
-
property :title
|
|
44
|
-
|
|
45
|
-
property :album, render_filter: ->(input, _options) { input.name = "Live"; input } do
|
|
46
|
-
property :name
|
|
47
|
-
|
|
48
|
-
collection :songs, render_filter: ->(input, _options) { input[0].title = 1; input } do
|
|
49
|
-
property :title
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it do
|
|
55
|
-
representer.prepare(source).to_object
|
|
56
|
-
_(source.album.name).must_equal "Live"
|
|
57
|
-
_(source.album.songs[0].title).must_equal 1
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
end
|
data/test/option_test.rb
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
{
|
|
30
|
-
"static" => "static",
|
|
31
|
-
"symbol" => "symbol",
|
|
32
|
-
"proc" => "proc",
|
|
33
|
-
"callable" => "callable"
|
|
34
|
-
}
|
|
35
|
-
)
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
data/test/parse_pipeline_test.rb
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ParsePipelineTest < Minitest::Spec
|
|
4
|
-
Album = Struct.new(:id, :artist, :songs)
|
|
5
|
-
Artist = Struct.new(:email)
|
|
6
|
-
Song = Struct.new(:title)
|
|
7
|
-
|
|
8
|
-
describe "transforming nil to [] when parsing" do
|
|
9
|
-
representer!(decorator: true) do
|
|
10
|
-
collection :songs,
|
|
11
|
-
parse_pipeline: ->(*) {
|
|
12
|
-
Representable::Pipeline.insert(
|
|
13
|
-
parse_functions, # original function list from Binding#parse_functions.
|
|
14
|
-
->(input, _options) { input.nil? ? [] : input }, # your new function (can be any callable object)..
|
|
15
|
-
replace: Representable::OverwriteOnNil # ..that replaces the original function.
|
|
16
|
-
)
|
|
17
|
-
},
|
|
18
|
-
class: Song do
|
|
19
|
-
property :title
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it do
|
|
24
|
-
representer.new(album = Album.new).from_hash("songs"=>nil)
|
|
25
|
-
_(album.songs).must_equal []
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it do
|
|
29
|
-
representer.new(album = Album.new).from_hash("songs"=>[{"title" => "Business Conduct"}])
|
|
30
|
-
_(album.songs).must_equal [Song.new("Business Conduct")]
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# tests [Collect[Instance, Prepare, Deserialize], Setter]
|
|
35
|
-
class Representer < Representable::Decorator
|
|
36
|
-
include Representable::Hash
|
|
37
|
-
|
|
38
|
-
# property :artist, populator: Uber::Options::Value.new(ArtistPopulator.new), pass_options:true do
|
|
39
|
-
# property :email
|
|
40
|
-
# end
|
|
41
|
-
# DISCUSS: rename to populator_pipeline ?
|
|
42
|
-
collection :songs, parse_pipeline: ->(*) {
|
|
43
|
-
[Collect[Instance, Prepare, Deserialize], Setter]
|
|
44
|
-
}, instance: :instance!, exec_context: :decorator, pass_options: true do
|
|
45
|
-
property :title
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def instance!(*_options)
|
|
49
|
-
Song.new
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def songs=(array)
|
|
53
|
-
represented.songs = array
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
it do
|
|
58
|
-
skip "TODO: implement :parse_pipeline and :render_pipeline, and before/after/replace semantics"
|
|
59
|
-
album = Album.new
|
|
60
|
-
Representer.new(album).from_hash(
|
|
61
|
-
{
|
|
62
|
-
"artist" => {"email"=>"yo"},
|
|
63
|
-
"songs" => [{"title"=>"Affliction"}, {"title"=>"Dream Beater"}]
|
|
64
|
-
}
|
|
65
|
-
)
|
|
66
|
-
_(album.songs).must_equal([Song.new("Affliction"), Song.new("Dream Beater")])
|
|
67
|
-
end
|
|
68
|
-
end
|
data/test/pipeline_test.rb
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class PipelineTest < Minitest::Spec
|
|
4
|
-
Song = Struct.new(:title, :artist)
|
|
5
|
-
Artist = Struct.new(:name)
|
|
6
|
-
Album = Struct.new(:ratings, :artists)
|
|
7
|
-
|
|
8
|
-
R = Representable
|
|
9
|
-
P = R::Pipeline
|
|
10
|
-
|
|
11
|
-
Getter = ->(_input, _options) { "Yo" }
|
|
12
|
-
StopOnNil = ->(input, _options) { input }
|
|
13
|
-
SkipRender = ->(input, *) { input == "Yo" ? input : P::Stop }
|
|
14
|
-
|
|
15
|
-
Prepare = ->(input, _options) { "Prepare(#{input})" }
|
|
16
|
-
Deserialize = ->(input, options) { "Deserialize(#{input}, #{options[:fragment]})" }
|
|
17
|
-
|
|
18
|
-
SkipParse = ->(input, _options) { input }
|
|
19
|
-
CreateObject = ->(_input, _options) { OpenStruct.new }
|
|
20
|
-
|
|
21
|
-
Setter = ->(input, _options) { "Setter(#{input})" }
|
|
22
|
-
|
|
23
|
-
AssignFragment = ->(input, options) { options[:fragment] = input }
|
|
24
|
-
|
|
25
|
-
it "linear" do
|
|
26
|
-
_(P[SkipParse, Setter].("doc", {fragment: 1})).must_equal "Setter(doc)"
|
|
27
|
-
|
|
28
|
-
# parse style.
|
|
29
|
-
_(P[AssignFragment, SkipParse, CreateObject, Prepare].("Bla", {})).must_equal "Prepare(#<OpenStruct>)"
|
|
30
|
-
|
|
31
|
-
# render style.
|
|
32
|
-
_(P[Getter, StopOnNil, SkipRender, Prepare, Setter].(nil, {}))
|
|
33
|
-
.must_equal "Setter(Prepare(Yo))"
|
|
34
|
-
|
|
35
|
-
# pipeline = Representable::Pipeline[SkipParse , SetResult, ModifyResult]
|
|
36
|
-
# pipeline.(fragment: "yo!").must_equal "modified object from yo!"
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
Stopping = ->(input, options) { return P::Stop if options[:fragment] == "stop!"; input }
|
|
40
|
-
|
|
41
|
-
it "stopping" do
|
|
42
|
-
pipeline = Representable::Pipeline[SkipParse, Stopping, Prepare]
|
|
43
|
-
_(pipeline.(nil, fragment: "oy!")).must_equal "Prepare()"
|
|
44
|
-
_(pipeline.(nil, fragment: "stop!")).must_equal Representable::Pipeline::Stop
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
describe "Collect" do
|
|
48
|
-
Reverse = ->(input, _options) { input.reverse }
|
|
49
|
-
Add = ->(input, _options) { "#{input}+" }
|
|
50
|
-
let(:pipeline) { R::Collect[Reverse, Add] }
|
|
51
|
-
|
|
52
|
-
it { _(pipeline.(["yo!", "oy!"], {})).must_equal ["!oy+", "!yo+"] }
|
|
53
|
-
|
|
54
|
-
describe "Pipeline with Collect" do
|
|
55
|
-
let(:pipeline) { P[Reverse, R::Collect[Reverse, Add]] }
|
|
56
|
-
it { _(pipeline.(["yo!", "oy!"], {})).must_equal ["!yo+", "!oy+"] }
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
######### scalar property
|
|
61
|
-
|
|
62
|
-
let(:title) do
|
|
63
|
-
dfn = R::Definition.new(:title)
|
|
64
|
-
|
|
65
|
-
R::Hash::Binding.new(dfn)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it "rendering scalar property" do
|
|
69
|
-
doc = {}
|
|
70
|
-
_(
|
|
71
|
-
P[
|
|
72
|
-
R::GetValue,
|
|
73
|
-
R::StopOnSkipable,
|
|
74
|
-
R::AssignName,
|
|
75
|
-
R::WriteFragment
|
|
76
|
-
].(nil, {represented: Song.new("Lime Green"), binding: title, doc: doc})
|
|
77
|
-
).must_equal "Lime Green"
|
|
78
|
-
|
|
79
|
-
_(doc).must_equal({"title"=>"Lime Green"})
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
it "parsing scalar property" do
|
|
83
|
-
_(
|
|
84
|
-
P[
|
|
85
|
-
R::AssignName,
|
|
86
|
-
R::ReadFragment,
|
|
87
|
-
R::StopOnNotFound,
|
|
88
|
-
R::OverwriteOnNil,
|
|
89
|
-
# R::SkipParse,
|
|
90
|
-
R::SetValue,
|
|
91
|
-
].extend(P::Debug).(doc = {"title"=>"Eruption"}, {
|
|
92
|
-
represented: song = Song.new("Lime Green"), binding: title,
|
|
93
|
-
doc: doc
|
|
94
|
-
})
|
|
95
|
-
).must_equal "Eruption"
|
|
96
|
-
_(song.title).must_equal "Eruption"
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
module ArtistRepresenter
|
|
100
|
-
include Representable::Hash
|
|
101
|
-
property :name
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
let(:artist) do
|
|
105
|
-
dfn = R::Definition.new(:artist, extend: ArtistRepresenter, class: Artist)
|
|
106
|
-
|
|
107
|
-
R::Hash::Binding.new(dfn)
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
let(:song_model) { Song.new("Lime Green", Artist.new("Diesel Boy")) }
|
|
111
|
-
|
|
112
|
-
it "rendering typed property" do
|
|
113
|
-
doc = {}
|
|
114
|
-
_(
|
|
115
|
-
P[
|
|
116
|
-
R::GetValue,
|
|
117
|
-
R::StopOnSkipable,
|
|
118
|
-
R::StopOnNil,
|
|
119
|
-
R::Decorate,
|
|
120
|
-
R::Serialize,
|
|
121
|
-
R::AssignName,
|
|
122
|
-
R::WriteFragment
|
|
123
|
-
].extend(P::Debug).(nil, {
|
|
124
|
-
represented: song_model, binding: artist, doc: doc,
|
|
125
|
-
options: {}
|
|
126
|
-
})
|
|
127
|
-
).must_equal({"name" => "Diesel Boy"})
|
|
128
|
-
|
|
129
|
-
_(doc).must_equal({"artist"=>{"name"=>"Diesel Boy"}})
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
it "parsing typed property" do
|
|
133
|
-
_(
|
|
134
|
-
P[
|
|
135
|
-
R::AssignName,
|
|
136
|
-
R::ReadFragment,
|
|
137
|
-
R::StopOnNotFound,
|
|
138
|
-
R::OverwriteOnNil,
|
|
139
|
-
R::AssignFragment,
|
|
140
|
-
R::CreateObject::Class,
|
|
141
|
-
R::Decorate,
|
|
142
|
-
R::Deserialize,
|
|
143
|
-
R::SetValue,
|
|
144
|
-
].extend(P::Debug).(doc = {"artist"=>{"name"=>"Doobie Brothers"}}, {
|
|
145
|
-
represented: song_model, binding: artist, doc: doc,
|
|
146
|
-
options: {}
|
|
147
|
-
})
|
|
148
|
-
).must_equal model = Artist.new("Doobie Brothers")
|
|
149
|
-
_(song_model.artist).must_equal model
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
######### collection :ratings
|
|
153
|
-
|
|
154
|
-
let(:ratings) do
|
|
155
|
-
dfn = R::Definition.new(:ratings, collection: true, skip_render: ->(*) { false })
|
|
156
|
-
|
|
157
|
-
R::Hash::Binding::Collection.new(dfn)
|
|
158
|
-
end
|
|
159
|
-
it "render scalar collection" do
|
|
160
|
-
doc = {}
|
|
161
|
-
_(
|
|
162
|
-
P[
|
|
163
|
-
R::GetValue,
|
|
164
|
-
R::StopOnSkipable,
|
|
165
|
-
R::Collect[
|
|
166
|
-
R::SkipRender,
|
|
167
|
-
],
|
|
168
|
-
R::AssignName,
|
|
169
|
-
R::WriteFragment
|
|
170
|
-
].extend(P::Debug).(nil, {represented: Album.new([1, 2, 3]), binding: ratings, doc: doc, options: {}})
|
|
171
|
-
).must_equal([1, 2, 3])
|
|
172
|
-
|
|
173
|
-
_(doc).must_equal({"ratings"=>[1, 2, 3]})
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
######### collection :songs, extend: SongRepresenter
|
|
177
|
-
let(:artists) do
|
|
178
|
-
dfn = R::Definition.new(:artists, collection: true, extend: ArtistRepresenter, class: Artist)
|
|
179
|
-
|
|
180
|
-
R::Hash::Binding::Collection.new(dfn)
|
|
181
|
-
end
|
|
182
|
-
it "render typed collection" do
|
|
183
|
-
doc = {}
|
|
184
|
-
_(
|
|
185
|
-
P[
|
|
186
|
-
R::GetValue,
|
|
187
|
-
R::StopOnSkipable,
|
|
188
|
-
R::Collect[
|
|
189
|
-
R::Decorate,
|
|
190
|
-
R::Serialize,
|
|
191
|
-
],
|
|
192
|
-
R::AssignName,
|
|
193
|
-
R::WriteFragment
|
|
194
|
-
].extend(P::Debug).(nil, {
|
|
195
|
-
represented: Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]), binding: artists,
|
|
196
|
-
doc: doc, options: {}
|
|
197
|
-
})
|
|
198
|
-
).must_equal([{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}])
|
|
199
|
-
|
|
200
|
-
_(doc).must_equal({"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]})
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
let(:album_model) { Album.new(nil, [Artist.new("Diesel Boy"), Artist.new("Van Halen")]) }
|
|
204
|
-
|
|
205
|
-
it "parse typed collection" do
|
|
206
|
-
doc = {"artists"=>[{"name"=>"Diesel Boy"}, {"name"=>"Van Halen"}]}
|
|
207
|
-
_(
|
|
208
|
-
P[
|
|
209
|
-
R::AssignName,
|
|
210
|
-
R::ReadFragment,
|
|
211
|
-
R::StopOnNotFound,
|
|
212
|
-
R::OverwriteOnNil,
|
|
213
|
-
# R::SkipParse,
|
|
214
|
-
R::Collect[
|
|
215
|
-
R::AssignFragment,
|
|
216
|
-
R::CreateObject::Class,
|
|
217
|
-
R::Decorate,
|
|
218
|
-
R::Deserialize,
|
|
219
|
-
],
|
|
220
|
-
R::SetValue,
|
|
221
|
-
].extend(P::Debug).(doc, {
|
|
222
|
-
represented: album_model, binding: artists, doc: doc,
|
|
223
|
-
options: {}
|
|
224
|
-
})
|
|
225
|
-
).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
|
226
|
-
|
|
227
|
-
_(album_model.artists).must_equal([Artist.new("Diesel Boy"), Artist.new("Van Halen")])
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
# TODO: test with arrays, too, not "only" Pipeline instances.
|
|
231
|
-
describe "#Insert Pipeline[], Function, replace: OldFunction" do
|
|
232
|
-
let(:pipeline) { P[R::GetValue, R::StopOnSkipable, R::StopOnNil] }
|
|
233
|
-
|
|
234
|
-
it "returns Pipeline instance when passing in Pipeline instance" do
|
|
235
|
-
_(P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable)).must_be_instance_of(R::Pipeline)
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
it "replaces if exists" do
|
|
239
|
-
# pipeline.insert!(R::Default, replace: R::StopOnSkipable)
|
|
240
|
-
_(P::Insert.(pipeline, R::Default, replace: R::StopOnSkipable)).must_equal P[R::GetValue, R::Default, R::StopOnNil]
|
|
241
|
-
_(pipeline).must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
it "replaces Function instance" do
|
|
245
|
-
pipeline = P[R::Prepare, R::StopOnSkipable, R::StopOnNil]
|
|
246
|
-
_(P::Insert.(pipeline, R::Default, replace: R::Prepare)).must_equal P[R::Default, R::StopOnSkipable, R::StopOnNil]
|
|
247
|
-
_(pipeline).must_equal P[R::Prepare, R::StopOnSkipable, R::StopOnNil]
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
it "does not replace when not existing" do
|
|
251
|
-
P::Insert.(pipeline, R::Default, replace: R::Prepare)
|
|
252
|
-
_(pipeline).must_equal P[R::GetValue, R::StopOnSkipable, R::StopOnNil]
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
it "applies on nested Collect" do
|
|
256
|
-
pipeline = P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil]
|
|
257
|
-
|
|
258
|
-
_(
|
|
259
|
-
P::Insert.(
|
|
260
|
-
pipeline, R::Default,
|
|
261
|
-
replace: R::StopOnSkipable
|
|
262
|
-
).extend(P::Debug).inspect
|
|
263
|
-
).must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
|
264
|
-
_(pipeline).must_equal P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil]
|
|
265
|
-
|
|
266
|
-
_(
|
|
267
|
-
P::Insert.(
|
|
268
|
-
pipeline, R::Default,
|
|
269
|
-
replace: R::StopOnNil
|
|
270
|
-
).extend(P::Debug).inspect
|
|
271
|
-
).must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], Default]"
|
|
272
|
-
end
|
|
273
|
-
|
|
274
|
-
it "applies on nested Collect with Function::CreateObject" do
|
|
275
|
-
pipeline = P[R::GetValue, R::Collect[R::GetValue, R::CreateObject], R::StopOnNil]
|
|
276
|
-
|
|
277
|
-
_(
|
|
278
|
-
P::Insert.(
|
|
279
|
-
pipeline, R::Default,
|
|
280
|
-
replace: R::CreateObject
|
|
281
|
-
).extend(P::Debug).inspect
|
|
282
|
-
).must_equal "Pipeline[GetValue, Collect[GetValue, Default], StopOnNil]"
|
|
283
|
-
_(pipeline).must_equal P[R::GetValue, R::Collect[R::GetValue, R::CreateObject], R::StopOnNil]
|
|
284
|
-
end
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
describe "Insert.(delete: true)" do
|
|
288
|
-
let(:pipeline) { P[R::GetValue, R::StopOnNil] }
|
|
289
|
-
|
|
290
|
-
it do
|
|
291
|
-
_(P::Insert.(pipeline, R::GetValue, delete: true).extend(P::Debug).inspect).must_equal "Pipeline[StopOnNil]"
|
|
292
|
-
_(pipeline.extend(P::Debug).inspect).must_equal "Pipeline[GetValue, StopOnNil]"
|
|
293
|
-
end
|
|
294
|
-
end
|
|
295
|
-
|
|
296
|
-
describe "Insert.(delete: true) with Collect" do
|
|
297
|
-
let(:pipeline) { P[R::GetValue, R::Collect[R::GetValue, R::StopOnSkipable], R::StopOnNil] }
|
|
298
|
-
|
|
299
|
-
it do
|
|
300
|
-
_(
|
|
301
|
-
P::Insert.(
|
|
302
|
-
pipeline, R::GetValue,
|
|
303
|
-
delete: true
|
|
304
|
-
).extend(P::Debug).inspect
|
|
305
|
-
).must_equal "Pipeline[Collect[StopOnSkipable], StopOnNil]"
|
|
306
|
-
_(pipeline.extend(P::Debug).inspect).must_equal "Pipeline[GetValue, Collect[GetValue, StopOnSkipable], StopOnNil]"
|
|
307
|
-
end
|
|
308
|
-
end
|
|
309
|
-
end
|
data/test/populator_test.rb
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class PopulatorTest < Minitest::Spec
|
|
4
|
-
Song = Struct.new(:id)
|
|
5
|
-
Artist = Struct.new(:name)
|
|
6
|
-
Album = Struct.new(:songs, :artist)
|
|
7
|
-
|
|
8
|
-
describe "populator: ->{}" do
|
|
9
|
-
representer! do
|
|
10
|
-
collection :songs, populator: ->(_input, options) { options[:represented].songs << song = Song.new; song } do
|
|
11
|
-
property :id
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
property :artist, populator: ->(_input, options) { options[:represented].artist = Artist.new } do
|
|
15
|
-
property :name
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
let(:album) { Album.new([]) }
|
|
20
|
-
|
|
21
|
-
it do
|
|
22
|
-
album.extend(representer).from_hash("songs" => [{"id"=>1}, {"id"=>2}], "artist" => {"name"=>"Waste"})
|
|
23
|
-
_(album.inspect).must_equal "#<struct PopulatorTest::Album songs=[#<struct PopulatorTest::Song id=1>, #<struct PopulatorTest::Song id=2>], artist=#<struct PopulatorTest::Artist name=\"Waste\">>"
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
describe "populator: ->{}, " do
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
class PopulatorFindOrInstantiateTest < Minitest::Spec
|
|
32
|
-
Song = Struct.new(:id, :title, :uid) do
|
|
33
|
-
def self.find_by(attributes = {})
|
|
34
|
-
return new(1, "Resist Stan", "abcd") if attributes[:id] == 1 # we should return the same object here
|
|
35
|
-
|
|
36
|
-
new
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
Composer = Class.new
|
|
41
|
-
Composer.class_eval do
|
|
42
|
-
def song=(v)
|
|
43
|
-
@song = v
|
|
44
|
-
"Absolute nonsense" # this tests that the populator always returns the correct object.
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
attr_reader :song
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
describe "FindOrInstantiate with property" do
|
|
51
|
-
representer! do
|
|
52
|
-
property :song, populator: Representable::FindOrInstantiate, class: Song do
|
|
53
|
-
property :id
|
|
54
|
-
property :title
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
let(:album) { Composer.new.extend(representer).extend(Representable::Debug) }
|
|
59
|
-
|
|
60
|
-
it "finds by :id and creates new without :id" do
|
|
61
|
-
album.from_hash({"song"=>{"id" => 1, "title" => "Resist Stance"}})
|
|
62
|
-
|
|
63
|
-
_(album.song.title).must_equal "Resist Stance" # NOTE: how title is updated from "Resist Stan"
|
|
64
|
-
_(album.song.id).must_equal 1
|
|
65
|
-
_(album.song.uid).must_equal "abcd" # not changed via populator, indicating this is a formerly "persisted" object.
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it "creates new without :id" do
|
|
69
|
-
album.from_hash({"song"=>{"title"=>"Lower"}})
|
|
70
|
-
|
|
71
|
-
_(album.song.title).must_equal "Lower"
|
|
72
|
-
_(album.song.id).must_be_nil
|
|
73
|
-
_(album.song.uid).must_be_nil
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
describe "FindOrInstantiate with collection" do
|
|
78
|
-
representer! do
|
|
79
|
-
collection :songs, populator: Representable::FindOrInstantiate, class: Song do
|
|
80
|
-
property :id
|
|
81
|
-
property :title
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
let(:album) { Struct.new(:songs).new([]).extend(representer) }
|
|
86
|
-
|
|
87
|
-
it "finds by :id and creates new without :id" do
|
|
88
|
-
album.from_hash(
|
|
89
|
-
{
|
|
90
|
-
"songs" => [
|
|
91
|
-
{"id" => 1, "title" => "Resist Stance"},
|
|
92
|
-
{"title"=>"Suffer"}
|
|
93
|
-
]
|
|
94
|
-
}
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
_(album.songs[0].title).must_equal "Resist Stance" # NOTE: how title is updated from "Resist Stan"
|
|
98
|
-
_(album.songs[0].id).must_equal 1
|
|
99
|
-
_(album.songs[0].uid).must_equal "abcd" # not changed via populator, indicating this is a formerly "persisted" object.
|
|
100
|
-
|
|
101
|
-
_(album.songs[1].title).must_equal "Suffer"
|
|
102
|
-
_(album.songs[1].id).must_be_nil
|
|
103
|
-
_(album.songs[1].uid).must_be_nil
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# TODO: test with existing collection
|
|
107
|
-
end
|
|
108
|
-
end
|