representable 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ci.yml +17 -0
- data/CHANGES.md +25 -0
- data/Gemfile +4 -12
- data/LICENSE +1 -1
- data/README.md +6 -6
- data/Rakefile +1 -6
- data/TODO +1 -3
- data/TODO-4.0.md +72 -0
- data/lib/representable.rb +19 -25
- 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.rb +9 -2
- 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_methods.rb +3 -2
- data/lib/representable/insert.rb +1 -1
- data/lib/representable/json.rb +8 -7
- data/lib/representable/json/collection.rb +3 -0
- data/lib/representable/object.rb +1 -1
- data/lib/representable/object/binding.rb +5 -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/virtus_coercion.rb +38 -0
- data/lib/representable/xml.rb +12 -10
- data/lib/representable/xml/binding.rb +19 -13
- data/lib/representable/xml/namespace.rb +122 -0
- data/lib/representable/yaml.rb +6 -2
- data/lib/representable/yaml/binding.rb +1 -0
- 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/virtus_coercion_test.rb +52 -0
- 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 +101 -39
- 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/examples/object.rb
CHANGED
@@ -7,8 +7,6 @@ require "pp"
|
|
7
7
|
source = OpenStruct.new(name: "30 Years Live", songs: [
|
8
8
|
OpenStruct.new(id: 1, title: "Dear Beloved"), OpenStruct.new(id: 2, title: "Fuck Armageddon")])
|
9
9
|
|
10
|
-
pp source
|
11
|
-
|
12
10
|
require "representable/object"
|
13
11
|
|
14
12
|
class AlbumRepresenter < Representable::Decorator
|
@@ -26,6 +24,4 @@ Song = Struct.new(:title)
|
|
26
24
|
|
27
25
|
target = Album.new
|
28
26
|
|
29
|
-
AlbumRepresenter.new(target).from_object(source)
|
30
|
-
|
31
|
-
pp target
|
27
|
+
AlbumRepresenter.new(target).from_object(source)
|
data/test/exec_context_test.rb
CHANGED
@@ -7,8 +7,8 @@ class ExecContextTest < MiniTest::Spec
|
|
7
7
|
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
8
8
|
) do |format, mod, input, output|
|
9
9
|
|
10
|
-
let
|
11
|
-
let
|
10
|
+
let(:song) { representer.prepare(Song.new("Timing")) }
|
11
|
+
let(:format) { format }
|
12
12
|
|
13
13
|
|
14
14
|
describe "exec_context: nil" do
|
@@ -17,7 +17,7 @@ class ExecContextTest < MiniTest::Spec
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it { render(song).must_equal_document output }
|
20
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
20
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
21
21
|
end
|
22
22
|
|
23
23
|
|
@@ -27,7 +27,7 @@ class ExecContextTest < MiniTest::Spec
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it { render(song).must_equal_document output }
|
30
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
30
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
31
31
|
end
|
32
32
|
|
33
33
|
|
@@ -41,7 +41,7 @@ class ExecContextTest < MiniTest::Spec
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
44
|
-
it { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
|
44
|
+
it { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
45
45
|
end
|
46
46
|
|
47
47
|
|
@@ -53,7 +53,7 @@ class ExecContextTest < MiniTest::Spec
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it { render(song).must_equal_document output }
|
56
|
-
it { parse(song, input).name.must_equal "Rebel Fate" }
|
56
|
+
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
57
57
|
end
|
58
58
|
|
59
59
|
|
@@ -71,7 +71,7 @@ class ExecContextTest < MiniTest::Spec
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it { render(song).must_equal_document({Representable::Decorator=>"Timebomb"}) }
|
74
|
-
it { parse(song, {Representable::Decorator=>"Listless"}).name.must_equal "Listless" }
|
74
|
+
it { _(parse(song, {Representable::Decorator=>"Listless"}).name).must_equal "Listless" }
|
75
75
|
end
|
76
76
|
|
77
77
|
|
@@ -86,7 +86,7 @@ class ExecContextTest < MiniTest::Spec
|
|
86
86
|
end
|
87
87
|
|
88
88
|
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
89
|
-
it("xxx") { parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name.must_equal "Rebel Fate" }
|
89
|
+
it("xxx") { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
data/test/features_test.rb
CHANGED
@@ -20,23 +20,23 @@ class FeaturesTest < MiniTest::Spec
|
|
20
20
|
end
|
21
21
|
}
|
22
22
|
|
23
|
-
let
|
23
|
+
let(:song) { OpenStruct.new(:details => Object.new) }
|
24
24
|
|
25
25
|
describe "Module" do
|
26
26
|
representer! do
|
27
|
-
instance_exec
|
27
|
+
instance_exec(&definition)
|
28
28
|
end
|
29
29
|
|
30
|
-
it { song.extend(representer).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
30
|
+
it { _(song.extend(representer).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
31
31
|
end
|
32
32
|
|
33
33
|
|
34
34
|
describe "Decorator" do
|
35
35
|
representer!(:decorator => true) do
|
36
|
-
instance_exec
|
36
|
+
instance_exec(&definition)
|
37
37
|
end
|
38
38
|
|
39
|
-
it { representer.new(song).to_hash.must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
39
|
+
it { _(representer.new(song).to_hash).must_equal({"title"=>"Is It A Lie", "length"=>"2:31", "details"=>{"title"=>"Is It A Lie"}}) }
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -65,6 +65,6 @@ class FeatureInclusionOrderTest < MiniTest::Spec
|
|
65
65
|
end
|
66
66
|
|
67
67
|
it do
|
68
|
-
representer.new(OpenStruct.new(song: Object)).to_hash.must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
|
68
|
+
_(representer.new(OpenStruct.new(song: Object)).to_hash).must_equal({"title"=>"I am number two, I was first!", "song"=>{"title"=>"I am number two, I was first!"}})
|
69
69
|
end
|
70
70
|
end
|
data/test/filter_test.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class FilterPipelineTest < MiniTest::Spec
|
4
|
-
let
|
5
|
-
let
|
4
|
+
let(:block1) { lambda { |input, options| "1: #{input}" } }
|
5
|
+
let(:block2) { lambda { |input, options| "2: #{input}" } }
|
6
6
|
|
7
7
|
subject { Representable::Pipeline[block1, block2] }
|
8
8
|
|
9
|
-
it { subject.call("Horowitz", {}).must_equal "2: 1: Horowitz" }
|
9
|
+
it { _(subject.call("Horowitz", {})).must_equal "2: 1: Horowitz" }
|
10
10
|
end
|
11
11
|
|
12
12
|
|
@@ -22,11 +22,11 @@ class FilterTest < MiniTest::Spec
|
|
22
22
|
# gets doc and options.
|
23
23
|
it {
|
24
24
|
song = OpenStruct.new.extend(representer).from_hash("title" => "VULCAN EARS", "track" => "Nine")
|
25
|
-
song.title.must_equal "VULCAN EARS"
|
26
|
-
song.track.must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
|
25
|
+
_(song.title).must_equal "VULCAN EARS"
|
26
|
+
_(song.track).must_equal "nine,{\"title\"=>\"VULCAN EARS\", \"track\"=>\"Nine\"}"
|
27
27
|
}
|
28
28
|
|
29
|
-
it { OpenStruct.new("title" => "vulcan ears", "track" => "Nine").extend(representer).to_hash.must_equal( {"title"=>"vulcan ears", "track"=>"NINE,{\"title\"=>\"vulcan ears\"},{}"}) }
|
29
|
+
it { _(OpenStruct.new("title" => "vulcan ears", "track" => "Nine").extend(representer).to_hash).must_equal( {"title"=>"vulcan ears", "track"=>"NINE,{\"title\"=>\"vulcan ears\"},{}"}) }
|
30
30
|
|
31
31
|
|
32
32
|
describe "#parse_filter" do
|
@@ -41,8 +41,8 @@ class FilterTest < MiniTest::Spec
|
|
41
41
|
end
|
42
42
|
|
43
43
|
# order matters.
|
44
|
-
it { OpenStruct.new.extend(representer).from_hash("track" => "Nine").track.must_equal "Nine-1-2" }
|
45
|
-
it { OpenStruct.new("track" => "Nine").extend(representer).to_hash.must_equal({"track"=>"Nine-1-2"}) }
|
44
|
+
it { _(OpenStruct.new.extend(representer).from_hash("track" => "Nine").track).must_equal "Nine-1-2" }
|
45
|
+
it { _(OpenStruct.new("track" => "Nine").extend(representer).to_hash).must_equal({"track"=>"Nine-1-2"}) }
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
data/test/for_collection_test.rb
CHANGED
@@ -7,8 +7,8 @@ class ForCollectionTest < MiniTest::Spec
|
|
7
7
|
property :name
|
8
8
|
end
|
9
9
|
|
10
|
-
let
|
11
|
-
let
|
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
12
|
|
13
13
|
|
14
14
|
# Module.for_collection
|
@@ -20,9 +20,9 @@ class ForCollectionTest < MiniTest::Spec
|
|
20
20
|
) do |format, mod, output, input|
|
21
21
|
|
22
22
|
describe "Module::for_collection [#{format}]" do
|
23
|
-
let
|
23
|
+
let(:format) { format }
|
24
24
|
|
25
|
-
let
|
25
|
+
let(:representer) {
|
26
26
|
Module.new do
|
27
27
|
include mod
|
28
28
|
property :name#, :as => :title
|
@@ -36,13 +36,13 @@ class ForCollectionTest < MiniTest::Spec
|
|
36
36
|
it { render(songs.extend(representer.for_collection)).must_equal_document output }
|
37
37
|
it { render(representer.for_collection.prepare(songs)).must_equal_document output }
|
38
38
|
# parsing needs the class set, at least
|
39
|
-
it { parse([].extend(representer.for_collection), input).must_equal songs }
|
39
|
+
it { _(parse([].extend(representer.for_collection), input)).must_equal songs }
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "Module::for_collection without configuration [#{format}]" do
|
43
|
-
let
|
43
|
+
let(:format) { format }
|
44
44
|
|
45
|
-
let
|
45
|
+
let(:representer) {
|
46
46
|
Module.new do
|
47
47
|
include mod
|
48
48
|
property :name
|
@@ -55,8 +55,8 @@ class ForCollectionTest < MiniTest::Spec
|
|
55
55
|
|
56
56
|
|
57
57
|
describe "Decorator::for_collection [#{format}]" do
|
58
|
-
let
|
59
|
-
let
|
58
|
+
let(:format) { format }
|
59
|
+
let(:representer) {
|
60
60
|
Class.new(Representable::Decorator) do
|
61
61
|
include mod
|
62
62
|
property :name
|
@@ -66,7 +66,7 @@ class ForCollectionTest < MiniTest::Spec
|
|
66
66
|
}
|
67
67
|
|
68
68
|
it { render(representer.for_collection.new(songs)).must_equal_document output }
|
69
|
-
it { parse(representer.for_collection.new([]), input).must_equal songs }
|
69
|
+
it { _(parse(representer.for_collection.new([]), input)).must_equal songs }
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
data/test/generic_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
4
|
-
let
|
5
|
-
let
|
6
|
-
let
|
7
|
-
let
|
4
|
+
let(:new_album) { OpenStruct.new.extend(representer) }
|
5
|
+
let(:album) { OpenStruct.new(:songs => ["Fuck Armageddon"]).extend(representer) }
|
6
|
+
let(:song) { OpenStruct.new(:title => "Resist Stance") }
|
7
|
+
let(:song_representer) { Module.new do include Representable::Hash; property :title end }
|
8
8
|
|
9
9
|
|
10
10
|
describe "::collection" do
|
@@ -14,13 +14,13 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
14
14
|
|
15
15
|
it "doesn't initialize property" do
|
16
16
|
new_album.from_hash({})
|
17
|
-
new_album.songs.
|
17
|
+
_(new_album.songs).must_be_nil
|
18
18
|
end
|
19
19
|
|
20
20
|
it "leaves properties untouched" do
|
21
21
|
album.from_hash({})
|
22
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.
|
23
|
+
_(album.songs).must_equal ["Fuck Armageddon"] # when the collection is not present in the incoming hash, this propery stays untouched.
|
24
24
|
end
|
25
25
|
|
26
26
|
|
@@ -32,14 +32,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
32
32
|
) do |format, mod, output, input|
|
33
33
|
|
34
34
|
describe "nil collections" do
|
35
|
-
let
|
35
|
+
let(:format) { format }
|
36
36
|
|
37
37
|
representer!(:module => mod) do
|
38
38
|
collection :songs
|
39
39
|
self.representation_wrap = :album if format == :xml
|
40
40
|
end
|
41
41
|
|
42
|
-
let
|
42
|
+
let(:album) { Album.new.extend(representer) }
|
43
43
|
|
44
44
|
it "doesn't render collection in #{format}" do
|
45
45
|
render(album).must_equal_document output
|
@@ -55,14 +55,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
55
55
|
) do |format, mod, output, input|
|
56
56
|
|
57
57
|
describe "empty collections" do
|
58
|
-
let
|
58
|
+
let(:format) { format }
|
59
59
|
|
60
60
|
representer!(:module => mod) do
|
61
61
|
collection :songs
|
62
62
|
self.representation_wrap = :album if format == :xml
|
63
63
|
end
|
64
64
|
|
65
|
-
let
|
65
|
+
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
66
66
|
|
67
67
|
it "renders empty collection in #{format}" do
|
68
68
|
render(album).must_equal_document output
|
@@ -79,14 +79,14 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
79
79
|
) do |format, mod, output, input|
|
80
80
|
|
81
81
|
describe "render_empty [#{format}]" do
|
82
|
-
let
|
82
|
+
let(:format) { format }
|
83
83
|
|
84
84
|
representer!(:module => mod) do
|
85
85
|
collection :songs, :render_empty => false
|
86
86
|
self.representation_wrap = :album if format == :xml
|
87
87
|
end
|
88
88
|
|
89
|
-
let
|
89
|
+
let(:album) { OpenStruct.new(:songs => []).extend(representer) }
|
90
90
|
|
91
91
|
it { render(album).must_equal_document output }
|
92
92
|
end
|
@@ -109,7 +109,7 @@ class GenericTest < MiniTest::Spec # TODO: rename/restructure to CollectionTest.
|
|
109
109
|
end
|
110
110
|
|
111
111
|
it "doesn't change represented object" do
|
112
|
-
song.extend(representer).send("from_#{format}", input).title.must_equal "Resist Stance"
|
112
|
+
_(song.extend(representer).send("from_#{format}", input).title).must_equal "Resist Stance"
|
113
113
|
end
|
114
114
|
end
|
115
115
|
end
|
data/test/getter_setter_test.rb
CHANGED
@@ -3,19 +3,19 @@ require 'test_helper'
|
|
3
3
|
class GetterSetterTest < BaseTest
|
4
4
|
representer! do
|
5
5
|
property :name, # key under :name.
|
6
|
-
:getter => lambda { |
|
7
|
-
:setter => lambda { |
|
6
|
+
:getter => lambda { |user_options:, **| "#{user_options[:welcome]} #{song_name}" },
|
7
|
+
:setter => lambda { |user_options:, input:, **| self.song_name = "#{user_options[:welcome]} #{input}" }
|
8
8
|
end
|
9
9
|
|
10
10
|
subject { Struct.new(:song_name).new("Mony Mony").extend(representer) }
|
11
11
|
|
12
12
|
it "uses :getter when rendering" do
|
13
13
|
subject.instance_eval { def name; raise; end }
|
14
|
-
subject.to_hash(user_options: {welcome: "Hi"}).must_equal({"name" => "Hi Mony Mony"})
|
14
|
+
_(subject.to_hash(user_options: {welcome: "Hi"})).must_equal({"name" => "Hi Mony Mony"})
|
15
15
|
end
|
16
16
|
|
17
17
|
it "uses :setter when parsing" do
|
18
18
|
subject.instance_eval { def name=(*); raise; end; self }
|
19
|
-
subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name.must_equal "Hello Eyes Without A Face"
|
19
|
+
_(subject.from_hash({"name" => "Eyes Without A Face"}, user_options: {welcome: "Hello"}).song_name).must_equal "Hello Eyes Without A Face"
|
20
20
|
end
|
21
|
-
end
|
21
|
+
end
|
data/test/hash_bindings_test.rb
CHANGED
@@ -21,7 +21,7 @@ class HashBindingTest < MiniTest::Spec
|
|
21
21
|
it "returns fragment if present" do
|
22
22
|
assert_equal "Stick The Flag Up Your Goddamn Ass, You Sonofabitch", @property.read({"song" => "Stick The Flag Up Your Goddamn Ass, You Sonofabitch"}, "song")
|
23
23
|
assert_equal "", @property.read({"song" => ""}, "song")
|
24
|
-
|
24
|
+
assert_nil @property.read({"song" => nil}, "song")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "returns FRAGMENT_NOT_FOUND if not in document" do
|
data/test/hash_test.rb
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
class HashPublicMethodsTest < Minitest::Spec
|
4
|
+
#---
|
5
|
+
# from_hash
|
6
|
+
class BandRepresenter < Representable::Decorator
|
7
|
+
include Representable::Hash
|
8
|
+
property :id
|
9
|
+
property :name
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:data) { {"id"=>1,"name"=>"Rancid"} }
|
13
|
+
|
14
|
+
it { _(BandRepresenter.new(Band.new).from_hash(data)[:id, :name]).must_equal [1, "Rancid"] }
|
15
|
+
it { _(BandRepresenter.new(Band.new).parse(data)[:id, :name]).must_equal [1, "Rancid"] }
|
16
|
+
|
17
|
+
#---
|
18
|
+
# to_hash
|
19
|
+
let(:band) { Band.new(1, "Rancid") }
|
20
|
+
|
21
|
+
it { _(BandRepresenter.new(band).to_hash).must_equal data }
|
22
|
+
it { _(BandRepresenter.new(band).render).must_equal data }
|
23
|
+
end
|
24
|
+
|
3
25
|
class HashWithScalarPropertyTest < MiniTest::Spec
|
4
26
|
Album = Struct.new(:title)
|
5
27
|
|
@@ -7,24 +29,24 @@ class HashWithScalarPropertyTest < MiniTest::Spec
|
|
7
29
|
property :title
|
8
30
|
end
|
9
31
|
|
10
|
-
let
|
32
|
+
let(:album) { Album.new("Liar") }
|
11
33
|
|
12
34
|
describe "#to_hash" do
|
13
35
|
it "renders plain property" do
|
14
|
-
album.extend(representer).to_hash.must_equal("title" => "Liar")
|
36
|
+
_(album.extend(representer).to_hash).must_equal("title" => "Liar")
|
15
37
|
end
|
16
38
|
end
|
17
39
|
|
18
40
|
|
19
41
|
describe "#from_hash" do
|
20
42
|
it "parses plain property" do
|
21
|
-
album.extend(representer).from_hash("title" => "This Song Is Recycled").title.must_equal "This Song Is Recycled"
|
43
|
+
_(album.extend(representer).from_hash("title" => "This Song Is Recycled").title).must_equal "This Song Is Recycled"
|
22
44
|
end
|
23
45
|
|
24
46
|
# Fixes issue #115
|
25
47
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
26
48
|
album = Album.new
|
27
|
-
album.title.
|
49
|
+
_(album.title).must_be_nil
|
28
50
|
album.extend(representer).from_hash("title" => nil)
|
29
51
|
end
|
30
52
|
end
|
@@ -40,39 +62,39 @@ class HashWithTypedPropertyTest < MiniTest::Spec
|
|
40
62
|
end
|
41
63
|
end
|
42
64
|
|
43
|
-
let
|
65
|
+
let(:album) { Album.new(Song.new("Liar")) }
|
44
66
|
|
45
67
|
describe "#to_hash" do
|
46
68
|
it "renders embedded typed property" do
|
47
|
-
album.extend(representer).to_hash.must_equal("best_song" => {"name" => "Liar"})
|
69
|
+
_(album.extend(representer).to_hash).must_equal("best_song" => {"name" => "Liar"})
|
48
70
|
end
|
49
71
|
end
|
50
72
|
|
51
73
|
describe "#from_hash" do
|
52
74
|
it "parses embedded typed property" do
|
53
75
|
album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
|
54
|
-
album.best_song.name.must_equal "Go With Me"
|
76
|
+
_(album.best_song.name).must_equal "Go With Me"
|
55
77
|
end
|
56
78
|
|
57
79
|
# nested nil removes nested object.
|
58
80
|
it do
|
59
81
|
album = Album.new(Song.new("Pre-medicated Murder"))
|
60
82
|
album.extend(representer).from_hash("best_song" => nil)
|
61
|
-
album.best_song.
|
83
|
+
_(album.best_song).must_be_nil
|
62
84
|
end
|
63
85
|
|
64
86
|
# nested blank hash creates blank object when not populated.
|
65
87
|
it do
|
66
88
|
album = Album.new#(Song.new("Pre-medicated Murder"))
|
67
89
|
album.extend(representer).from_hash("best_song" => {})
|
68
|
-
album.best_song.name.
|
90
|
+
_(album.best_song.name).must_be_nil
|
69
91
|
end
|
70
92
|
|
71
93
|
# Fixes issue #115
|
72
94
|
it "allows nil value in the incoming document and corresponding nil value for the represented" do
|
73
95
|
album = Album.new
|
74
96
|
album.extend(representer).from_hash("best_song" => nil)
|
75
|
-
album.best_song.
|
97
|
+
_(album.best_song).must_be_nil
|
76
98
|
end
|
77
99
|
end
|
78
100
|
end
|
@@ -85,10 +107,10 @@ class HashWithTypedPropertyAndAs < MiniTest::Spec
|
|
85
107
|
end
|
86
108
|
end
|
87
109
|
|
88
|
-
let
|
110
|
+
let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }
|
89
111
|
|
90
|
-
it { album.to_hash.must_equal("hit" => {"name" => "Liar"}) }
|
91
|
-
it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
112
|
+
it { _(album.to_hash).must_equal("hit" => {"name" => "Liar"}) }
|
113
|
+
it { _(album.from_hash("hit" => {"name" => "Go With Me"})).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
92
114
|
end
|
93
115
|
# # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
|
94
116
|
# # hash_song = Module.new do
|
@@ -97,13 +119,13 @@ end
|
|
97
119
|
# # property :name
|
98
120
|
# # end
|
99
121
|
|
100
|
-
# # let
|
122
|
+
# # let(:hash_album) { Module.new do
|
101
123
|
# # include Representable::XML
|
102
124
|
# # self.representation_wrap = :album
|
103
125
|
# # property :song, :extend => hash_song, :class => Song, :as => :hit
|
104
126
|
# # end }
|
105
127
|
|
106
|
-
# # let
|
128
|
+
# # let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(hash_album) }
|
107
129
|
|
108
130
|
# # it { album.to_xml.must_equal_xml("<album><hit><name>Liar</name></hit></album>") }
|
109
131
|
# # #it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
|
@@ -122,18 +144,18 @@ class HashWithTypedCollectionTest < MiniTest::Spec
|
|
122
144
|
end
|
123
145
|
end
|
124
146
|
|
125
|
-
let
|
147
|
+
let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
|
126
148
|
|
127
149
|
describe "#to_hash" do
|
128
150
|
it "renders collection of typed property" do
|
129
|
-
album.extend(representer).to_hash.must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
151
|
+
_(album.extend(representer).to_hash).must_equal("songs" => [{"name" => "Liar", "track" => 1}, {"name" => "What I Know", "track" => 2}])
|
130
152
|
end
|
131
153
|
end
|
132
154
|
|
133
155
|
describe "#from_hash" do
|
134
156
|
it "parses collection of typed property" do
|
135
|
-
album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
136
|
-
{"name" => "Three Way Dance", "track" => 5}]).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
157
|
+
_(album.extend(representer).from_hash("songs" => [{"name" => "One Shot Deal", "track" => 4},
|
158
|
+
{"name" => "Three Way Dance", "track" => 5}])).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
137
159
|
end
|
138
160
|
end
|
139
161
|
end
|
@@ -142,19 +164,19 @@ class HashWithScalarCollectionTest < MiniTest::Spec
|
|
142
164
|
Album = Struct.new(:songs)
|
143
165
|
representer! { collection :songs }
|
144
166
|
|
145
|
-
let
|
167
|
+
let(:album) { Album.new(["Jackhammer", "Terrible Man"]) }
|
146
168
|
|
147
169
|
|
148
170
|
describe "#to_hash" do
|
149
171
|
it "renders a block style list per default" do
|
150
|
-
album.extend(representer).to_hash.must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
172
|
+
_(album.extend(representer).to_hash).must_equal("songs" => ["Jackhammer", "Terrible Man"])
|
151
173
|
end
|
152
174
|
end
|
153
175
|
|
154
176
|
|
155
177
|
describe "#from_hash" do
|
156
178
|
it "parses a block style list" do
|
157
|
-
album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"]).must_equal Album.new(["Off Key Melody", "Sinking"])
|
179
|
+
_(album.extend(representer).from_hash("songs" => ["Off Key Melody", "Sinking"])).must_equal Album.new(["Off Key Melody", "Sinking"])
|
158
180
|
end
|
159
181
|
end
|
160
|
-
end
|
182
|
+
end
|