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/definition_test.rb
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class DefinitionTest < Minitest::Spec
|
|
4
|
-
Definition = Representable::Definition
|
|
5
|
-
|
|
6
|
-
# TODO: test that we DON'T clone options, that must happen in
|
|
7
|
-
describe "#initialize" do
|
|
8
|
-
it do
|
|
9
|
-
# new yields the defaultized options HASH.
|
|
10
|
-
definition = Definition.new(:song, :extend => Module) do |options|
|
|
11
|
-
options[:awesome] = true
|
|
12
|
-
options[:parse_filter] << 1
|
|
13
|
-
|
|
14
|
-
# default variables
|
|
15
|
-
_(options[:as]).must_be_nil
|
|
16
|
-
_(options[:extend]).must_equal Module
|
|
17
|
-
end
|
|
18
|
-
_(definition.name).must_equal "song"
|
|
19
|
-
|
|
20
|
-
_(definition[:awesome]).must_equal true
|
|
21
|
-
_(definition[:parse_filter]).must_equal Representable::Pipeline[1]
|
|
22
|
-
_(definition[:render_filter]).must_equal Representable::Pipeline[]
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
describe "#[]" do
|
|
27
|
-
let(:definition) { Definition.new(:song) }
|
|
28
|
-
# default is nil.
|
|
29
|
-
it { _(definition[:bla]).must_be_nil }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# merge!
|
|
33
|
-
describe "#merge!" do
|
|
34
|
-
let(:definition) { Definition.new(:song, :whatever => true) }
|
|
35
|
-
|
|
36
|
-
# merges new options.
|
|
37
|
-
it { _(definition.merge!(:something => true)[:something]).must_equal true }
|
|
38
|
-
# doesn't override original options.
|
|
39
|
-
it { _(definition.merge!({:something => true})[:whatever]).must_equal true }
|
|
40
|
-
# override original when passed in #merge!.
|
|
41
|
-
it { _(definition.merge!({:whatever => false})[:whatever]).must_equal false }
|
|
42
|
-
|
|
43
|
-
# with block
|
|
44
|
-
it do
|
|
45
|
-
definition = Definition.new(:song, :extend => Module).merge!({:something => true}) do |options|
|
|
46
|
-
options[:awesome] = true
|
|
47
|
-
options[:render_filter] << 1
|
|
48
|
-
|
|
49
|
-
# default variables
|
|
50
|
-
# options[:as].must_equal "song"
|
|
51
|
-
# options[:extend].must_equal Module
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
_(definition[:awesome]).must_equal true
|
|
55
|
-
_(definition[:something]).must_equal true
|
|
56
|
-
_(definition[:render_filter]).must_equal Representable::Pipeline[1]
|
|
57
|
-
_(definition[:parse_filter]).must_equal Representable::Pipeline[]
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
describe "with :parse_filter" do
|
|
61
|
-
let(:definition) { Definition.new(:title, :parse_filter => 1) }
|
|
62
|
-
|
|
63
|
-
# merges :parse_filter and :render_filter.
|
|
64
|
-
it do
|
|
65
|
-
merged = definition.merge!(:parse_filter => 2)[:parse_filter]
|
|
66
|
-
|
|
67
|
-
_(merged).must_be_kind_of Representable::Pipeline
|
|
68
|
-
_(merged.size).must_equal 2
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# :parse_filter can also be array.
|
|
72
|
-
it { _(definition.merge!(:parse_filter => [2, 3])[:parse_filter].size).must_equal 3 }
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# does not change arguments
|
|
76
|
-
it do
|
|
77
|
-
Definition.new(:title).merge!(options = {:whatever => 1})
|
|
78
|
-
_(options).must_equal(:whatever => 1)
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
# delete!
|
|
83
|
-
describe "#delete!" do
|
|
84
|
-
let(:definition) { Definition.new(:song, serialize: "remove me!") }
|
|
85
|
-
|
|
86
|
-
before { _(definition[:serialize].(nil)).must_equal "remove me!" }
|
|
87
|
-
|
|
88
|
-
it { _(definition.delete!(:serialize)[:serialize]).must_be_nil }
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
# #inspect
|
|
92
|
-
describe "#inspect" do
|
|
93
|
-
it {
|
|
94
|
-
_(Definition.new(:songs).inspect).must_equal "#<Representable::Definition ==>songs @options=#{{name: "songs", parse_filter: [], render_filter: []}.inspect}>"
|
|
95
|
-
}
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
describe "generic API" do
|
|
99
|
-
before do
|
|
100
|
-
@def = Representable::Definition.new(:songs)
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
it "responds to #representer_module" do
|
|
104
|
-
assert_nil Representable::Definition.new(:song).representer_module
|
|
105
|
-
assert_equal Hash, Representable::Definition.new(:song, :extend => Hash).representer_module
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
describe "#typed?" do
|
|
109
|
-
it "is false per default" do
|
|
110
|
-
refute @def.typed?
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
it "is true when :class is present" do
|
|
114
|
-
assert Representable::Definition.new(:songs, :class => Hash).typed?
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
it "is true when :extend is present, only" do
|
|
118
|
-
assert Representable::Definition.new(:songs, :extend => Hash).typed?
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it "is true when :instance is present, only" do
|
|
122
|
-
assert Representable::Definition.new(:songs, :instance => Object.new).typed?
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
describe "#representable?" do
|
|
127
|
-
it { assert Definition.new(:song, :representable => true).representable? }
|
|
128
|
-
it { _(Definition.new(:song, :representable => true, :extend => Object).representable?).must_equal true }
|
|
129
|
-
it { refute Definition.new(:song, :representable => false, :extend => Object).representable? }
|
|
130
|
-
it { assert Definition.new(:song, :extend => Object).representable? }
|
|
131
|
-
it { refute Definition.new(:song).representable? }
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
it "responds to #getter and returns string" do
|
|
135
|
-
assert_equal "songs", @def.getter
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
it "responds to #name" do
|
|
139
|
-
assert_equal "songs", @def.name
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
it "responds to #setter" do
|
|
143
|
-
assert_equal :"songs=", @def.setter
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
describe "nested: FIXME" do
|
|
147
|
-
it do
|
|
148
|
-
dfn = Representable::Definition.new(:songs, nested: Module)
|
|
149
|
-
assert dfn.typed?
|
|
150
|
-
_(dfn[:extend].(nil)).must_equal Module
|
|
151
|
-
end
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
describe "#clone" do
|
|
155
|
-
subject { Representable::Definition.new(:title, :volume => 9, :clonable => ::Representable::Option(1)) }
|
|
156
|
-
|
|
157
|
-
it { _(subject.clone).must_be_kind_of Representable::Definition }
|
|
158
|
-
it { _(subject.clone[:clonable].(nil)).must_equal 1 }
|
|
159
|
-
|
|
160
|
-
it "clones @options" do
|
|
161
|
-
@def.merge!(:volume => 9)
|
|
162
|
-
|
|
163
|
-
cloned = @def.clone
|
|
164
|
-
cloned.merge!(:volume => 8)
|
|
165
|
-
|
|
166
|
-
assert_equal @def[:volume], 9
|
|
167
|
-
assert_equal cloned[:volume], 8
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
describe "#has_default?" do
|
|
173
|
-
it "returns false if no :default set" do
|
|
174
|
-
refute Representable::Definition.new(:song).has_default?
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
it "returns true if :default set" do
|
|
178
|
-
assert Representable::Definition.new(:song, :default => nil).has_default?
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
describe "#binding" do
|
|
183
|
-
it "returns true when :binding is set" do
|
|
184
|
-
assert Representable::Definition.new(:songs, :binding => Object)[:binding]
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
it "returns false when :binding is not set" do
|
|
188
|
-
refute Representable::Definition.new(:songs)[:binding]
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
describe "#create_binding" do
|
|
193
|
-
it "executes the block (without special context)" do
|
|
194
|
-
definition = Representable::Definition.new(
|
|
195
|
-
:title, :binding => ->(*args) {
|
|
196
|
-
@binding = Representable::Binding.new(*args)
|
|
197
|
-
}
|
|
198
|
-
)
|
|
199
|
-
_(definition.create_binding).must_equal @binding
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
describe ":collection => true" do
|
|
204
|
-
before do
|
|
205
|
-
@def = Representable::Definition.new(:songs, :collection => true, :tag => :song)
|
|
206
|
-
end
|
|
207
|
-
|
|
208
|
-
it "responds to #array?" do
|
|
209
|
-
assert @def.array?
|
|
210
|
-
end
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
describe ":default => value" do
|
|
214
|
-
it "responds to #default" do
|
|
215
|
-
@def = Representable::Definition.new(:song)
|
|
216
|
-
assert_nil @def[:default]
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
it "accepts a default value" do
|
|
220
|
-
@def = Representable::Definition.new(:song, :default => "Atheist Peace")
|
|
221
|
-
assert_equal "Atheist Peace", @def[:default]
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
|
|
225
|
-
describe ":hash => true" do
|
|
226
|
-
before do
|
|
227
|
-
@def = Representable::Definition.new(:songs, :hash => true)
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
it "responds to #hash?" do
|
|
231
|
-
assert @def.hash?
|
|
232
|
-
refute Representable::Definition.new(:songs).hash?
|
|
233
|
-
end
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
describe ":binding => Object" do
|
|
237
|
-
subject do
|
|
238
|
-
Representable::Definition.new(:songs, :binding => Object)
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
it "responds to #binding" do
|
|
242
|
-
assert_equal subject[:binding], Object
|
|
243
|
-
end
|
|
244
|
-
end
|
|
245
|
-
|
|
246
|
-
describe "#[]=" do
|
|
247
|
-
it "raises exception since it's deprecated" do
|
|
248
|
-
assert_raises NoMethodError do
|
|
249
|
-
Definition.new(:title)[:extend] = Module.new # use merge! after initialize.
|
|
250
|
-
end
|
|
251
|
-
end
|
|
252
|
-
end
|
|
253
|
-
end
|
data/test/examples/example.rb
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
require "bundler"
|
|
2
|
-
Bundler.setup
|
|
3
|
-
|
|
4
|
-
require "representable/yaml"
|
|
5
|
-
require "ostruct"
|
|
6
|
-
|
|
7
|
-
def reset_representer(*module_name)
|
|
8
|
-
module_name.each do |mod|
|
|
9
|
-
mod.module_eval do
|
|
10
|
-
@representable_attrs = nil
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
class Song < OpenStruct
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
song = Song.new(:title => "Fallout", :track => 1)
|
|
19
|
-
|
|
20
|
-
module SongRepresenter
|
|
21
|
-
include Representable::JSON
|
|
22
|
-
|
|
23
|
-
property :title
|
|
24
|
-
property :track
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
puts song.extend(SongRepresenter).to_json
|
|
28
|
-
|
|
29
|
-
rox = Song.new.extend(SongRepresenter).from_json(%{ {"title":"Roxanne"} })
|
|
30
|
-
puts rox.inspect
|
|
31
|
-
|
|
32
|
-
module SongRepresenter
|
|
33
|
-
include Representable::JSON
|
|
34
|
-
|
|
35
|
-
self.representation_wrap = :hit
|
|
36
|
-
|
|
37
|
-
property :title
|
|
38
|
-
property :track
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
puts song.extend(SongRepresenter).to_json
|
|
42
|
-
|
|
43
|
-
######### collections
|
|
44
|
-
|
|
45
|
-
reset_representer(SongRepresenter)
|
|
46
|
-
|
|
47
|
-
module SongRepresenter
|
|
48
|
-
include Representable::JSON
|
|
49
|
-
|
|
50
|
-
property :title
|
|
51
|
-
property :track
|
|
52
|
-
collection :composers
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
|
|
56
|
-
puts song.extend(SongRepresenter).to_json
|
|
57
|
-
|
|
58
|
-
######### nesting types
|
|
59
|
-
|
|
60
|
-
class Album < OpenStruct
|
|
61
|
-
def name
|
|
62
|
-
puts @table.inspect
|
|
63
|
-
# @attributes
|
|
64
|
-
@table[:name]
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
module AlbumRepresenter
|
|
69
|
-
include Representable::JSON
|
|
70
|
-
|
|
71
|
-
property :name
|
|
72
|
-
property :song, :extend => SongRepresenter, :class => Song
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
album = Album.new(:name => "The Police", :song => song)
|
|
76
|
-
puts album.extend(AlbumRepresenter).to_json
|
|
77
|
-
|
|
78
|
-
reset_representer(AlbumRepresenter)
|
|
79
|
-
|
|
80
|
-
module AlbumRepresenter
|
|
81
|
-
include Representable::JSON
|
|
82
|
-
|
|
83
|
-
property :name
|
|
84
|
-
collection :songs, :extend => SongRepresenter, :class => Song
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
album = Album.new(:name => "The Police", :songs => [song, Song.new(:title => "Synchronicity")])
|
|
88
|
-
puts album.extend(AlbumRepresenter).to_json
|
|
89
|
-
|
|
90
|
-
album = Album.new.extend(AlbumRepresenter).from_json(
|
|
91
|
-
%{{"name":"Offspring","songs":[{"title":"Genocide"},{"title":"Nitro","composers":["Offspring"]}]}
|
|
92
|
-
}
|
|
93
|
-
)
|
|
94
|
-
puts album.inspect
|
|
95
|
-
|
|
96
|
-
reset_representer(SongRepresenter)
|
|
97
|
-
|
|
98
|
-
######### using helpers (customizing the rendering/parsing)
|
|
99
|
-
module AlbumRepresenter
|
|
100
|
-
include Representable::JSON
|
|
101
|
-
|
|
102
|
-
def name
|
|
103
|
-
super.upcase
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
puts Album.new(:name => "The Police")
|
|
108
|
-
.extend(AlbumRepresenter).to_json
|
|
109
|
-
|
|
110
|
-
reset_representer(SongRepresenter)
|
|
111
|
-
|
|
112
|
-
######### inheritance
|
|
113
|
-
module SongRepresenter
|
|
114
|
-
include Representable::JSON
|
|
115
|
-
|
|
116
|
-
property :title
|
|
117
|
-
property :track
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
module CoverSongRepresenter
|
|
121
|
-
include Representable::JSON
|
|
122
|
-
include SongRepresenter
|
|
123
|
-
|
|
124
|
-
property :covered_by
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
song = Song.new(:title => "Truth Hits Everybody", :covered_by => "No Use For A Name")
|
|
128
|
-
puts song.extend(CoverSongRepresenter).to_json
|
|
129
|
-
|
|
130
|
-
### XML
|
|
131
|
-
require "representable/xml"
|
|
132
|
-
module SongRepresenter
|
|
133
|
-
include Representable::XML
|
|
134
|
-
|
|
135
|
-
property :title
|
|
136
|
-
property :track
|
|
137
|
-
collection :composers
|
|
138
|
-
end
|
|
139
|
-
song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
|
|
140
|
-
puts song.extend(SongRepresenter).to_xml
|
|
141
|
-
|
|
142
|
-
reset_representer(SongRepresenter)
|
|
143
|
-
|
|
144
|
-
### YAML
|
|
145
|
-
require "representable/yaml"
|
|
146
|
-
module SongRepresenter
|
|
147
|
-
include Representable::YAML
|
|
148
|
-
|
|
149
|
-
property :title
|
|
150
|
-
property :track
|
|
151
|
-
collection :composers
|
|
152
|
-
end
|
|
153
|
-
puts song.extend(SongRepresenter).to_yaml
|
|
154
|
-
|
|
155
|
-
SongRepresenter.module_eval do
|
|
156
|
-
@representable_attrs = nil
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
### YAML
|
|
160
|
-
module SongRepresenter
|
|
161
|
-
include Representable::YAML
|
|
162
|
-
|
|
163
|
-
property :title
|
|
164
|
-
property :track
|
|
165
|
-
collection :composers, :style => :flow
|
|
166
|
-
end
|
|
167
|
-
puts song.extend(SongRepresenter).to_yaml
|
|
168
|
-
|
|
169
|
-
SongRepresenter.module_eval do
|
|
170
|
-
@representable_attrs = nil
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
# R/W support
|
|
174
|
-
song = Song.new(:title => "You're Wrong", :track => 4)
|
|
175
|
-
module SongRepresenter
|
|
176
|
-
include Representable::Hash
|
|
177
|
-
|
|
178
|
-
property :title, :readable => false
|
|
179
|
-
property :track
|
|
180
|
-
end
|
|
181
|
-
puts song.extend(SongRepresenter).to_hash
|
|
182
|
-
|
|
183
|
-
SongRepresenter.module_eval do
|
|
184
|
-
@representable_attrs = nil
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
module SongRepresenter
|
|
188
|
-
include Representable::Hash
|
|
189
|
-
|
|
190
|
-
property :title, :writeable => false
|
|
191
|
-
property :track
|
|
192
|
-
end
|
|
193
|
-
song = Song.new.extend(SongRepresenter)
|
|
194
|
-
song.from_hash({:title => "Fallout", :track => 1})
|
|
195
|
-
puts song
|
|
196
|
-
|
|
197
|
-
######### custom methods in representer (using helpers)
|
|
198
|
-
######### conditions
|
|
199
|
-
######### :as
|
|
200
|
-
######### XML::Collection
|
|
201
|
-
######### polymorphic :extend and :class, instance context!, :instance
|
|
202
|
-
class CoverSong < Song
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
songs = [Song.new(title: "Weirdo", track: 5), CoverSong.new(title: "Truth Hits Everybody", track: 6, copyright: "The Police")]
|
|
206
|
-
album = Album.new(name: "Incognito", songs: songs)
|
|
207
|
-
|
|
208
|
-
reset_representer(SongRepresenter, AlbumRepresenter)
|
|
209
|
-
|
|
210
|
-
module SongRepresenter
|
|
211
|
-
include Representable::Hash
|
|
212
|
-
|
|
213
|
-
property :title
|
|
214
|
-
property :track
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
module CoverSongRepresenter
|
|
218
|
-
include Representable::Hash
|
|
219
|
-
include SongRepresenter
|
|
220
|
-
|
|
221
|
-
property :copyright
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
module AlbumRepresenter
|
|
225
|
-
include Representable::Hash
|
|
226
|
-
|
|
227
|
-
property :name
|
|
228
|
-
collection :songs, :extend => ->(song) { song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter }
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
puts album.extend(AlbumRepresenter).to_hash
|
|
232
|
-
|
|
233
|
-
reset_representer(AlbumRepresenter)
|
|
234
|
-
|
|
235
|
-
module AlbumRepresenter
|
|
236
|
-
include Representable::Hash
|
|
237
|
-
|
|
238
|
-
property :name
|
|
239
|
-
collection :songs,
|
|
240
|
-
:extend => ->(song) { song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
|
|
241
|
-
:class => ->(hsh) { hsh.key?("copyright") ? CoverSong : Song } #=> {"title"=>"Weirdo", "track"=>5}
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
album = Album.new.extend(AlbumRepresenter).from_hash(
|
|
245
|
-
{
|
|
246
|
-
"name" => "Incognito",
|
|
247
|
-
"songs" => [
|
|
248
|
-
{"title" => "Weirdo", "track" => 5},
|
|
249
|
-
{"title" => "Truth Hits Everybody", "track" => 6, "copyright" => "The Police"}
|
|
250
|
-
]
|
|
251
|
-
}
|
|
252
|
-
)
|
|
253
|
-
puts album.inspect
|
|
254
|
-
|
|
255
|
-
reset_representer(AlbumRepresenter)
|
|
256
|
-
|
|
257
|
-
module AlbumRepresenter
|
|
258
|
-
include Representable::Hash
|
|
259
|
-
|
|
260
|
-
property :name
|
|
261
|
-
collection :songs,
|
|
262
|
-
:extend => ->(song) { song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
|
|
263
|
-
:instance => ->(hsh) { hsh.key?("copyright") ? CoverSong.new : Song.new(original: true) }
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
album = Album.new.extend(AlbumRepresenter).from_hash(
|
|
267
|
-
{
|
|
268
|
-
"name" => "Incognito",
|
|
269
|
-
"songs" => [
|
|
270
|
-
{"title" => "Weirdo", "track" => 5},
|
|
271
|
-
{"title" => "Truth Hits Everybody", "track" => 6, "copyright" => "The Police"}
|
|
272
|
-
]
|
|
273
|
-
}
|
|
274
|
-
)
|
|
275
|
-
puts album.inspect
|
|
276
|
-
|
|
277
|
-
######### #hash
|
|
278
|
-
|
|
279
|
-
reset_representer(SongRepresenter)
|
|
280
|
-
|
|
281
|
-
module SongRepresenter
|
|
282
|
-
include Representable::JSON
|
|
283
|
-
|
|
284
|
-
property :title
|
|
285
|
-
hash :ratings
|
|
286
|
-
end
|
|
287
|
-
|
|
288
|
-
puts Song.new(title: "Bliss", ratings: {"Rolling Stone" => 4.9, "FryZine" => 4.5}).extend(SongRepresenter).to_json
|
|
289
|
-
|
|
290
|
-
######### JSON::Hash
|
|
291
|
-
|
|
292
|
-
module FavoriteSongsRepresenter
|
|
293
|
-
include Representable::JSON::Hash
|
|
294
|
-
end
|
|
295
|
-
|
|
296
|
-
puts({"Nick" => "Hyper Music", "El" => "Blown In The Wind"}.extend(FavoriteSongsRepresenter).to_json)
|
|
297
|
-
|
|
298
|
-
require "representable/json/hash"
|
|
299
|
-
module FavoriteSongsRepresenter
|
|
300
|
-
include Representable::JSON::Hash
|
|
301
|
-
|
|
302
|
-
values extend: SongRepresenter, class: Song
|
|
303
|
-
end
|
|
304
|
-
|
|
305
|
-
puts({"Nick" => Song.new(title: "Hyper Music")}.extend(FavoriteSongsRepresenter).to_json)
|
|
306
|
-
|
|
307
|
-
require "representable/json/collection"
|
|
308
|
-
module SongsRepresenter
|
|
309
|
-
include Representable::JSON::Collection
|
|
310
|
-
|
|
311
|
-
items extend: SongRepresenter, class: Song
|
|
312
|
-
end
|
|
313
|
-
|
|
314
|
-
puts [Song.new(title: "Hyper Music"), Song.new(title: "Screenager")].extend(SongsRepresenter).to_json
|
data/test/examples/object.rb
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
require "ostruct"
|
|
4
|
-
require "pp"
|
|
5
|
-
|
|
6
|
-
source = OpenStruct.new(
|
|
7
|
-
name: "30 Years Live", songs: [
|
|
8
|
-
OpenStruct.new(id: 1, title: "Dear Beloved"), OpenStruct.new(id: 2, title: "Fuck Armageddon")
|
|
9
|
-
]
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
require "representable/object"
|
|
13
|
-
|
|
14
|
-
class AlbumRepresenter < Representable::Decorator
|
|
15
|
-
include Representable::Object
|
|
16
|
-
|
|
17
|
-
property :name
|
|
18
|
-
collection :songs, instance: ->(_fragment, *) { Song.new } do
|
|
19
|
-
property :title
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
Album = Struct.new(:name, :songs)
|
|
24
|
-
Song = Struct.new(:title)
|
|
25
|
-
|
|
26
|
-
target = Album.new
|
|
27
|
-
|
|
28
|
-
AlbumRepresenter.new(target).from_object(source)
|
data/test/exec_context_test.rb
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ExecContextTest < Minitest::Spec
|
|
4
|
-
for_formats(
|
|
5
|
-
:hash => [Representable::Hash, {Song => "Rebel Fate"}, {Song=>"Timing"}]
|
|
6
|
-
# :xml => [Representable::XML, "<open_struct>\n <song>\n <name>Alive</name>\n </song>\n</open_struct>", "<open_struct><song><name>You've Taken Everything</name></song>/open_struct>"],
|
|
7
|
-
# :yaml => [Representable::YAML, "---\nsong:\n name: Alive\n", "---\nsong:\n name: You've Taken Everything\n"],
|
|
8
|
-
) do |format, mod, input, output|
|
|
9
|
-
let(:song) { representer.prepare(Song.new("Timing")) }
|
|
10
|
-
let(:format) { format }
|
|
11
|
-
|
|
12
|
-
describe "exec_context: nil" do
|
|
13
|
-
representer!(:module => mod) do
|
|
14
|
-
property :name, :as => ->(*) { self.class }
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it { render(song).must_equal_document output }
|
|
18
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "exec_context: :decorator" do
|
|
22
|
-
representer!(:module => mod) do
|
|
23
|
-
property :name, :as => ->(*) { self.class }, :exec_context => :decorator
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it { render(song).must_equal_document output }
|
|
27
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe "exec_context: :binding" do
|
|
31
|
-
representer!(:module => mod) do
|
|
32
|
-
property :name,
|
|
33
|
-
:as => ->(*) { self.class }, # to actually test
|
|
34
|
-
:exec_context => :binding,
|
|
35
|
-
:setter => ->(options) {
|
|
36
|
-
options[:represented].name = options[:fragment] # to make parsing work.
|
|
37
|
-
}
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
|
41
|
-
it { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe "Decorator" do
|
|
45
|
-
# DISCUSS: do we need this test?
|
|
46
|
-
describe "exec_context: nil" do
|
|
47
|
-
representer!(:module => mod, :decorator => true) do
|
|
48
|
-
property :name, :as => ->(*) { self.class }
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it { render(song).must_equal_document output }
|
|
52
|
-
it { _(parse(song, input).name).must_equal "Rebel Fate" }
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
describe "exec_context: :decorator" do # this tests if lambdas are run in the right context, if methods are called in the right context and if we can access the represented object.
|
|
56
|
-
representer!(:module => mod, :decorator => true) do
|
|
57
|
-
property :name, :as => ->(*) { self.class.superclass }, :exec_context => :decorator
|
|
58
|
-
|
|
59
|
-
define_method :name do # def in Decorator class.
|
|
60
|
-
"Timebomb"
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
define_method :"name=" do |v| # def in Decorator class.
|
|
64
|
-
represented.name = v
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it { render(song).must_equal_document({Representable::Decorator=>"Timebomb"}) }
|
|
69
|
-
it { _(parse(song, {Representable::Decorator=>"Listless"}).name).must_equal "Listless" }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# DISCUSS: do we need this test?
|
|
73
|
-
describe "exec_context: :binding" do
|
|
74
|
-
representer!(:module => mod, :decorator => true) do
|
|
75
|
-
property :name,
|
|
76
|
-
:as => ->(*) { self.class }, # to actually test
|
|
77
|
-
:exec_context => :binding,
|
|
78
|
-
:setter => ->(options) {
|
|
79
|
-
options[:represented].name = options[:fragment] # to make parsing work.
|
|
80
|
-
}
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it { render(song).must_equal_document({Representable::Hash::Binding => "name"}) }
|
|
84
|
-
it("xxx") { _(parse(song, {Representable::Hash::Binding => "Rebel Fate"}).name).must_equal "Rebel Fate" }
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|