representable 2.4.0.rc3 → 2.4.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +7 -2
- data/Rakefile +6 -0
- data/lib/representable.rb +21 -17
- data/lib/representable/binding.rb +3 -2
- data/lib/representable/definition.rb +1 -1
- data/lib/representable/deprecations.rb +31 -5
- data/lib/representable/deserializer.rb +24 -34
- data/lib/representable/hash/collection.rb +9 -2
- data/lib/representable/hash_methods.rb +2 -2
- data/lib/representable/parse_strategies.rb +6 -7
- data/lib/representable/pipeline.rb +12 -1
- data/lib/representable/pipeline_factories.rb +9 -2
- data/lib/representable/serializer.rb +3 -3
- data/lib/representable/version.rb +1 -1
- data/test-with-deprecations/as_test.rb +65 -0
- data/test-with-deprecations/benchmarking.rb +83 -0
- data/test-with-deprecations/binding_test.rb +46 -0
- data/test-with-deprecations/blaaaaaaaa_test.rb +69 -0
- data/test-with-deprecations/cached_test.rb +147 -0
- data/test-with-deprecations/class_test.rb +119 -0
- data/test-with-deprecations/coercion_test.rb +52 -0
- data/test-with-deprecations/config/inherit_test.rb +135 -0
- data/test-with-deprecations/config_test.rb +122 -0
- data/test-with-deprecations/decorator_scope_test.rb +28 -0
- data/test-with-deprecations/decorator_test.rb +96 -0
- data/test-with-deprecations/default_test.rb +34 -0
- data/test-with-deprecations/defaults_options_test.rb +93 -0
- data/test-with-deprecations/definition_test.rb +264 -0
- data/test-with-deprecations/example.rb +310 -0
- data/test-with-deprecations/examples/object.rb +31 -0
- data/test-with-deprecations/exec_context_test.rb +93 -0
- data/test-with-deprecations/features_test.rb +70 -0
- data/test-with-deprecations/filter_test.rb +57 -0
- data/test-with-deprecations/for_collection_test.rb +74 -0
- data/test-with-deprecations/generic_test.rb +116 -0
- data/test-with-deprecations/getter_setter_test.rb +21 -0
- data/test-with-deprecations/hash_bindings_test.rb +87 -0
- data/test-with-deprecations/hash_test.rb +160 -0
- data/test-with-deprecations/heritage_test.rb +62 -0
- data/test-with-deprecations/if_test.rb +79 -0
- data/test-with-deprecations/include_exclude_test.rb +88 -0
- data/test-with-deprecations/inherit_test.rb +159 -0
- data/test-with-deprecations/inline_test.rb +272 -0
- data/test-with-deprecations/instance_test.rb +266 -0
- data/test-with-deprecations/is_representable_test.rb +77 -0
- data/test-with-deprecations/json_test.rb +355 -0
- data/test-with-deprecations/lonely_test.rb +239 -0
- data/test-with-deprecations/mongoid_test.rb +31 -0
- data/test-with-deprecations/nested_test.rb +115 -0
- data/test-with-deprecations/object_test.rb +60 -0
- data/{test/---deserialize-pipeline_test.rb → test-with-deprecations/parse_pipeline_test.rb} +29 -2
- data/test-with-deprecations/parse_strategy_test.rb +279 -0
- data/{test → test-with-deprecations}/pass_options_test.rb +0 -0
- data/test-with-deprecations/pipeline_test.rb +277 -0
- data/test-with-deprecations/populator_test.rb +105 -0
- data/test-with-deprecations/prepare_test.rb +67 -0
- data/test-with-deprecations/private_options_test.rb +18 -0
- data/test-with-deprecations/reader_writer_test.rb +19 -0
- data/test-with-deprecations/realistic_benchmark.rb +115 -0
- data/test-with-deprecations/render_nil_test.rb +21 -0
- data/test-with-deprecations/represent_test.rb +88 -0
- data/test-with-deprecations/representable_test.rb +511 -0
- data/test-with-deprecations/schema_test.rb +148 -0
- data/test-with-deprecations/serialize_deserialize_test.rb +33 -0
- data/test-with-deprecations/skip_test.rb +81 -0
- data/test-with-deprecations/stringify_hash_test.rb +41 -0
- data/test-with-deprecations/test_helper.rb +135 -0
- data/test-with-deprecations/test_helper_test.rb +25 -0
- data/test-with-deprecations/uncategorized_test.rb +67 -0
- data/test-with-deprecations/user_options_test.rb +15 -0
- data/test-with-deprecations/wrap_test.rb +152 -0
- data/test-with-deprecations/xml_bindings_test.rb +62 -0
- data/test-with-deprecations/xml_test.rb +503 -0
- data/test-with-deprecations/yaml_test.rb +162 -0
- data/test/as_test.rb +3 -3
- data/test/cached_test.rb +2 -2
- data/test/class_test.rb +5 -5
- data/test/exec_context_test.rb +2 -2
- data/test/filter_test.rb +1 -1
- data/test/getter_setter_test.rb +4 -4
- data/test/if_test.rb +2 -2
- data/test/include_exclude_test.rb +88 -0
- data/test/instance_test.rb +15 -15
- data/test/lonely_test.rb +18 -2
- data/test/object_test.rb +4 -4
- data/test/parse_pipeline_test.rb +64 -0
- data/test/parse_strategy_test.rb +3 -3
- data/test/pipeline_test.rb +8 -12
- data/test/prepare_test.rb +2 -3
- data/test/reader_writer_test.rb +3 -3
- data/test/representable_test.rb +12 -48
- data/test/serialize_deserialize_test.rb +9 -9
- data/test/skip_test.rb +11 -11
- data/test/test_helper.rb +2 -0
- data/test/uncategorized_test.rb +10 -10
- data/test/user_options_test.rb +15 -0
- data/test/wrap_test.rb +1 -1
- metadata +65 -4
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class YamlTest < MiniTest::Spec
|
4
|
+
def self.yaml_representer(&block)
|
5
|
+
Module.new do
|
6
|
+
include Representable::YAML
|
7
|
+
instance_exec &block
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def yaml_representer(&block)
|
12
|
+
self.class.yaml_representer(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
describe "property" do
|
17
|
+
let (:yaml) { yaml_representer do property :best_song end }
|
18
|
+
|
19
|
+
let (:album) { Album.new.tap do |album|
|
20
|
+
album.best_song = "Liar"
|
21
|
+
end }
|
22
|
+
|
23
|
+
describe "#to_yaml" do
|
24
|
+
it "renders plain property" do
|
25
|
+
album.extend(yaml).to_yaml.must_equal(
|
26
|
+
"---
|
27
|
+
best_song: Liar
|
28
|
+
")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "always renders values into strings" do
|
32
|
+
Album.new.tap { |a| a.best_song = 8675309 }.extend(yaml).to_yaml.must_equal(
|
33
|
+
"---
|
34
|
+
best_song: 8675309
|
35
|
+
"
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
describe "#from_yaml" do
|
42
|
+
it "parses plain property" do
|
43
|
+
album.extend(yaml).from_yaml(
|
44
|
+
"---
|
45
|
+
best_song: This Song Is Recycled
|
46
|
+
").best_song.must_equal "This Song Is Recycled"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
describe "with :class and :extend" do
|
52
|
+
yaml_song = yaml_representer do property :name end
|
53
|
+
let (:yaml_album) { Module.new do
|
54
|
+
include Representable::YAML
|
55
|
+
property :best_song, :extend => yaml_song, :class => Song
|
56
|
+
end }
|
57
|
+
|
58
|
+
let (:album) { Album.new.tap do |album|
|
59
|
+
album.best_song = Song.new("Liar")
|
60
|
+
end }
|
61
|
+
|
62
|
+
|
63
|
+
describe "#to_yaml" do
|
64
|
+
it "renders embedded typed property" do
|
65
|
+
album.extend(yaml_album).to_yaml.must_equal "---
|
66
|
+
best_song:
|
67
|
+
name: Liar
|
68
|
+
"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#from_yaml" do
|
73
|
+
it "parses embedded typed property" do
|
74
|
+
album.extend(yaml_album).from_yaml("---
|
75
|
+
best_song:
|
76
|
+
name: Go With Me
|
77
|
+
").must_equal Album.new(nil,Song.new("Go With Me"))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
describe "collection" do
|
85
|
+
let (:yaml) { yaml_representer do collection :songs end }
|
86
|
+
|
87
|
+
let (:album) { Album.new.tap do |album|
|
88
|
+
album.songs = ["Jackhammer", "Terrible Man"]
|
89
|
+
end }
|
90
|
+
|
91
|
+
|
92
|
+
describe "#to_yaml" do
|
93
|
+
it "renders a block style list per default" do
|
94
|
+
album.extend(yaml).to_yaml.must_equal "---
|
95
|
+
songs:
|
96
|
+
- Jackhammer
|
97
|
+
- Terrible Man
|
98
|
+
"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "renders a flow style list when :style => :flow set" do
|
102
|
+
yaml = yaml_representer { collection :songs, :style => :flow }
|
103
|
+
album.extend(yaml).to_yaml.must_equal "---
|
104
|
+
songs: [Jackhammer, Terrible Man]
|
105
|
+
"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
describe "#from_yaml" do
|
111
|
+
it "parses a block style list" do
|
112
|
+
album.extend(yaml).from_yaml("---
|
113
|
+
songs:
|
114
|
+
- Off Key Melody
|
115
|
+
- Sinking").must_equal Album.new(["Off Key Melody", "Sinking"])
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
it "parses a flow style list" do
|
120
|
+
album.extend(yaml).from_yaml("---
|
121
|
+
songs: [Off Key Melody, Sinking]").must_equal Album.new(["Off Key Melody", "Sinking"])
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
describe "with :class and :extend" do
|
127
|
+
let (:yaml_album) { Module.new do
|
128
|
+
include Representable::YAML
|
129
|
+
collection :songs, :class => Song do
|
130
|
+
property :name
|
131
|
+
property :track
|
132
|
+
end
|
133
|
+
end }
|
134
|
+
|
135
|
+
let (:album) { Album.new([Song.new("Liar", 1), Song.new("What I Know", 2)]) }
|
136
|
+
|
137
|
+
|
138
|
+
describe "#to_yaml" do
|
139
|
+
it "renders collection of typed property" do
|
140
|
+
album.extend(yaml_album).to_yaml.must_equal "---
|
141
|
+
songs:
|
142
|
+
- name: Liar
|
143
|
+
track: 1
|
144
|
+
- name: What I Know
|
145
|
+
track: 2
|
146
|
+
"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "#from_yaml" do
|
151
|
+
it "parses collection of typed property" do
|
152
|
+
album.extend(yaml_album).from_yaml("---
|
153
|
+
songs:
|
154
|
+
- name: One Shot Deal
|
155
|
+
track: 4
|
156
|
+
- name: Three Way Dance
|
157
|
+
track: 5").must_equal Album.new([Song.new("One Shot Deal", 4), Song.new("Three Way Dance", 5)])
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/test/as_test.rb
CHANGED
@@ -33,11 +33,11 @@ class AsTest < MiniTest::Spec
|
|
33
33
|
|
34
34
|
describe "lambda arguments" do
|
35
35
|
representer! do
|
36
|
-
property :name, :as => lambda {
|
36
|
+
property :name, :as => lambda { |options| options[:user_options].inspect }
|
37
37
|
end
|
38
38
|
|
39
|
-
it { render(song, :volume
|
40
|
-
it { parse(song, {"
|
39
|
+
it { render(song, user_options:{volume: 1}).must_equal_document({"{:volume=>1}" => "Revolution"}) }
|
40
|
+
it { parse(song, {"{:volume=>1}" => "Wie Es Geht"}, user_options: {volume: 1}).name.must_equal "Wie Es Geht" }
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
data/test/cached_test.rb
CHANGED
@@ -14,7 +14,7 @@ class CachedTest < MiniTest::Spec
|
|
14
14
|
include Representable::Hash
|
15
15
|
feature Representable::Cached
|
16
16
|
|
17
|
-
property :title, render_filter: lambda { |input, options| "#{input}:#{options[:user_options]}" }
|
17
|
+
property :title, render_filter: lambda { |input, options| "#{input}:#{options[:options][:user_options]}" }
|
18
18
|
property :composer, class: Model::Artist do
|
19
19
|
property :name
|
20
20
|
end
|
@@ -41,7 +41,7 @@ class CachedTest < MiniTest::Spec
|
|
41
41
|
album2 = Model::Album.new("Louder And Even More Dangerous", [song2, song])
|
42
42
|
|
43
43
|
# makes sure options are passed correctly.
|
44
|
-
representer.to_hash(volume: 9).must_equal({"name"=>"Live And Dangerous",
|
44
|
+
representer.to_hash(user_options: {volume: 9}).must_equal({"name"=>"Live And Dangerous",
|
45
45
|
"songs"=>[{"title"=>"Jailbreak:{:volume=>9}"}, {"title"=>"Southbound:{:volume=>9}"}, {"title"=>"Emerald:{:volume=>9}"}]}) # called in Deserializer/Serializer
|
46
46
|
|
47
47
|
# representer becomes reusable as it is stateless.
|
data/test/class_test.rb
CHANGED
@@ -5,7 +5,7 @@ class ClassTest < BaseTest
|
|
5
5
|
class RepresentingSong
|
6
6
|
attr_reader :name
|
7
7
|
|
8
|
-
def from_hash(doc, *
|
8
|
+
def from_hash(doc, *)
|
9
9
|
@name = doc["__name__"]
|
10
10
|
|
11
11
|
self # DISCUSS: do we wanna be able to return whatever we want here? this is a trick to replace the actual object
|
@@ -66,10 +66,10 @@ class ClassTest < BaseTest
|
|
66
66
|
|
67
67
|
representer!(:inject => :klass) do
|
68
68
|
_klass = klass
|
69
|
-
property :song, :class => lambda { |
|
69
|
+
property :song, :class => lambda { |options| _klass.args=([options[:fragment],options[:user_options]]); _klass }
|
70
70
|
end
|
71
71
|
|
72
|
-
it { representer.prepare(OpenStruct.new).from_hash({"song" => {"name" => "Captured"}}, :volume
|
72
|
+
it { representer.prepare(OpenStruct.new).from_hash({"song" => {"name" => "Captured"}}, user_options: {volume: true}).song.class.args.
|
73
73
|
must_equal([{"name"=>"Captured"}, {:volume=>true}]) }
|
74
74
|
end
|
75
75
|
|
@@ -87,10 +87,10 @@ class ClassTest < BaseTest
|
|
87
87
|
|
88
88
|
representer!(:inject => :klass) do
|
89
89
|
_klass = klass
|
90
|
-
collection :songs, :class => lambda { |
|
90
|
+
collection :songs, :class => lambda { |options| _klass.args=([options[:fragment],options[:index],options[:user_options]]); _klass }
|
91
91
|
end
|
92
92
|
|
93
|
-
it { representer.prepare(OpenStruct.new).from_hash({"songs" => [{"name" => "Captured"}]}, :volume
|
93
|
+
it { representer.prepare(OpenStruct.new).from_hash({"songs" => [{"name" => "Captured"}]}, user_options: {volume: true}).songs.first.class.args.
|
94
94
|
must_equal([{"name"=>"Captured"}, 0, {:volume=>true}]) }
|
95
95
|
end
|
96
96
|
|
data/test/exec_context_test.rb
CHANGED
@@ -36,7 +36,7 @@ class ExecContextTest < MiniTest::Spec
|
|
36
36
|
property :name,
|
37
37
|
:as => lambda { |*| self.class }, # to actually test
|
38
38
|
:exec_context => :binding,
|
39
|
-
:setter => lambda { |
|
39
|
+
:setter => lambda { |options| represented.name = options[:fragment] # to make parsing work.
|
40
40
|
}
|
41
41
|
end
|
42
42
|
|
@@ -81,7 +81,7 @@ class ExecContextTest < MiniTest::Spec
|
|
81
81
|
property :name,
|
82
82
|
:as => lambda { |*| self.class }, # to actually test
|
83
83
|
:exec_context => :binding,
|
84
|
-
:setter => lambda { |
|
84
|
+
:setter => lambda { |options| represented.name = options[:fragment ] # to make parsing work.
|
85
85
|
}
|
86
86
|
end
|
87
87
|
|
data/test/filter_test.rb
CHANGED
@@ -16,7 +16,7 @@ class FilterTest < MiniTest::Spec
|
|
16
16
|
|
17
17
|
property :track,
|
18
18
|
:parse_filter => lambda { |input, options| "#{input.downcase},#{options[:doc]}" },
|
19
|
-
:render_filter => lambda { |val, options| "#{val.upcase},#{options[:doc]},#{options[:user_options]}" }
|
19
|
+
:render_filter => lambda { |val, options| "#{val.upcase},#{options[:doc]},#{options[:options][:user_options]}" }
|
20
20
|
end
|
21
21
|
|
22
22
|
# gets doc and options.
|
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 { |options| "#{options[:user_options][:welcome]} #{song_name}" },
|
7
|
+
:setter => lambda { |options| self.song_name = "#{options[:user_options][:welcome]} #{options[: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(:welcome
|
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"}, :welcome
|
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
21
|
end
|
data/test/if_test.rb
CHANGED
@@ -64,7 +64,7 @@ class IfTest < MiniTest::Spec
|
|
64
64
|
|
65
65
|
describe "propagating user options to the block" do
|
66
66
|
representer! do
|
67
|
-
property :name, :if => lambda { |opts| opts[:include_name] }
|
67
|
+
property :name, :if => lambda { |opts| opts[:user_options][:include_name] }
|
68
68
|
end
|
69
69
|
subject { OpenStruct.new(:name => "Outbound").extend(representer) }
|
70
70
|
|
@@ -73,7 +73,7 @@ class IfTest < MiniTest::Spec
|
|
73
73
|
end
|
74
74
|
|
75
75
|
it "passes user options to block" do
|
76
|
-
subject.to_hash(:include_name
|
76
|
+
subject.to_hash(user_options: { include_name: true }).must_equal({"name" => "Outbound"})
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class IncludeExcludeTest < Minitest::Spec
|
4
|
+
Song = Struct.new(:title, :artist, :id)
|
5
|
+
Artist = Struct.new(:name, :id, :songs)
|
6
|
+
|
7
|
+
representer!(decorator: true) do
|
8
|
+
property :title
|
9
|
+
property :artist, class: Artist do
|
10
|
+
property :name
|
11
|
+
property :id
|
12
|
+
|
13
|
+
collection :songs, class: Song do
|
14
|
+
property :title
|
15
|
+
property :id
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let (:song) { Song.new("Listless", Artist.new("7yearsbadluck", 1 )) }
|
21
|
+
let (:decorator) { representer.new(song) }
|
22
|
+
|
23
|
+
describe "#from_hash" do
|
24
|
+
it "accepts :exclude option" do
|
25
|
+
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, exclude: [:title])
|
26
|
+
|
27
|
+
song.title.must_equal "Listless"
|
28
|
+
song.artist.must_equal Artist.new(nil, 2)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "accepts :include option" do
|
32
|
+
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"id"=>2}}, include: [:title])
|
33
|
+
|
34
|
+
song.title.must_equal "Don't Smile In Trouble"
|
35
|
+
song.artist.must_equal Artist.new("7yearsbadluck", 1)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "accepts nested :exclude/:include option" do
|
39
|
+
decorator.from_hash({"title"=>"Don't Smile In Trouble", "artist"=>{"name"=>"Foo", "id"=>2, "songs"=>[{"id"=>1, "title"=>"Listless"}]}},
|
40
|
+
exclude: [:title],
|
41
|
+
artist: {
|
42
|
+
exclude: [:id],
|
43
|
+
songs: { include: [:title] }
|
44
|
+
}
|
45
|
+
)
|
46
|
+
|
47
|
+
song.title.must_equal "Listless"
|
48
|
+
song.artist.must_equal Artist.new("Foo", nil, [Song.new("Listless", nil, nil)])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#to_hash" do
|
53
|
+
it "accepts :exclude option" do
|
54
|
+
decorator.to_hash(exclude: [:title]).must_equal({"artist"=>{"name"=>"7yearsbadluck", "id"=>1}})
|
55
|
+
end
|
56
|
+
|
57
|
+
it "accepts :include option" do
|
58
|
+
decorator.to_hash(include: [:title]).must_equal({"title"=>"Listless"})
|
59
|
+
end
|
60
|
+
|
61
|
+
it "accepts nested :exclude/:include option" do
|
62
|
+
decorator = representer.new(Song.new("Listless", Artist.new("7yearsbadluck", 1, [Song.new("C.O.A.B.I.E.T.L.")])))
|
63
|
+
|
64
|
+
decorator.to_hash(
|
65
|
+
exclude: [:title],
|
66
|
+
artist: {
|
67
|
+
exclude: [:id],
|
68
|
+
songs: { include: [:title] }
|
69
|
+
}
|
70
|
+
).must_equal({"artist"=>{"name"=>"7yearsbadluck", "songs"=>[{"title"=>"C.O.A.B.I.E.T.L."}]}})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
it "xdoes not propagate private options to nested objects" do
|
75
|
+
Cover = Struct.new(:title, :original)
|
76
|
+
|
77
|
+
cover_rpr = Module.new do
|
78
|
+
include Representable::Hash
|
79
|
+
property :title
|
80
|
+
property :original, extend: self
|
81
|
+
end
|
82
|
+
|
83
|
+
# FIXME: we should test all representable-options (:include, :exclude, ?)
|
84
|
+
|
85
|
+
Cover.new("Roxanne", Cover.new("Roxanne (Don't Put On The Red Light)")).extend(cover_rpr).
|
86
|
+
to_hash(:include => [:original]).must_equal({"original"=>{"title"=>"Roxanne (Don't Put On The Red Light)"}})
|
87
|
+
end
|
88
|
+
end
|
data/test/instance_test.rb
CHANGED
@@ -10,7 +10,7 @@ class InstanceTest < BaseTest
|
|
10
10
|
|
11
11
|
describe "lambda { fragment } (new way of class: lambda { nil })" do
|
12
12
|
representer! do
|
13
|
-
property :title, :instance => lambda { |
|
13
|
+
property :title, :instance => lambda { |options| options[:fragment] }
|
14
14
|
end
|
15
15
|
|
16
16
|
it "skips creating new instance" do
|
@@ -21,7 +21,7 @@ class InstanceTest < BaseTest
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
song = OpenStruct.new.extend(representer).from_hash(
|
24
|
+
song = OpenStruct.new.extend(representer).from_hash({"title" => object})
|
25
25
|
song.title.must_equal object
|
26
26
|
end
|
27
27
|
end
|
@@ -32,7 +32,7 @@ class InstanceTest < BaseTest
|
|
32
32
|
describe "property with :instance" do
|
33
33
|
representer!(:inject => :song_representer) do
|
34
34
|
property :song,
|
35
|
-
:instance => lambda { |
|
35
|
+
:instance => lambda { |options| options[:fragment]["id"] == song.id ? song : Song.find(options[:fragment]["id"]) },
|
36
36
|
:extend => song_representer
|
37
37
|
end
|
38
38
|
|
@@ -47,8 +47,8 @@ class InstanceTest < BaseTest
|
|
47
47
|
describe "collection with :instance" do
|
48
48
|
representer!(:inject => :song_representer) do
|
49
49
|
collection :songs,
|
50
|
-
:instance => lambda { |
|
51
|
-
fragment["id"] == songs[
|
50
|
+
:instance => lambda { |options|
|
51
|
+
options[:fragment]["id"] == songs[options[:index]].id ? songs[options[:index]] : Song.find(options[:fragment]["id"])
|
52
52
|
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
53
53
|
|
54
54
|
:extend => song_representer
|
@@ -68,11 +68,11 @@ class InstanceTest < BaseTest
|
|
68
68
|
|
69
69
|
describe "property with lambda receiving fragment and args" do
|
70
70
|
representer!(:inject => :song_representer) do
|
71
|
-
property :song, :instance => lambda { |
|
71
|
+
property :song, :instance => lambda { |options| Struct.new(:args, :id).new([options[:fragment], options[:user_options]]) }, :extend => song_representer
|
72
72
|
end
|
73
73
|
|
74
74
|
it { OpenStruct.new(:song => Song.new(1, "The Answer Is Still No")).extend(representer).
|
75
|
-
from_hash({"song" => {"id" => 1}}, {:
|
75
|
+
from_hash({"song" => {"id" => 1}}, user_options: { volume: 1 }).song.args.must_equal([{"id"=>1}, {:volume=>1}]) }
|
76
76
|
end
|
77
77
|
|
78
78
|
# TODO: raise and test instance:{nil}
|
@@ -100,8 +100,8 @@ class InstanceTest < BaseTest
|
|
100
100
|
describe "sync" do
|
101
101
|
representer!(:inject => :song_representer) do
|
102
102
|
collection :songs,
|
103
|
-
:instance => lambda { |
|
104
|
-
songs[
|
103
|
+
:instance => lambda { |options|
|
104
|
+
songs[options[:index]]
|
105
105
|
},
|
106
106
|
:extend => song_representer,
|
107
107
|
# :parse_strategy => :sync
|
@@ -130,10 +130,10 @@ class InstanceTest < BaseTest
|
|
130
130
|
describe "update existing elements, only" do
|
131
131
|
representer!(:inject => :song_representer) do
|
132
132
|
collection :songs,
|
133
|
-
:instance => lambda { |
|
133
|
+
:instance => lambda { |options|
|
134
134
|
|
135
135
|
#fragment["id"] == songs[i].id ? songs[i] : Song.find(fragment["id"])
|
136
|
-
songs.find { |s| s.id == fragment["id"] }
|
136
|
+
songs.find { |s| s.id == options[:fragment]["id"] }
|
137
137
|
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
138
138
|
|
139
139
|
:extend => song_representer,
|
@@ -164,7 +164,7 @@ class InstanceTest < BaseTest
|
|
164
164
|
describe "add incoming elements, only" do
|
165
165
|
representer!(:inject => :song_representer) do
|
166
166
|
collection :songs,
|
167
|
-
:instance => lambda { |
|
167
|
+
:instance => lambda { |options|
|
168
168
|
songs << song=Song.new(2)
|
169
169
|
song
|
170
170
|
}, # let's not allow returning nil anymore. make sure we can still do everything as with nil. also, let's remove parse_strategy: sync.
|
@@ -195,8 +195,8 @@ class InstanceTest < BaseTest
|
|
195
195
|
describe "replace existing element" do
|
196
196
|
representer!(:inject => :song_representer) do
|
197
197
|
collection :songs,
|
198
|
-
:instance => lambda { |
|
199
|
-
id = fragment.delete("replace_id")
|
198
|
+
:instance => lambda { |options|
|
199
|
+
id = options[:fragment].delete("replace_id")
|
200
200
|
replaced = songs.find { |s| s.id == id }
|
201
201
|
songs[songs.index(replaced)] = song=Song.new(3)
|
202
202
|
song
|
@@ -249,7 +249,7 @@ class InstanceTest < BaseTest
|
|
249
249
|
describe "new syntax for instance: true" do
|
250
250
|
representer!(:inject => :song_representer) do
|
251
251
|
property :song, :pass_options => true,
|
252
|
-
:extend => song_representer, :instance => lambda { |
|
252
|
+
:extend => song_representer, :instance => lambda { |options| options[:binding].get(represented: options[:represented]) }
|
253
253
|
end
|
254
254
|
|
255
255
|
it "uses Binding#get instead of creating an instance, but deprecates" do
|