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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +6 -0
  3. data/Gemfile +0 -1
  4. data/README.md +1 -7
  5. data/lib/representable/json.rb +11 -14
  6. data/lib/representable/version.rb +1 -1
  7. data/representable.gemspec +1 -2
  8. metadata +1 -60
  9. data/test/as_test.rb +0 -60
  10. data/test/benchmarking.rb +0 -78
  11. data/test/binding_test.rb +0 -54
  12. data/test/cached_test.rb +0 -177
  13. data/test/class_test.rb +0 -132
  14. data/test/coercion_test.rb +0 -73
  15. data/test/config/inherit_test.rb +0 -130
  16. data/test/config_test.rb +0 -120
  17. data/test/decorator_scope_test.rb +0 -30
  18. data/test/decorator_test.rb +0 -109
  19. data/test/default_test.rb +0 -34
  20. data/test/defaults_options_test.rb +0 -121
  21. data/test/definition_test.rb +0 -253
  22. data/test/examples/example.rb +0 -314
  23. data/test/examples/object.rb +0 -28
  24. data/test/exec_context_test.rb +0 -88
  25. data/test/features_test.rb +0 -89
  26. data/test/filter_test.rb +0 -66
  27. data/test/for_collection_test.rb +0 -71
  28. data/test/generic_test.rb +0 -109
  29. data/test/getter_setter_test.rb +0 -26
  30. data/test/hash_bindings_test.rb +0 -96
  31. data/test/hash_test.rb +0 -197
  32. data/test/heritage_test.rb +0 -64
  33. data/test/if_test.rb +0 -83
  34. data/test/include_exclude_test.rb +0 -96
  35. data/test/inherit_test.rb +0 -246
  36. data/test/inline_test.rb +0 -281
  37. data/test/instance_test.rb +0 -289
  38. data/test/is_representable_test.rb +0 -79
  39. data/test/json_test.rb +0 -379
  40. data/test/lonely_test.rb +0 -241
  41. data/test/nested_test.rb +0 -112
  42. data/test/object_test.rb +0 -60
  43. data/test/option_test.rb +0 -38
  44. data/test/parse_pipeline_test.rb +0 -68
  45. data/test/pipeline_test.rb +0 -309
  46. data/test/populator_test.rb +0 -108
  47. data/test/prepare_test.rb +0 -64
  48. data/test/private_options_test.rb +0 -19
  49. data/test/reader_writer_test.rb +0 -19
  50. data/test/realistic_benchmark.rb +0 -96
  51. data/test/render_nil_test.rb +0 -21
  52. data/test/represent_test.rb +0 -84
  53. data/test/representable_test.rb +0 -526
  54. data/test/schema_test.rb +0 -175
  55. data/test/serialize_deserialize_test.rb +0 -33
  56. data/test/skip_test.rb +0 -92
  57. data/test/stringify_hash_test.rb +0 -39
  58. data/test/test_helper.rb +0 -153
  59. data/test/test_helper_test.rb +0 -25
  60. data/test/uncategorized_test.rb +0 -72
  61. data/test/user_options_test.rb +0 -24
  62. data/test/wrap_test.rb +0 -162
  63. data/test/xml_assertions.rb +0 -29
  64. data/test/xml_bindings_test.rb +0 -55
  65. data/test/xml_namespace_test.rb +0 -193
  66. data/test/xml_test.rb +0 -584
  67. data/test/yaml_test.rb +0 -209
@@ -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
@@ -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
@@ -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
@@ -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
data/test/hash_test.rb DELETED
@@ -1,197 +0,0 @@
1
- require "test_helper"
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
-
25
- class HashWithScalarPropertyTest < Minitest::Spec
26
- Album = Struct.new(:title)
27
-
28
- representer! do
29
- property :title
30
- end
31
-
32
- let(:album) { Album.new("Liar") }
33
-
34
- describe "#to_hash" do
35
- it "renders plain property" do
36
- _(album.extend(representer).to_hash).must_equal("title" => "Liar")
37
- end
38
- end
39
-
40
- describe "#from_hash" do
41
- it "parses plain property" do
42
- _(album.extend(representer).from_hash("title" => "This Song Is Recycled").title).must_equal "This Song Is Recycled"
43
- end
44
-
45
- # Fixes issue #115
46
- it "allows nil value in the incoming document and corresponding nil value for the represented" do
47
- album = Album.new
48
- _(album.title).must_be_nil
49
- album.extend(representer).from_hash("title" => nil)
50
- end
51
- end
52
- end
53
-
54
- class HashWithTypedPropertyTest < Minitest::Spec
55
- Album = Struct.new(:best_song)
56
-
57
- representer! do
58
- property :best_song, :class => Song do
59
- property :name
60
- end
61
- end
62
-
63
- let(:album) { Album.new(Song.new("Liar")) }
64
-
65
- describe "#to_hash" do
66
- it "renders embedded typed property" do
67
- _(album.extend(representer).to_hash).must_equal("best_song" => {"name" => "Liar"})
68
- end
69
- end
70
-
71
- describe "#from_hash" do
72
- it "parses embedded typed property" do
73
- album.extend(representer).from_hash("best_song" => {"name" => "Go With Me"})
74
- _(album.best_song.name).must_equal "Go With Me"
75
- end
76
-
77
- # nested nil removes nested object.
78
- it do
79
- album = Album.new(Song.new("Pre-medicated Murder"))
80
- album.extend(representer).from_hash("best_song" => nil)
81
- _(album.best_song).must_be_nil
82
- end
83
-
84
- # nested blank hash creates blank object when not populated.
85
- it do
86
- album = Album.new # (Song.new("Pre-medicated Murder"))
87
- album.extend(representer).from_hash("best_song" => {})
88
- _(album.best_song.name).must_be_nil
89
- end
90
-
91
- # Fixes issue #115
92
- it "allows nil value in the incoming document and corresponding nil value for the represented" do
93
- album = Album.new
94
- album.extend(representer).from_hash("best_song" => nil)
95
- _(album.best_song).must_be_nil
96
- end
97
- end
98
- end
99
-
100
- # TODO: move to AsTest.
101
- class HashWithTypedPropertyAndAs < Minitest::Spec
102
- representer! do
103
- property :song, :class => Song, :as => :hit do
104
- property :name
105
- end
106
- end
107
-
108
- let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(representer) }
109
-
110
- it { _(album.to_hash).must_equal("hit" => {"name" => "Liar"}) }
111
- it { _(album.from_hash("hit" => {"name" => "Go With Me"})).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
112
- end
113
- # # describe "FIXME COMBINE WITH ABOVE with :extend and :as" do
114
- # # hash_song = Module.new do
115
- # # include Representable::XML
116
- # # self.representation_wrap = :song
117
- # # property :name
118
- # # end
119
-
120
- # # let(:hash_album) { Module.new do
121
- # # include Representable::XML
122
- # # self.representation_wrap = :album
123
- # # property :song, :extend => hash_song, :class => Song, :as => :hit
124
- # # end }
125
-
126
- # # let(:album) { OpenStruct.new(:song => Song.new("Liar")).extend(hash_album) }
127
-
128
- # # it { album.to_xml.must_equal_xml("<album><hit><name>Liar</name></hit></album>") }
129
- # # #it { album.from_hash("hit" => {"name" => "Go With Me"}).must_equal OpenStruct.new(:song => Song.new("Go With Me")) }
130
- # # end
131
- # end
132
-
133
- class HashWithTypedCollectionTest < Minitest::Spec
134
- Album = Struct.new(:songs)
135
-
136
- representer! do
137
- collection :songs, class: Song do
138
- property :name
139
- property :track
140
- end
141
- end
142
-
143
- let(:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
144
-
145
- describe "#to_hash" do
146
- it "renders collection of typed property" do
147
- _(album.extend(representer).to_hash).must_equal(
148
- "songs" => [
149
- {"name" => "Liar", "track" => 1},
150
- {"name" => "What I Know", "track" => 2}
151
- ]
152
- )
153
- end
154
- end
155
-
156
- describe "#from_hash" do
157
- it "parses collection of typed property" do
158
- _(
159
- album.extend(representer).from_hash(
160
- "songs" => [
161
- {"name" => "One Shot Deal", "track" => 4},
162
- {
163
- "name" => "Three Way Dance",
164
- "track" => 5
165
- }
166
- ]
167
- )
168
- ).must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
169
- end
170
- end
171
- end
172
-
173
- class HashWithScalarCollectionTest < Minitest::Spec
174
- Album = Struct.new(:songs)
175
- representer! { collection :songs }
176
-
177
- let(:album) { Album.new(["Jackhammer", "Terrible Man"]) }
178
-
179
- describe "#to_hash" do
180
- it "renders a block style list per default" do
181
- _(album.extend(representer).to_hash).must_equal("songs" => ["Jackhammer", "Terrible Man"])
182
- end
183
- end
184
-
185
- describe "#from_hash" do
186
- it "parses a block style list" do
187
- _(
188
- album.extend(representer).from_hash(
189
- "songs" => [
190
- "Off Key Melody",
191
- "Sinking"
192
- ]
193
- )
194
- ).must_equal Album.new(["Off Key Melody", "Sinking"])
195
- end
196
- end
197
- end