representable 3.3.0 → 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 +10 -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 +3 -65
- 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/exec_context_test.rb
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ExecContextTest < Minitest::Spec
|
|
4
|
-
for_formats(
|
|
5
|
-
:hash => [Representable::Hash, {Song => "Rebel Fate"}, {Song=>"Timing"}]
|
|
6
|
-
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
|
|
7
|
-
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
|
8
|
-
) do |format, mod, input, output|
|
|
9
|
-
let(:song) { representer.prepare(Song.new("Timing")) }
|
|
10
|
-
let(:format) { format }
|
|
11
|
-
|
|
12
|
-
describe "exec_context: nil" do
|
|
13
|
-
representer!(:module => mod) do
|
|
14
|
-
property :name, :as => ->(*) { self.class }
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it { render(song).must_equal_document output }
|
|
18
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "exec_context: :decorator" do
|
|
22
|
-
representer!(:module => mod) do
|
|
23
|
-
property :name, :as => ->(*) { self.class }, :exec_context => :decorator
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it { render(song).must_equal_document output }
|
|
27
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe "exec_context: :binding" do
|
|
31
|
-
representer!(:module => mod) do
|
|
32
|
-
property :name,
|
|
33
|
-
:as => ->(*) { self.class }, # to actually test
|
|
34
|
-
:exec_context => :binding,
|
|
35
|
-
:setter => ->(options) {
|
|
36
|
-
options[:represented].name = options[:fragment] # to make parsing work.
|
|
37
|
-
}
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
|
41
|
-
it { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe "Decorator" do
|
|
45
|
-
# DISCUSS: do we need this test?
|
|
46
|
-
describe "exec_context: nil" do
|
|
47
|
-
representer!(:module => mod, :decorator => true) do
|
|
48
|
-
property :name, :as => ->(*) { self.class }
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it { render(song).must_equal_document output }
|
|
52
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
describe "exec_context: :decorator" do # this tests if lambdas are run in the right context, if methods are called in the right context and if we can access the represented object.
|
|
56
|
-
representer!(:module => mod, :decorator => true) do
|
|
57
|
-
property :name, :as => ->(*) { self.class.superclass }, :exec_context => :decorator
|
|
58
|
-
|
|
59
|
-
define_method :name do # def in Decorator class.
|
|
60
|
-
"Timebomb"
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
define_method :"name=" do |v| # def in Decorator class.
|
|
64
|
-
represented.name = v
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it { render(song).must_equal_document({Representable::Decorator=>"Timebomb"}) }
|
|
69
|
-
it { _(parse(song, {Representable::Decorator=>"Listless"}).name).must_equal "Listless" }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# DISCUSS: do we need this test?
|
|
73
|
-
describe "exec_context: :binding" do
|
|
74
|
-
representer!(:module => mod, :decorator => true) do
|
|
75
|
-
property :name,
|
|
76
|
-
:as => ->(*) { self.class }, # to actually test
|
|
77
|
-
:exec_context => :binding,
|
|
78
|
-
:setter => ->(options) {
|
|
79
|
-
options[:represented].name = options[:fragment] # to make parsing work.
|
|
80
|
-
}
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
|
84
|
-
it("xxx") { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|
data/test/features_test.rb
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class FeaturesTest < Minitest::Spec
|
|
4
|
-
module Title
|
|
5
|
-
def title; "Is It A Lie"; end
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module Length
|
|
9
|
-
def length; "2:31"; end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
definition = -> {
|
|
13
|
-
feature Title
|
|
14
|
-
feature Length
|
|
15
|
-
|
|
16
|
-
# exec_context: :decorator, so the readers are called on the Decorator instance (which gets readers from features!).
|
|
17
|
-
property :title, exec_context: :decorator
|
|
18
|
-
property :length, exec_context: :decorator
|
|
19
|
-
property :details do
|
|
20
|
-
property :title, exec_context: :decorator
|
|
21
|
-
end
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
let(:song) { OpenStruct.new(:details => Object.new) }
|
|
25
|
-
|
|
26
|
-
describe "Module" do
|
|
27
|
-
representer! do
|
|
28
|
-
instance_exec(&definition)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it {
|
|
32
|
-
_(song.extend(representer).to_hash).must_equal(
|
|
33
|
-
{
|
|
34
|
-
"title" => "Is It A Lie", "length" => "2:31",
|
|
35
|
-
"details" => {"title"=>"Is It A Lie"}
|
|
36
|
-
}
|
|
37
|
-
)
|
|
38
|
-
}
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
describe "Decorator" do
|
|
42
|
-
representer!(:decorator => true) do
|
|
43
|
-
instance_exec(&definition)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it {
|
|
47
|
-
_(representer.new(song).to_hash).must_equal(
|
|
48
|
-
{
|
|
49
|
-
"title" => "Is It A Lie", "length" => "2:31",
|
|
50
|
-
"details" => {"title"=>"Is It A Lie"}
|
|
51
|
-
}
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
class FeatureInclusionOrderTest < Minitest::Spec
|
|
58
|
-
module Title
|
|
59
|
-
def title
|
|
60
|
-
"I was first!"
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
module OverridingTitle
|
|
65
|
-
def title
|
|
66
|
-
"I am number two, #{super}"
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
representer!(decorator: true) do
|
|
71
|
-
feature Title
|
|
72
|
-
feature OverridingTitle
|
|
73
|
-
|
|
74
|
-
property :title, exec_context: :decorator
|
|
75
|
-
|
|
76
|
-
property :song do
|
|
77
|
-
property :title, exec_context: :decorator
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it do
|
|
82
|
-
_(representer.new(OpenStruct.new(song: Object)).to_hash).must_equal(
|
|
83
|
-
{
|
|
84
|
-
"title" => "I am number two, I was first!",
|
|
85
|
-
"song" => {"title"=>"I am number two, I was first!"}
|
|
86
|
-
}
|
|
87
|
-
)
|
|
88
|
-
end
|
|
89
|
-
end
|
data/test/filter_test.rb
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class FilterPipelineTest < Minitest::Spec
|
|
4
|
-
let(:block1) { ->(input, _options) { "1: #{input}" } }
|
|
5
|
-
let(:block2) { ->(input, _options) { "2: #{input}" } }
|
|
6
|
-
|
|
7
|
-
subject { Representable::Pipeline[block1, block2] }
|
|
8
|
-
|
|
9
|
-
it { _(subject.call("Horowitz", {})).must_equal "2: 1: Horowitz" }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
class FilterTest < Minitest::Spec
|
|
13
|
-
representer! do
|
|
14
|
-
property :title
|
|
15
|
-
|
|
16
|
-
property :track,
|
|
17
|
-
:parse_filter => ->(input, options) { "#{input.downcase},#{options[:doc]}" },
|
|
18
|
-
:render_filter => ->(val, options) { "#{val.upcase},#{options[:doc]},#{options[:options][:user_options]}" }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# gets doc and options.
|
|
22
|
-
it {
|
|
23
|
-
song = OpenStruct.new.extend(representer).from_hash("title" => "VULCAN EARS", "track" => "Nine")
|
|
24
|
-
_(song.title).must_equal "VULCAN EARS"
|
|
25
|
-
_(song.track).must_equal "nine,#{{"title" => "VULCAN EARS", "track" => "Nine"}.inspect}"
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
it {
|
|
29
|
-
_(
|
|
30
|
-
OpenStruct.new(
|
|
31
|
-
"title" => "vulcan ears",
|
|
32
|
-
"track" => "Nine"
|
|
33
|
-
).extend(representer).to_hash
|
|
34
|
-
).must_equal({
|
|
35
|
-
"title" => "vulcan ears",
|
|
36
|
-
"track" => "NINE,#{{"title" => "vulcan ears"}.inspect},#{{}.inspect}"
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
describe "#parse_filter" do
|
|
41
|
-
representer! do
|
|
42
|
-
property :track,
|
|
43
|
-
:parse_filter => [
|
|
44
|
-
->(input, _options) { "#{input}-1" },
|
|
45
|
-
->(input, _options) { "#{input}-2" }
|
|
46
|
-
],
|
|
47
|
-
:render_filter => [
|
|
48
|
-
->(val, _options) { "#{val}-1" },
|
|
49
|
-
->(val, _options) { "#{val}-2" }
|
|
50
|
-
]
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# order matters.
|
|
54
|
-
it { _(OpenStruct.new.extend(representer).from_hash("track" => "Nine").track).must_equal "Nine-1-2" }
|
|
55
|
-
it { _(OpenStruct.new("track" => "Nine").extend(representer).to_hash).must_equal({"track"=>"Nine-1-2"}) }
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# class RenderFilterTest < Minitest::Spec
|
|
60
|
-
# representer! do
|
|
61
|
-
# property :track, :render_filter => [lambda { |val, options| "#{val}-1" } ]
|
|
62
|
-
# property :track, :render_filter => [lambda { |val, options| "#{val}-2" } ], :inherit => true
|
|
63
|
-
# end
|
|
64
|
-
|
|
65
|
-
# it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
|
|
66
|
-
# end
|
data/test/for_collection_test.rb
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ForCollectionTest < Minitest::Spec
|
|
4
|
-
module SongRepresenter
|
|
5
|
-
include Representable::JSON
|
|
6
|
-
|
|
7
|
-
property :name
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
let(:songs) { [Song.new("Days Go By"), Song.new("Can't Take Them All")] }
|
|
11
|
-
let(:json) { "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]" }
|
|
12
|
-
|
|
13
|
-
# Module.for_collection
|
|
14
|
-
# Decorator.for_collection
|
|
15
|
-
for_formats(
|
|
16
|
-
:hash => [Representable::Hash, out = [{"name" => "Days Go By"}, {"name"=>"Can't Take Them All"}], out],
|
|
17
|
-
:json => [Representable::JSON, out = "[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]", out]
|
|
18
|
-
# :xml => [Representable::XML, out="<a><song></song><song></song></a>", out]
|
|
19
|
-
) do |format, mod, output, input|
|
|
20
|
-
describe "Module::for_collection [#{format}]" do
|
|
21
|
-
let(:format) { format }
|
|
22
|
-
|
|
23
|
-
let(:representer) do
|
|
24
|
-
Module.new do
|
|
25
|
-
include mod
|
|
26
|
-
property :name # , :as => :title
|
|
27
|
-
|
|
28
|
-
collection_representer :class => Song
|
|
29
|
-
|
|
30
|
-
# self.representation_wrap = :songs if format == :xml
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it { render(songs.extend(representer.for_collection)).must_equal_document output }
|
|
35
|
-
it { render(representer.for_collection.prepare(songs)).must_equal_document output }
|
|
36
|
-
# parsing needs the class set, at least
|
|
37
|
-
it { _(parse([].extend(representer.for_collection), input)).must_equal songs }
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
describe "Module::for_collection without configuration [#{format}]" do
|
|
41
|
-
let(:format) { format }
|
|
42
|
-
|
|
43
|
-
let(:representer) do
|
|
44
|
-
Module.new do
|
|
45
|
-
include mod
|
|
46
|
-
property :name
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# rendering works out of the box, no config necessary
|
|
51
|
-
it { render(songs.extend(representer.for_collection)).must_equal_document output }
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
describe "Decorator::for_collection [#{format}]" do
|
|
55
|
-
let(:format) { format }
|
|
56
|
-
let(:representer) do
|
|
57
|
-
Class.new(Representable::Decorator) do
|
|
58
|
-
include mod
|
|
59
|
-
property :name
|
|
60
|
-
|
|
61
|
-
collection_representer :class => Song
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it { render(representer.for_collection.new(songs)).must_equal_document output }
|
|
66
|
-
it { _(parse(representer.for_collection.new([]), input)).must_equal songs }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
# with module including module
|
|
71
|
-
end
|
data/test/generic_test.rb
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
# TODO: rename/restructure to CollectionTest.
|
|
4
|
-
class GenericTest < Minitest::Spec
|
|
5
|
-
let(:new_album) { OpenStruct.new.extend(representer) }
|
|
6
|
-
let(:album) { OpenStruct.new(:songs => ["Fuck Armageddon"]).extend(representer) }
|
|
7
|
-
let(:song) { OpenStruct.new(:title => "Resist Stance") }
|
|
8
|
-
let(:song_representer) { Module.new { include Representable::Hash; property :title } }
|
|
9
|
-
|
|
10
|
-
describe "::collection" do
|
|
11
|
-
representer! do
|
|
12
|
-
collection :songs
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "doesn't initialize property" do
|
|
16
|
-
new_album.from_hash({})
|
|
17
|
-
_(new_album.songs).must_be_nil
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it "leaves properties untouched" do
|
|
21
|
-
album.from_hash({})
|
|
22
|
-
# TODO: test property.
|
|
23
|
-
_(album.songs).must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# when collection is nil, it doesn't get rendered:
|
|
27
|
-
for_formats(
|
|
28
|
-
:hash => [Representable::Hash, {}],
|
|
29
|
-
:xml => [Representable::XML, "<album></album>"],
|
|
30
|
-
:yaml => [Representable::YAML, "--- {}\n"] # FIXME: this doesn't look right.
|
|
31
|
-
) do |format, mod, output, _input|
|
|
32
|
-
describe "nil collections" do
|
|
33
|
-
let(:format) { format }
|
|
34
|
-
|
|
35
|
-
representer!(:module => mod) do
|
|
36
|
-
collection :songs
|
|
37
|
-
self.representation_wrap = :album if format == :xml
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
let(:album) { Album.new.extend(representer) }
|
|
41
|
-
|
|
42
|
-
it "doesn't render collection in #{format}" do
|
|
43
|
-
render(album).must_equal_document output
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# when collection is set but empty, render the empty collection.
|
|
49
|
-
for_formats(
|
|
50
|
-
:hash => [Representable::Hash, {"songs" => []}],
|
|
51
|
-
# :xml => [Representable::XML, "<open_struct><songs/></open_struct>"],
|
|
52
|
-
:yaml => [Representable::YAML, "---\nsongs: []\n"]
|
|
53
|
-
) do |format, mod, output, _input|
|
|
54
|
-
describe "empty collections" do
|
|
55
|
-
let(:format) { format }
|
|
56
|
-
|
|
57
|
-
representer!(:module => mod) do
|
|
58
|
-
collection :songs
|
|
59
|
-
self.representation_wrap = :album if format == :xml
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
|
63
|
-
|
|
64
|
-
it "renders empty collection in #{format}" do
|
|
65
|
-
render(album).must_equal_document output
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
# when collection is [], suppress rendering when render_empty: false.
|
|
71
|
-
for_formats(
|
|
72
|
-
:hash => [Representable::Hash, {}],
|
|
73
|
-
# :xml => [Representable::XML, "<open_struct><songs/></open_struct>"],
|
|
74
|
-
:yaml => [Representable::YAML, "--- {}\n"]
|
|
75
|
-
) do |format, mod, output, _input|
|
|
76
|
-
describe "render_empty [#{format}]" do
|
|
77
|
-
let(:format) { format }
|
|
78
|
-
|
|
79
|
-
representer!(:module => mod) do
|
|
80
|
-
collection :songs, :render_empty => false
|
|
81
|
-
self.representation_wrap = :album if format == :xml
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
|
85
|
-
|
|
86
|
-
it { render(album).must_equal_document output }
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# wrap_test
|
|
92
|
-
for_formats(
|
|
93
|
-
:hash => [Representable::Hash, {}]
|
|
94
|
-
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
|
|
95
|
-
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
|
96
|
-
) do |format, mod, input|
|
|
97
|
-
describe "parsing [#{format}] with wrap where wrap is missing" do
|
|
98
|
-
representer!(:module => mod) do
|
|
99
|
-
self.representation_wrap = :song
|
|
100
|
-
|
|
101
|
-
property :title
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
it "doesn't change represented object" do
|
|
105
|
-
_(song.extend(representer).send("from_#{format}", input).title).must_equal "Resist Stance"
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
data/test/getter_setter_test.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class GetterSetterTest < BaseTest
|
|
4
|
-
representer! do
|
|
5
|
-
property :name, # key under :name.
|
|
6
|
-
:getter => ->(user_options:, **) { "#{user_options[:welcome]} #{song_name}" },
|
|
7
|
-
:setter => ->(user_options:, input:, **) { self.song_name = "#{user_options[:welcome]} #{input}" }
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
subject { Struct.new(:song_name).new("Mony Mony").extend(representer) }
|
|
11
|
-
|
|
12
|
-
it "uses :getter when rendering" do
|
|
13
|
-
subject.instance_eval { def name; fail; end }
|
|
14
|
-
_(subject.to_hash(user_options: {welcome: "Hi"})).must_equal({"name" => "Hi Mony Mony"})
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it "uses :setter when parsing" do
|
|
18
|
-
subject.instance_eval { def name=(*); fail; end; self }
|
|
19
|
-
_(
|
|
20
|
-
subject.from_hash(
|
|
21
|
-
{"name" => "Eyes Without A Face"},
|
|
22
|
-
user_options: {welcome: "Hello"}
|
|
23
|
-
).song_name
|
|
24
|
-
).must_equal "Hello Eyes Without A Face"
|
|
25
|
-
end
|
|
26
|
-
end
|
data/test/hash_bindings_test.rb
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class HashBindingTest < Minitest::Spec
|
|
4
|
-
module SongRepresenter
|
|
5
|
-
include Representable::JSON
|
|
6
|
-
property :name
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
class SongWithRepresenter < ::Song
|
|
10
|
-
include Representable
|
|
11
|
-
include SongRepresenter
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
describe "PropertyBinding" do
|
|
15
|
-
describe "#read" do
|
|
16
|
-
before do
|
|
17
|
-
@property = Representable::Hash::Binding.new(Representable::Definition.new(:song))
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it "returns fragment if present" do
|
|
21
|
-
assert_equal "Stick The Flag Up Your Goddamn Ass, You Sonofabitch",
|
|
22
|
-
@property.read({"song" => "Stick The Flag Up Your Goddamn Ass, You Sonofabitch"}, "song")
|
|
23
|
-
assert_equal "", @property.read({"song" => ""}, "song")
|
|
24
|
-
assert_nil @property.read({"song" => nil}, "song")
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "returns FRAGMENT_NOT_FOUND if not in document" do
|
|
28
|
-
assert_equal Representable::Binding::FragmentNotFound, @property.read({}, "song")
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it "will not fail if given an empty hash value and will return FRAGMENT_NOT_FOUND" do
|
|
32
|
-
assert_equal Representable::Binding::FragmentNotFound, @property.read(nil, "song")
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
describe "CollectionBinding" do
|
|
38
|
-
describe "with plain text items" do
|
|
39
|
-
before do
|
|
40
|
-
@property = Representable::Hash::Binding::Collection.new(Representable::Definition.new(:songs, :collection => true))
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
it "extracts with #read" do
|
|
44
|
-
assert_equal ["The Gargoyle", "Bronx"], @property.read({"songs" => ["The Gargoyle", "Bronx"]}, "songs")
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it "inserts with #write" do
|
|
48
|
-
doc = {}
|
|
49
|
-
assert_equal(["The Gargoyle", "Bronx"], @property.write(doc, ["The Gargoyle", "Bronx"], "songs"))
|
|
50
|
-
assert_equal({"songs"=>["The Gargoyle", "Bronx"]}, doc)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
describe "HashBinding" do
|
|
56
|
-
describe "with plain text items" do
|
|
57
|
-
before do
|
|
58
|
-
@property = Representable::Hash::Binding.new(Representable::Definition.new(:songs, :hash => true))
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
it "extracts with #read" do
|
|
62
|
-
assert_equal(
|
|
63
|
-
{"first" => "The Gargoyle", "second" => "Bronx"},
|
|
64
|
-
@property.read({"songs" => {"first" => "The Gargoyle", "second" => "Bronx"}}, "songs")
|
|
65
|
-
)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it "inserts with #write" do
|
|
69
|
-
doc = {}
|
|
70
|
-
assert_equal(
|
|
71
|
-
{"first" => "The Gargoyle", "second" => "Bronx"},
|
|
72
|
-
@property.write(doc, {"first" => "The Gargoyle", "second" => "Bronx"}, "songs")
|
|
73
|
-
)
|
|
74
|
-
assert_equal({"songs"=>{"first" => "The Gargoyle", "second" => "Bronx"}}, doc)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
describe "with objects" do
|
|
79
|
-
before do
|
|
80
|
-
@property = Representable::Hash::Binding.new(
|
|
81
|
-
Representable::Definition.new(
|
|
82
|
-
:songs, :hash => true, :class => Song,
|
|
83
|
-
:extend => SongRepresenter
|
|
84
|
-
)
|
|
85
|
-
)
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
it "doesn't change the represented hash in #write" do
|
|
89
|
-
song = Song.new("Better Than That")
|
|
90
|
-
hash = {"first" => song}
|
|
91
|
-
@property.write({}, hash, "song")
|
|
92
|
-
assert_equal({"first" => song}, hash)
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|