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/coercion_test.rb
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "test_helper"
|
|
4
|
-
require "representable/coercion"
|
|
5
|
-
|
|
6
|
-
class CoercionTest < Minitest::Spec
|
|
7
|
-
representer! do
|
|
8
|
-
include Representable::Coercion
|
|
9
|
-
|
|
10
|
-
property :title # no coercion.
|
|
11
|
-
property :length, type: Representable::Coercion::Types::Params::Float
|
|
12
|
-
|
|
13
|
-
property :band, class: OpenStruct do
|
|
14
|
-
property :founded, type: Representable::Coercion::Types::Params::Integer
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
collection :songs, class: OpenStruct do
|
|
18
|
-
property :ok, type: Representable::Coercion::Types::Params::Bool
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
let(:album) do
|
|
23
|
-
OpenStruct.new(
|
|
24
|
-
title: "Dire Straits", length: 41.34,
|
|
25
|
-
band: OpenStruct.new(founded: "1977"),
|
|
26
|
-
songs: [OpenStruct.new(ok: 1), OpenStruct.new(ok: 0)]
|
|
27
|
-
)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it do
|
|
31
|
-
_(album.extend(representer).to_hash).must_equal(
|
|
32
|
-
{
|
|
33
|
-
"title" => "Dire Straits",
|
|
34
|
-
"length" => 41.34,
|
|
35
|
-
"band" => {"founded" => 1977},
|
|
36
|
-
"songs" => [{"ok" => true}, {"ok" => false}]
|
|
37
|
-
}
|
|
38
|
-
)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it {
|
|
42
|
-
album = OpenStruct.new
|
|
43
|
-
album.extend(representer)
|
|
44
|
-
album.from_hash(
|
|
45
|
-
{
|
|
46
|
-
"title" => "Dire Straits",
|
|
47
|
-
"length" => "41.34",
|
|
48
|
-
"band" => {"founded" => "1977"},
|
|
49
|
-
"songs" => [{"ok" => 1}, {"ok" => 0}]
|
|
50
|
-
}
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
# it
|
|
54
|
-
_(album.length).must_equal 41.34
|
|
55
|
-
_(album.band.founded).must_equal 1977
|
|
56
|
-
_(album.songs[0].ok).must_equal true
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
describe "with user :parse_filter and :render_filter" do
|
|
60
|
-
representer! do
|
|
61
|
-
include Representable::Coercion
|
|
62
|
-
|
|
63
|
-
property :length, type: Representable::Coercion::Types::Params::Float,
|
|
64
|
-
parse_filter: ->(input, _options) { "#{input}.1" }, # happens BEFORE coercer.
|
|
65
|
-
render_filter: ->(fragment, *) { "#{fragment}.1" }
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# user's :parse_filter(s) are run before coercion.
|
|
69
|
-
it { _(OpenStruct.new.extend(representer).from_hash("length" => "1").length).must_equal 1.1 }
|
|
70
|
-
# user's :render_filter(s) are run before coercion.
|
|
71
|
-
it { _(OpenStruct.new(length: 1).extend(representer).to_hash).must_equal({"length" => 1.1}) }
|
|
72
|
-
end
|
|
73
|
-
end
|
data/test/config/inherit_test.rb
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
# tests defining representers in modules, decorators and classes and the inheritance when combined.
|
|
4
|
-
|
|
5
|
-
class ConfigInheritTest < Minitest::Spec
|
|
6
|
-
def assert_cloned(child, parent, property)
|
|
7
|
-
child_def = child.representable_attrs.get(property)
|
|
8
|
-
parent_def = parent.representable_attrs.get(property)
|
|
9
|
-
|
|
10
|
-
child_def.merge!(:alias => property)
|
|
11
|
-
|
|
12
|
-
_(child_def[:alias]).wont_equal parent_def[:alias]
|
|
13
|
-
_(child_def.object_id).wont_equal parent_def.object_id
|
|
14
|
-
end
|
|
15
|
-
# class Object
|
|
16
|
-
|
|
17
|
-
# end
|
|
18
|
-
module GenreModule
|
|
19
|
-
include Representable::Hash
|
|
20
|
-
property :genre
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# in Decorator ------------------------------------------------
|
|
24
|
-
class Decorator < Representable::Decorator
|
|
25
|
-
include Representable::Hash
|
|
26
|
-
property :title
|
|
27
|
-
property :artist do
|
|
28
|
-
property :id
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it { _(Decorator.definitions.keys).must_equal %w[title artist] }
|
|
33
|
-
|
|
34
|
-
# in inheriting Decorator
|
|
35
|
-
|
|
36
|
-
class InheritingDecorator < Decorator
|
|
37
|
-
property :location
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it { _(InheritingDecorator.definitions.keys).must_equal %w[title artist location] }
|
|
41
|
-
it { assert_cloned(InheritingDecorator, Decorator, "title") }
|
|
42
|
-
it do
|
|
43
|
-
_(InheritingDecorator.representable_attrs.get(:artist).representer_module.object_id).wont_equal Decorator.representable_attrs.get(:artist).representer_module.object_id
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# in inheriting and including Decorator
|
|
47
|
-
|
|
48
|
-
class InheritingAndIncludingDecorator < Decorator
|
|
49
|
-
include GenreModule
|
|
50
|
-
property :location
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
it { _(InheritingAndIncludingDecorator.definitions.keys).must_equal %w[title artist genre location] }
|
|
54
|
-
it { assert_cloned(InheritingAndIncludingDecorator, GenreModule, :genre) }
|
|
55
|
-
|
|
56
|
-
# in module ---------------------------------------------------
|
|
57
|
-
module Module
|
|
58
|
-
include Representable
|
|
59
|
-
property :title
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it { _(Module.definitions.keys).must_equal ["title"] }
|
|
63
|
-
|
|
64
|
-
# in module including module
|
|
65
|
-
module SubModule
|
|
66
|
-
include Representable
|
|
67
|
-
include Module
|
|
68
|
-
|
|
69
|
-
property :location
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it { _(SubModule.definitions.keys).must_equal %w[title location] }
|
|
73
|
-
it { assert_cloned(SubModule, Module, :title) }
|
|
74
|
-
|
|
75
|
-
# including preserves order
|
|
76
|
-
module IncludingModule
|
|
77
|
-
include Representable
|
|
78
|
-
property :genre
|
|
79
|
-
include Module
|
|
80
|
-
|
|
81
|
-
property :location
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it { _(IncludingModule.definitions.keys).must_equal %w[genre title location] }
|
|
85
|
-
|
|
86
|
-
# included in class -------------------------------------------
|
|
87
|
-
class Class
|
|
88
|
-
include Representable
|
|
89
|
-
include IncludingModule
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
it { _(Class.definitions.keys).must_equal %w[genre title location] }
|
|
93
|
-
it { assert_cloned(Class, IncludingModule, :title) }
|
|
94
|
-
it { assert_cloned(Class, IncludingModule, :location) }
|
|
95
|
-
it { assert_cloned(Class, IncludingModule, :genre) }
|
|
96
|
-
|
|
97
|
-
# included in class with order
|
|
98
|
-
class DefiningClass
|
|
99
|
-
include Representable
|
|
100
|
-
property :street_cred
|
|
101
|
-
include IncludingModule
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
it { _(DefiningClass.definitions.keys).must_equal %w[street_cred genre title location] }
|
|
105
|
-
|
|
106
|
-
# in class
|
|
107
|
-
class RepresenterClass
|
|
108
|
-
include Representable
|
|
109
|
-
property :title
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
it { _(RepresenterClass.definitions.keys).must_equal ["title"] }
|
|
113
|
-
|
|
114
|
-
# in inheriting class
|
|
115
|
-
class InheritingClass < RepresenterClass
|
|
116
|
-
include Representable
|
|
117
|
-
property :location
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
it { _(InheritingClass.definitions.keys).must_equal %w[title location] }
|
|
121
|
-
it { assert_cloned(InheritingClass, RepresenterClass, :title) }
|
|
122
|
-
|
|
123
|
-
# in inheriting class and including
|
|
124
|
-
class InheritingAndIncludingClass < RepresenterClass
|
|
125
|
-
property :location
|
|
126
|
-
include GenreModule
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
it { _(InheritingAndIncludingClass.definitions.keys).must_equal %w[title location genre] }
|
|
130
|
-
end
|
data/test/config_test.rb
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ConfigTest < Minitest::Spec
|
|
4
|
-
subject { Representable::Config.new(Representable::Definition) }
|
|
5
|
-
PunkRock = Class.new
|
|
6
|
-
Definition = Representable::Definition
|
|
7
|
-
|
|
8
|
-
describe "wrapping" do
|
|
9
|
-
it "returns false per default" do
|
|
10
|
-
assert_nil subject.wrap_for("Punk", nil)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
# it "infers a printable class name if set to true" do
|
|
14
|
-
# subject.wrap = true
|
|
15
|
-
# assert_equal "punk_rock", subject.wrap_for(PunkRock, nil)
|
|
16
|
-
# end
|
|
17
|
-
|
|
18
|
-
# it "can be set explicitely" do
|
|
19
|
-
# subject.wrap = "Descendents"
|
|
20
|
-
# assert_equal "Descendents", subject.wrap_for(PunkRock, nil)
|
|
21
|
-
# end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
describe "#[]" do
|
|
25
|
-
# does return nil for non-existent
|
|
26
|
-
it { _(subject[:hello]).must_be_nil }
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# describe "#[]" do
|
|
30
|
-
# before { subject.add(:title, {:me => true}) }
|
|
31
|
-
|
|
32
|
-
# it { subject[:unknown].must_be_nil }
|
|
33
|
-
# it { subject.get(:title)[:me].must_equal true }
|
|
34
|
-
# it { subject["title"][:me].must_equal true }
|
|
35
|
-
# end
|
|
36
|
-
|
|
37
|
-
# []=
|
|
38
|
-
# []=(... inherit: true)
|
|
39
|
-
# forwarded to Config#definitions
|
|
40
|
-
# that goes to ConfigDefinitionsTest
|
|
41
|
-
describe "#add" do
|
|
42
|
-
describe "returns" do
|
|
43
|
-
it do
|
|
44
|
-
# #add returns Definition.`
|
|
45
|
-
subject = Representable::Config.new(Representable::Definition).add(:title, {:me => true})
|
|
46
|
-
|
|
47
|
-
_(subject).must_be_kind_of Representable::Definition
|
|
48
|
-
_(subject[:me]).must_equal true
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
before { subject.add(:title, {:me => true}) }
|
|
53
|
-
|
|
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 }
|
|
58
|
-
|
|
59
|
-
# this is actually tested in context in inherit_test.
|
|
60
|
-
it "overrides former definition" do
|
|
61
|
-
subject.add(:title, {:peer => Module})
|
|
62
|
-
_(subject.get(:title)[:me]).must_be_nil
|
|
63
|
-
_(subject.get(:title)[:peer]).must_equal Module
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
describe "inherit: true" do
|
|
67
|
-
before do
|
|
68
|
-
subject.add(:title, {:me => true})
|
|
69
|
-
subject.add(:title, {:peer => Module, :inherit => true})
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it { _(subject.get(:title)[:me]).must_equal true }
|
|
73
|
-
it { _(subject.get(:title)[:peer]).must_equal Module }
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
describe "#remove" do
|
|
78
|
-
subject { Representable::Config.new(Representable::Definition) }
|
|
79
|
-
|
|
80
|
-
it do
|
|
81
|
-
subject.add(:title, {:me => true})
|
|
82
|
-
subject.add(:genre, {})
|
|
83
|
-
_(subject.get(:genre)).must_be_kind_of Representable::Definition
|
|
84
|
-
|
|
85
|
-
subject.remove(:genre)
|
|
86
|
-
_(subject.get(:genre)).must_be_nil
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
describe "#each" do
|
|
91
|
-
before { subject.add(:title, {:me => true}) }
|
|
92
|
-
|
|
93
|
-
it "what" do
|
|
94
|
-
definitions = []
|
|
95
|
-
subject.each { |dfn| definitions << dfn }
|
|
96
|
-
_(definitions.size).must_equal 1
|
|
97
|
-
_(definitions[0][:me]).must_equal true
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
describe "#options" do
|
|
102
|
-
it { _(subject.options).must_equal({}) }
|
|
103
|
-
it do
|
|
104
|
-
subject.options[:namespacing] = true
|
|
105
|
-
_(subject.options[:namespacing]).must_equal true
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
describe "#get" do
|
|
110
|
-
subject { Representable::Config.new(Representable::Definition) }
|
|
111
|
-
|
|
112
|
-
it do
|
|
113
|
-
title = subject.add(:title, {})
|
|
114
|
-
length = subject.add(:length, {})
|
|
115
|
-
|
|
116
|
-
_(subject.get(:title)).must_equal title
|
|
117
|
-
_(subject.get(:length)).must_equal length
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
# TODO: remove in 2.0.
|
|
4
|
-
class DecoratorScopeTest < Minitest::Spec
|
|
5
|
-
representer! do
|
|
6
|
-
property :title, :getter => ->(*) { title_from_representer }, :decorator_scope => true
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
let(:representer_with_method) do
|
|
10
|
-
Module.new do
|
|
11
|
-
include Representable::Hash
|
|
12
|
-
property :title, :decorator_scope => true
|
|
13
|
-
def title; "Crystal Planet"; end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it "executes lambdas in represented context" do
|
|
18
|
-
_(
|
|
19
|
-
Class.new {
|
|
20
|
-
def title_from_representer
|
|
21
|
-
"Sounds Of Silence"
|
|
22
|
-
end
|
|
23
|
-
}.new.extend(representer).to_hash
|
|
24
|
-
).must_equal({"title"=>"Sounds Of Silence"})
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "executes method in represented context" do
|
|
28
|
-
_(Object.new.extend(representer_with_method).to_hash).must_equal({"title"=>"Crystal Planet"})
|
|
29
|
-
end
|
|
30
|
-
end
|
data/test/decorator_test.rb
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class DecoratorTest < Minitest::Spec
|
|
4
|
-
class SongRepresentation < Representable::Decorator
|
|
5
|
-
include Representable::JSON
|
|
6
|
-
property :name
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
class AlbumRepresentation < Representable::Decorator
|
|
10
|
-
include Representable::JSON
|
|
11
|
-
|
|
12
|
-
collection :songs, :class => Song, :extend => SongRepresentation
|
|
13
|
-
end
|
|
14
|
-
|
|
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") }
|
|
26
|
-
|
|
27
|
-
describe "inheritance" do
|
|
28
|
-
let(:inherited_decorator) do
|
|
29
|
-
Class.new(AlbumRepresentation) {
|
|
30
|
-
property :best_song
|
|
31
|
-
}.new(Album.new([song], "Stand Up"))
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it { _(inherited_decorator.to_hash).must_equal({"songs" => [{"name"=>"Mama, I'm Coming Home"}], "best_song" => "Stand Up"}) }
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
let(:decorator) { AlbumRepresentation.new(album) }
|
|
38
|
-
|
|
39
|
-
let(:rating_decorator) { RatingRepresentation.new(rating) }
|
|
40
|
-
|
|
41
|
-
it "renders" do
|
|
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?
|
|
45
|
-
# no @representable_attrs in decorated objects
|
|
46
|
-
_(song).wont_be(:instance_variable_defined?, :@representable_attrs)
|
|
47
|
-
|
|
48
|
-
_(rating_decorator.to_hash).must_equal({"system" => "MPAA", "value" => "R"})
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
describe "#from_hash" do
|
|
52
|
-
it "returns represented" do
|
|
53
|
-
_(decorator.from_hash({"songs"=>[{"name"=>"Mama, I'm Coming Home"}]})).must_equal album
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it "parses" do
|
|
57
|
-
decorator.from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
|
|
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?
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
describe "#decorated" do
|
|
66
|
-
it "is aliased to #represented" do
|
|
67
|
-
_(AlbumRepresentation.prepare(album).decorated).must_equal album
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
describe "inline decorators" do
|
|
72
|
-
representer!(decorator: true) do
|
|
73
|
-
collection :songs, :class => Song do
|
|
74
|
-
property :name
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
it "does not pollute represented" do
|
|
79
|
-
representer.new(album).from_hash({"songs"=>[{"name"=>"Atomic Garden"}]})
|
|
80
|
-
|
|
81
|
-
# no @representable_attrs in decorated objects
|
|
82
|
-
_(song).wont_be(:instance_variable_defined?, :@representable_attrs)
|
|
83
|
-
_(album).wont_be(:instance_variable_defined?, :@representable_attrs)
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
require "uber/inheritable_attr"
|
|
89
|
-
class InheritanceWithDecoratorTest < Minitest::Spec
|
|
90
|
-
class Twin
|
|
91
|
-
extend Uber::InheritableAttr
|
|
92
|
-
inheritable_attr :representer_class
|
|
93
|
-
self.representer_class = Class.new(Representable::Decorator) { include Representable::Hash }
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
class Album < Twin
|
|
97
|
-
representer_class.property :title # Twin.representer_class.clone
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
# Twin.representer_class.clone
|
|
101
|
-
class Song < Twin
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
it do
|
|
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
|
|
108
|
-
end
|
|
109
|
-
end
|
data/test/default_test.rb
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class DefaultTest < Minitest::Spec
|
|
4
|
-
Song = Struct.new(:id, :title)
|
|
5
|
-
|
|
6
|
-
representer! do
|
|
7
|
-
property :id
|
|
8
|
-
property :title, default: "Huber Breeze" #->(options) { options[:default] }
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
describe "#from_hash" do
|
|
12
|
-
let(:song) { Song.new.extend(representer) }
|
|
13
|
-
|
|
14
|
-
it { _(song.from_hash({})).must_equal Song.new(nil, "Huber Breeze") }
|
|
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") }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "#to_json" do
|
|
22
|
-
it "uses :default when not available from object" do
|
|
23
|
-
_(Song.new.extend(representer).to_hash).must_equal({"title"=>"Huber Breeze"})
|
|
24
|
-
end
|
|
25
|
-
|
|
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"})
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it "uses value from represented object when emtpy string" do
|
|
31
|
-
_(Song.new(nil, "").extend(representer).to_hash).must_equal({"title"=>""})
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class DefaultsOptionsTest < BaseTest
|
|
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
|
-
|
|
8
|
-
describe "hash options combined with dynamic options" do
|
|
9
|
-
representer! do
|
|
10
|
-
defaults render_nil: true do |name|
|
|
11
|
-
{as: name.to_s.upcase}
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
property :title
|
|
15
|
-
property :author_name
|
|
16
|
-
property :description
|
|
17
|
-
property :song_volume
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it {
|
|
21
|
-
render(prepared).must_equal_document(
|
|
22
|
-
{
|
|
23
|
-
"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "DESCRIPTION" => nil,
|
|
24
|
-
"SONG_VOLUME" => 20
|
|
25
|
-
}
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe "with only dynamic property options" do
|
|
31
|
-
representer! do
|
|
32
|
-
defaults do |name|
|
|
33
|
-
{as: name.to_s.upcase}
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
property :title
|
|
37
|
-
property :author_name
|
|
38
|
-
property :description
|
|
39
|
-
property :song_volume
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "SONG_VOLUME" => 20}) }
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
describe "with only hashes" do
|
|
46
|
-
representer! do
|
|
47
|
-
defaults render_nil: true
|
|
48
|
-
|
|
49
|
-
property :title
|
|
50
|
-
property :author_name
|
|
51
|
-
property :description
|
|
52
|
-
property :song_volume
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it {
|
|
56
|
-
render(prepared).must_equal_document(
|
|
57
|
-
{
|
|
58
|
-
"title" => "Revolution", "author_name" => "Some author", "description" => nil,
|
|
59
|
-
"song_volume" => 20
|
|
60
|
-
}
|
|
61
|
-
)
|
|
62
|
-
}
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
describe "direct defaults hash" do
|
|
66
|
-
representer! do
|
|
67
|
-
defaults render_nil: true
|
|
68
|
-
|
|
69
|
-
property :title
|
|
70
|
-
property :author_name
|
|
71
|
-
property :description
|
|
72
|
-
property :song_volume
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
it {
|
|
76
|
-
render(prepared).must_equal_document(
|
|
77
|
-
{
|
|
78
|
-
"title" => "Revolution", "author_name" => "Some author", "description" => nil,
|
|
79
|
-
"song_volume" => 20
|
|
80
|
-
}
|
|
81
|
-
)
|
|
82
|
-
}
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
describe "direct defaults hash with dynamic options" do
|
|
86
|
-
representer! do
|
|
87
|
-
defaults render_nil: true do |name|
|
|
88
|
-
{as: name.to_s.upcase}
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
property :title
|
|
92
|
-
property :author_name
|
|
93
|
-
property :description
|
|
94
|
-
property :song_volume
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
it {
|
|
98
|
-
render(prepared).must_equal_document(
|
|
99
|
-
{
|
|
100
|
-
"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "DESCRIPTION" => nil,
|
|
101
|
-
"SONG_VOLUME" => 20
|
|
102
|
-
}
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
describe "prioritizes specific options" do
|
|
108
|
-
representer! do
|
|
109
|
-
defaults render_nil: true do |name|
|
|
110
|
-
{as: name.to_s.upcase}
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
property :title
|
|
114
|
-
property :author_name
|
|
115
|
-
property :description, render_nil: false
|
|
116
|
-
property :song_volume, as: :volume
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "volume" => 20}) }
|
|
120
|
-
end
|
|
121
|
-
end
|