representable 3.3.0 → 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 +10 -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 +3 -65
- 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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 75dcdfbd65fe81c58c582f0d9949fde3041e0adf8a9ed3fa6816e65cd29fadef
|
|
4
|
+
data.tar.gz: 1e3908699d065421404b837fd28c955b35631d21794211b9429275a1041738c9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 664102321effd8283d8d0bd19973078950270b3e3e7e64a16f591dedb60dc71a0ebe3022df9ff628f2a9d9d6f629882a4eaa41aa7ca5ce47af73fa98bf1ad4c9
|
|
7
|
+
data.tar.gz: a0da40d0eb83a9fcf39ba41f2bebdd87a6092901150c5b77a2fe44753f57a75facd612fc37fc95927426b9765de4a939d9a7b70024d85a086c4bb4b6a7e6d5ee
|
data/CHANGES.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
# 3.4.0
|
|
2
|
+
|
|
3
|
+
* Use the standard json library instead of multi_json.
|
|
4
|
+
* `to_json` no longer calls `as_json`; with ActiveSupport, format `Time` values in the representer.
|
|
5
|
+
* `from_json` takes a String, not an IO.
|
|
6
|
+
|
|
7
|
+
# 3.3.1
|
|
8
|
+
|
|
9
|
+
* Remove MultiJson deprecation.
|
|
10
|
+
|
|
1
11
|
# 3.3.0
|
|
2
12
|
|
|
3
13
|
* Drop support for Ruby < 3.0.0.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -149,13 +149,7 @@ gem 'representable'
|
|
|
149
149
|
### Dependencies
|
|
150
150
|
|
|
151
151
|
Representable does a great job with JSON, it also features support for XML, YAML and pure ruby
|
|
152
|
-
hashes.
|
|
153
|
-
|
|
154
|
-
If you want to use JSON, add the following to your Gemfile:
|
|
155
|
-
|
|
156
|
-
```ruby
|
|
157
|
-
gem 'multi_json'
|
|
158
|
-
```
|
|
152
|
+
hashes.
|
|
159
153
|
|
|
160
154
|
If you want to use XML, add the following to your Gemfile:
|
|
161
155
|
|
data/lib/representable/json.rb
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
require "multi_json"
|
|
1
|
+
require "json"
|
|
3
2
|
|
|
4
3
|
require "representable"
|
|
5
4
|
|
|
6
5
|
module Representable
|
|
7
6
|
# Brings #to_json and #from_json to your object.
|
|
8
7
|
module JSON
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
end
|
|
8
|
+
def self.generate(object)
|
|
9
|
+
::JSON.dump(object)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.parse(data)
|
|
13
|
+
return if data.nil? || data.strip.empty?
|
|
14
|
+
|
|
15
|
+
::JSON.parse(data)
|
|
19
16
|
end
|
|
20
17
|
|
|
21
18
|
autoload :Collection, "representable/json/collection"
|
|
@@ -45,13 +42,13 @@ module Representable
|
|
|
45
42
|
|
|
46
43
|
# Parses the body as JSON and delegates to #from_hash.
|
|
47
44
|
def from_json(data, *args)
|
|
48
|
-
data =
|
|
45
|
+
data = Representable::JSON.parse(data)
|
|
49
46
|
from_hash(data, *args)
|
|
50
47
|
end
|
|
51
48
|
|
|
52
49
|
# Returns a JSON string representing this object.
|
|
53
50
|
def to_json(*args)
|
|
54
|
-
|
|
51
|
+
Representable::JSON.generate to_hash(*args)
|
|
55
52
|
end
|
|
56
53
|
|
|
57
54
|
alias_method :render, :to_json
|
data/representable.gemspec
CHANGED
|
@@ -13,8 +13,7 @@ Gem::Specification.new do |spec|
|
|
|
13
13
|
spec.summary = %q{Renders and parses JSON/XML/YAML documents from and to Ruby objects. Includes plain properties, collections, nesting, coercion and more.}
|
|
14
14
|
spec.description = spec.summary
|
|
15
15
|
|
|
16
|
-
spec.files = `git ls-files`.split("\n")
|
|
17
|
-
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
16
|
+
spec.files = `git ls-files`.split("\n").reject { |f| f.start_with?("test/", "spec/", "features/") }
|
|
18
17
|
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
19
18
|
spec.require_paths = ["lib"]
|
|
20
19
|
spec.license = "MIT"
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: representable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nick Sutterer
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: uber
|
|
@@ -170,70 +169,10 @@ files:
|
|
|
170
169
|
- lib/representable/yaml.rb
|
|
171
170
|
- lib/representable/yaml/binding.rb
|
|
172
171
|
- representable.gemspec
|
|
173
|
-
- test/as_test.rb
|
|
174
|
-
- test/benchmarking.rb
|
|
175
|
-
- test/binding_test.rb
|
|
176
|
-
- test/cached_test.rb
|
|
177
|
-
- test/class_test.rb
|
|
178
|
-
- test/coercion_test.rb
|
|
179
|
-
- test/config/inherit_test.rb
|
|
180
|
-
- test/config_test.rb
|
|
181
|
-
- test/decorator_scope_test.rb
|
|
182
|
-
- test/decorator_test.rb
|
|
183
|
-
- test/default_test.rb
|
|
184
|
-
- test/defaults_options_test.rb
|
|
185
|
-
- test/definition_test.rb
|
|
186
|
-
- test/examples/example.rb
|
|
187
|
-
- test/examples/object.rb
|
|
188
|
-
- test/exec_context_test.rb
|
|
189
|
-
- test/features_test.rb
|
|
190
|
-
- test/filter_test.rb
|
|
191
|
-
- test/for_collection_test.rb
|
|
192
|
-
- test/generic_test.rb
|
|
193
|
-
- test/getter_setter_test.rb
|
|
194
|
-
- test/hash_bindings_test.rb
|
|
195
|
-
- test/hash_test.rb
|
|
196
|
-
- test/heritage_test.rb
|
|
197
|
-
- test/if_test.rb
|
|
198
|
-
- test/include_exclude_test.rb
|
|
199
|
-
- test/inherit_test.rb
|
|
200
|
-
- test/inline_test.rb
|
|
201
|
-
- test/instance_test.rb
|
|
202
|
-
- test/is_representable_test.rb
|
|
203
|
-
- test/json_test.rb
|
|
204
|
-
- test/lonely_test.rb
|
|
205
|
-
- test/nested_test.rb
|
|
206
|
-
- test/object_test.rb
|
|
207
|
-
- test/option_test.rb
|
|
208
|
-
- test/parse_pipeline_test.rb
|
|
209
|
-
- test/pipeline_test.rb
|
|
210
|
-
- test/populator_test.rb
|
|
211
|
-
- test/prepare_test.rb
|
|
212
|
-
- test/private_options_test.rb
|
|
213
|
-
- test/reader_writer_test.rb
|
|
214
|
-
- test/realistic_benchmark.rb
|
|
215
|
-
- test/render_nil_test.rb
|
|
216
|
-
- test/represent_test.rb
|
|
217
|
-
- test/representable_test.rb
|
|
218
|
-
- test/schema_test.rb
|
|
219
|
-
- test/serialize_deserialize_test.rb
|
|
220
|
-
- test/skip_test.rb
|
|
221
|
-
- test/stringify_hash_test.rb
|
|
222
|
-
- test/test_helper.rb
|
|
223
|
-
- test/test_helper_test.rb
|
|
224
|
-
- test/uncategorized_test.rb
|
|
225
|
-
- test/user_options_test.rb
|
|
226
|
-
- test/wrap_test.rb
|
|
227
|
-
- test/xml_assertions.rb
|
|
228
|
-
- test/xml_bindings_test.rb
|
|
229
|
-
- test/xml_namespace_test.rb
|
|
230
|
-
- test/xml_test.rb
|
|
231
|
-
- test/yaml_test.rb
|
|
232
172
|
homepage: https://github.com/trailblazer/representable/
|
|
233
173
|
licenses:
|
|
234
174
|
- MIT
|
|
235
175
|
metadata: {}
|
|
236
|
-
post_install_message:
|
|
237
176
|
rdoc_options: []
|
|
238
177
|
require_paths:
|
|
239
178
|
- lib
|
|
@@ -248,8 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
248
187
|
- !ruby/object:Gem::Version
|
|
249
188
|
version: '0'
|
|
250
189
|
requirements: []
|
|
251
|
-
rubygems_version:
|
|
252
|
-
signing_key:
|
|
190
|
+
rubygems_version: 4.0.17
|
|
253
191
|
specification_version: 4
|
|
254
192
|
summary: Renders and parses JSON/XML/YAML documents from and to Ruby objects. Includes
|
|
255
193
|
plain properties, collections, nesting, coercion and more.
|
data/test/as_test.rb
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class AsTest < Minitest::Spec
|
|
4
|
-
for_formats(
|
|
5
|
-
:hash => [Representable::Hash, {"title" => "Wie Es Geht"}, {"title" => "Revolution"}]
|
|
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("Revolution")) }
|
|
10
|
-
let(:format) { format }
|
|
11
|
-
|
|
12
|
-
describe "as: with :symbol" do
|
|
13
|
-
representer!(:module => mod) do
|
|
14
|
-
property :name, :as => :title
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it { render(song).must_equal_document output }
|
|
18
|
-
it { _(parse(song, input).name).must_equal "Wie Es Geht" }
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "as: with lambda" do
|
|
22
|
-
representer!(:module => mod) do
|
|
23
|
-
property :name, :as => ->(*) { self.class.to_s }
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it { render(song).must_equal_document({"Song" => "Revolution"}) }
|
|
27
|
-
it { _(parse(song, {"Song" => "Wie Es Geht"}).name).must_equal "Wie Es Geht" }
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe "lambda arguments" do
|
|
31
|
-
representer! do
|
|
32
|
-
property :name, :as => ->(options) { options[:user_options].inspect }
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it { render(song, user_options: {volume: 1}).must_equal_document({{volume: 1}.inspect => "Revolution"}) }
|
|
36
|
-
it { _(parse(song, {{volume: 1}.inspect => "Wie Es Geht"}, user_options: {volume: 1}).name).must_equal "Wie Es Geht" }
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# hash: to_hash(wrap: ) is representation_wrap
|
|
42
|
-
|
|
43
|
-
class AsXmlTest < Minitest::Spec
|
|
44
|
-
Band = Struct.new(:name, :label)
|
|
45
|
-
Album = Struct.new(:band)
|
|
46
|
-
Label = Struct.new(:name)
|
|
47
|
-
|
|
48
|
-
representer!(module: Representable::XML, decorator: true) do
|
|
49
|
-
self.representation_wrap = :album
|
|
50
|
-
property :band, as: :combo do
|
|
51
|
-
self.representation_wrap = :band
|
|
52
|
-
property :name
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it do
|
|
57
|
-
skip
|
|
58
|
-
_(representer.new(Album.new(Band.new("Offspring"))).to_xml).must_equal ""
|
|
59
|
-
end
|
|
60
|
-
end
|
data/test/benchmarking.rb
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
require "benchmark"
|
|
3
|
-
|
|
4
|
-
SONG_PROPERTIES = 1000.times.collect do |i|
|
|
5
|
-
"property_#{i}"
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
module SongRepresenter
|
|
9
|
-
include Representable::JSON
|
|
10
|
-
|
|
11
|
-
SONG_PROPERTIES.each { |p| property p }
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
class SongDecorator < Representable::Decorator
|
|
15
|
-
include Representable::JSON
|
|
16
|
-
|
|
17
|
-
SONG_PROPERTIES.each { |p| property p }
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
module AlbumRepresenter
|
|
21
|
-
include Representable::JSON
|
|
22
|
-
|
|
23
|
-
# collection :songs, extend: SongRepresenter
|
|
24
|
-
collection :songs, extend: SongDecorator
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def random_song
|
|
28
|
-
attrs = Hash[SONG_PROPERTIES.collect { |n| [n, n] }]
|
|
29
|
-
OpenStruct.new(attrs)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
times = []
|
|
33
|
-
|
|
34
|
-
3.times.each do
|
|
35
|
-
album = OpenStruct.new(songs: 100.times.collect { random_song })
|
|
36
|
-
|
|
37
|
-
times << Benchmark.measure do
|
|
38
|
-
puts "================ next!"
|
|
39
|
-
album.extend(AlbumRepresenter).to_json
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
puts times.join("")
|
|
44
|
-
|
|
45
|
-
# 100 songs, 100 attrs
|
|
46
|
-
# 0.050000 0.000000 0.050000 ( 0.093157)
|
|
47
|
-
|
|
48
|
-
## 100 songs, 1000 attrs
|
|
49
|
-
# 0.470000 0.010000 0.480000 ( 0.483708)
|
|
50
|
-
|
|
51
|
-
### without binding cache:
|
|
52
|
-
# 2.790000 0.030000 2.820000 ( 2.820190)
|
|
53
|
-
|
|
54
|
-
### with extend: on Song, with binding cache>
|
|
55
|
-
# 2.490000 0.030000 2.520000 ( 2.517433) 2.4-3.0
|
|
56
|
-
### without skip?
|
|
57
|
-
# 2.030000 0.020000 2.050000 ( 2.050796) 2.1-2.3
|
|
58
|
-
|
|
59
|
-
### without :writer
|
|
60
|
-
# 2.270000 0.010000 2.280000 ( 2.284530 1.9-2.2
|
|
61
|
-
### without :render_filter
|
|
62
|
-
# 2.020000 0.000000 2.020000 ( 2.030234) 1.5-2.0
|
|
63
|
-
# ##without default_for and skipable?
|
|
64
|
-
# 1.730000 0.010000 1.740000 ( 1.735597 1.4-1.7
|
|
65
|
-
### without :serialize
|
|
66
|
-
# 1.780000 0.010000 1.790000 ( 1.786791) 1.4-1.7
|
|
67
|
-
### using decorator
|
|
68
|
-
# 1.400000 0.030000 1.430000 ( 1.434206) 1.4-1.6
|
|
69
|
-
### with prepare AFTER representable?
|
|
70
|
-
# 1.330000 0.010000 1.340000 ( 1.335900) 1.1-1.3
|
|
71
|
-
|
|
72
|
-
# representable 2.0
|
|
73
|
-
# 3.000000 0.020000 3.020000 ( 3.013031) 2.7-3.0
|
|
74
|
-
|
|
75
|
-
# no method missing
|
|
76
|
-
# 2.280000 0.030000 2.310000 ( 2.313522) 2.2-2.5
|
|
77
|
-
# no def_delegator in Definition
|
|
78
|
-
# 2.130000 0.010000 2.140000 ( 2.136115) 1.7-2.1
|
data/test/binding_test.rb
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class BindingTest < Minitest::Spec
|
|
4
|
-
Binding = Representable::Binding
|
|
5
|
-
let(:render_nil_definition) { Representable::Definition.new(:song, :render_nil => true) }
|
|
6
|
-
|
|
7
|
-
describe "#skipable_empty_value?" do
|
|
8
|
-
let(:binding) { Binding.new(render_nil_definition) }
|
|
9
|
-
|
|
10
|
-
# don't skip when present.
|
|
11
|
-
it { _(binding.skipable_empty_value?("Disconnect, Disconnect")).must_equal false }
|
|
12
|
-
|
|
13
|
-
# don't skip when it's nil and render_nil: true
|
|
14
|
-
it { _(binding.skipable_empty_value?(nil)).must_equal false }
|
|
15
|
-
|
|
16
|
-
# skip when nil and :render_nil undefined.
|
|
17
|
-
it { _(Binding.new(Representable::Definition.new(:song)).skipable_empty_value?(nil)).must_equal true }
|
|
18
|
-
|
|
19
|
-
# don't skip when nil and :render_nil undefined.
|
|
20
|
-
it { _(Binding.new(Representable::Definition.new(:song)).skipable_empty_value?("Fatal Flu")).must_equal false }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
describe "#default_for" do
|
|
24
|
-
let(:definition) { Representable::Definition.new(:song, :default => "Insider") }
|
|
25
|
-
let(:binding) { Binding.new(definition) }
|
|
26
|
-
|
|
27
|
-
# return value when value present.
|
|
28
|
-
it { _(binding.default_for("Black And Blue")).must_equal "Black And Blue" }
|
|
29
|
-
|
|
30
|
-
# return false when value false.
|
|
31
|
-
it { _(binding.default_for(false)).must_equal false }
|
|
32
|
-
|
|
33
|
-
# return default when value nil.
|
|
34
|
-
it { _(binding.default_for(nil)).must_equal "Insider" }
|
|
35
|
-
|
|
36
|
-
# return nil when value nil and render_nil: true.
|
|
37
|
-
it { _(Binding.new(render_nil_definition).default_for(nil)).must_be_nil }
|
|
38
|
-
|
|
39
|
-
# return nil when value nil and render_nil: true, even when :default is set" do
|
|
40
|
-
it {
|
|
41
|
-
_(
|
|
42
|
-
Binding.new(
|
|
43
|
-
Representable::Definition.new(
|
|
44
|
-
:song, :render_nil => true,
|
|
45
|
-
:default => "The Quest"
|
|
46
|
-
)
|
|
47
|
-
).default_for(nil)
|
|
48
|
-
).must_be_nil
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
# return nil if no :default
|
|
52
|
-
it { _(Binding.new(Representable::Definition.new(:song)).default_for(nil)).must_be_nil }
|
|
53
|
-
end
|
|
54
|
-
end
|
data/test/cached_test.rb
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class Profiler
|
|
4
|
-
SUPPORTED_ENGINES = %w[ruby jruby].freeze
|
|
5
|
-
|
|
6
|
-
def self.available?
|
|
7
|
-
SUPPORTED_ENGINES.include?(RUBY_ENGINE)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def self.profile(&block)
|
|
11
|
-
case RUBY_ENGINE
|
|
12
|
-
when "ruby"
|
|
13
|
-
require "ruby-prof"
|
|
14
|
-
|
|
15
|
-
output = StringIO.new
|
|
16
|
-
profile_result = RubyProf::Profile.profile(&block)
|
|
17
|
-
printer = RubyProf::FlatPrinter.new(profile_result)
|
|
18
|
-
printer.print(output)
|
|
19
|
-
output.string
|
|
20
|
-
when "jruby"
|
|
21
|
-
require "jruby/profiler"
|
|
22
|
-
|
|
23
|
-
output_stream = java.io.ByteArrayOutputStream.new
|
|
24
|
-
print_stream = java.io.PrintStream.new(output_stream)
|
|
25
|
-
profile_result = JRuby::Profiler.profile(&block)
|
|
26
|
-
printer = JRuby::Profiler::FlatProfilePrinter.new(profile_result)
|
|
27
|
-
printer.printProfile(print_stream)
|
|
28
|
-
output_stream.toString
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
class CachedTest < Minitest::Spec
|
|
34
|
-
# TODO: also test with feature(Cached)
|
|
35
|
-
|
|
36
|
-
module Model
|
|
37
|
-
Song = Struct.new(:title, :composer)
|
|
38
|
-
Album = Struct.new(:name, :songs, :artist)
|
|
39
|
-
Artist = Struct.new(:name, :hidden_taste)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
class SongRepresenter < Representable::Decorator
|
|
43
|
-
include Representable::Hash
|
|
44
|
-
feature Representable::Cached
|
|
45
|
-
|
|
46
|
-
property :title, render_filter: ->(input, options) { "#{input}:#{options[:options][:user_options]}" }
|
|
47
|
-
property :composer, class: Model::Artist do
|
|
48
|
-
property :name
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
class AlbumRepresenter < Representable::Decorator
|
|
53
|
-
include Representable::Hash
|
|
54
|
-
include Representable::Cached
|
|
55
|
-
|
|
56
|
-
property :name
|
|
57
|
-
collection :songs, decorator: SongRepresenter, class: Model::Song
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
describe "serialization" do
|
|
61
|
-
let(:album_hash) do
|
|
62
|
-
{
|
|
63
|
-
"name" => "Louder And Even More Dangerous",
|
|
64
|
-
"songs" => [{"title" => "Southbound:#{{volume: 10}.inspect}"}, {"title" => "Jailbreak:#{{volume: 10}.inspect}"}]
|
|
65
|
-
}
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
let(:song) { Model::Song.new("Jailbreak") }
|
|
69
|
-
let(:song2) { Model::Song.new("Southbound") }
|
|
70
|
-
let(:album) { Model::Album.new("Live And Dangerous", [song, song2, Model::Song.new("Emerald")]) }
|
|
71
|
-
let(:representer) { AlbumRepresenter.new(album) }
|
|
72
|
-
|
|
73
|
-
it do
|
|
74
|
-
# album2 = Model::Album.new("Louder And Even More Dangerous", [song2, song])
|
|
75
|
-
|
|
76
|
-
# makes sure options are passed correctly.
|
|
77
|
-
_(representer.to_hash(user_options: {volume: 9})).must_equal(
|
|
78
|
-
{
|
|
79
|
-
"name" => "Live And Dangerous",
|
|
80
|
-
"songs" => [
|
|
81
|
-
{"title" => "Jailbreak:#{{volume: 9}.inspect}"}, {"title" => "Southbound:#{{volume: 9}.inspect}"},
|
|
82
|
-
{"title" => "Emerald:#{{volume: 9}.inspect}"}
|
|
83
|
-
]
|
|
84
|
-
}
|
|
85
|
-
) # called in Deserializer/Serializer
|
|
86
|
-
|
|
87
|
-
# representer becomes reusable as it is stateless.
|
|
88
|
-
# representer.update!(album2)
|
|
89
|
-
|
|
90
|
-
# makes sure options are passed correctly.
|
|
91
|
-
# representer.to_hash(volume:10).must_equal(album_hash)
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# profiling
|
|
95
|
-
it do
|
|
96
|
-
skip "no profiler available for #{RUBY_ENGINE}" unless Profiler.available?
|
|
97
|
-
|
|
98
|
-
representer.to_hash
|
|
99
|
-
|
|
100
|
-
data = Profiler.profile { representer.to_hash }
|
|
101
|
-
|
|
102
|
-
# 3 songs get decorated.
|
|
103
|
-
_(data).must_match(/3\s*Representable::Function::Decorate#call/m)
|
|
104
|
-
# These weird Regexp bellow are a quick workaround to accomodate
|
|
105
|
-
# the different profiler result formats.
|
|
106
|
-
# - "3 <Class::Representable::Decorator>#prepare" -> At MRI Ruby
|
|
107
|
-
# - "3 Representable::Decorator.prepare" -> At JRuby
|
|
108
|
-
|
|
109
|
-
# 3 nested decorator is instantiated for 3 Songs, though.
|
|
110
|
-
_(data).must_match(/3\s*(<Class::)?Representable::Decorator>?[\#.]prepare/m)
|
|
111
|
-
# no Binding is instantiated at runtime.
|
|
112
|
-
_(data).wont_match "Representable::Binding#initialize"
|
|
113
|
-
# 2 mappers for Album, Song
|
|
114
|
-
# data.must_match "2 Representable::Mapper::Methods#initialize"
|
|
115
|
-
# title, songs, 3x title, composer
|
|
116
|
-
_(data).must_match(/8\s*Representable::Binding[#.]render_pipeline/m)
|
|
117
|
-
_(data).wont_match "render_functions"
|
|
118
|
-
_(data).wont_match "Representable::Binding::Factories#render_functions"
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
describe "deserialization" do
|
|
123
|
-
let(:album_hash) do
|
|
124
|
-
{
|
|
125
|
-
"name" => "Louder And Even More Dangerous",
|
|
126
|
-
"songs" => [
|
|
127
|
-
{"title" => "Southbound", "composer" => {"name"=>"Lynott"}},
|
|
128
|
-
{"title" => "Jailbreak", "composer" => {"name"=>"Phil Lynott"}},
|
|
129
|
-
{"title"=>"Emerald"}
|
|
130
|
-
]
|
|
131
|
-
}
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
it do
|
|
135
|
-
album = Model::Album.new
|
|
136
|
-
|
|
137
|
-
AlbumRepresenter.new(album).from_hash(album_hash)
|
|
138
|
-
|
|
139
|
-
_(album.songs.size).must_equal 3
|
|
140
|
-
_(album.name).must_equal "Louder And Even More Dangerous"
|
|
141
|
-
_(album.songs[0].title).must_equal "Southbound"
|
|
142
|
-
_(album.songs[0].composer.name).must_equal "Lynott"
|
|
143
|
-
_(album.songs[1].title).must_equal "Jailbreak"
|
|
144
|
-
_(album.songs[1].composer.name).must_equal "Phil Lynott"
|
|
145
|
-
_(album.songs[2].title).must_equal "Emerald"
|
|
146
|
-
_(album.songs[2].composer).must_be_nil
|
|
147
|
-
|
|
148
|
-
# TODO: test options.
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
it "xxx" do
|
|
152
|
-
skip "no profiler available for #{RUBY_ENGINE}" unless Profiler.available?
|
|
153
|
-
|
|
154
|
-
representer = AlbumRepresenter.new(Model::Album.new)
|
|
155
|
-
representer.from_hash(album_hash)
|
|
156
|
-
|
|
157
|
-
data = Profiler.profile { representer.from_hash(album_hash) }
|
|
158
|
-
|
|
159
|
-
# only 2 nested decorators are instantiated, Song, and Artist.
|
|
160
|
-
# Didn't like the regexp?
|
|
161
|
-
# MRI and JRuby has different output formats. See note above.
|
|
162
|
-
_(data).must_match(/5\s*(<Class::)?Representable::Decorator>?[#.]prepare/)
|
|
163
|
-
# a total of 5 properties in the object graph.
|
|
164
|
-
_(data).wont_match "Representable::Binding#initialize"
|
|
165
|
-
|
|
166
|
-
_(data).wont_match "parse_functions" # no pipeline creation.
|
|
167
|
-
_(data).must_match(/10\s*Representable::Binding[#.]parse_pipeline/)
|
|
168
|
-
# three mappers for Album, Song, composer
|
|
169
|
-
# data.must_match "3 Representable::Mapper::Methods#initialize"
|
|
170
|
-
# # 6 deserializers as the songs collection uses 2.
|
|
171
|
-
# data.must_match "6 Representable::Deserializer#initialize"
|
|
172
|
-
# # one populater for every property.
|
|
173
|
-
# data.must_match "5 Representable::Populator#initialize"
|
|
174
|
-
# printer.print(STDOUT)
|
|
175
|
-
end
|
|
176
|
-
end
|
|
177
|
-
end
|