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/prepare_test.rb
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class PrepareTest < BaseTest
|
|
4
|
-
class PreparerClass
|
|
5
|
-
def initialize(object)
|
|
6
|
-
@object = object
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def ==(other)
|
|
10
|
-
return unless other.instance_of?(PreparerClass)
|
|
11
|
-
|
|
12
|
-
object == other.object
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
attr_reader :object
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
describe "#to_hash" do # TODO: introduce :representable option?
|
|
19
|
-
representer! do
|
|
20
|
-
property :song,
|
|
21
|
-
:prepare => ->(options) { options[:binding][:arbitrary].new(options[:input]) },
|
|
22
|
-
:arbitrary => PreparerClass,
|
|
23
|
-
:extend => true,
|
|
24
|
-
:representable => false # don't call #to_hash.
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
let(:hit) { Struct.new(:song).new(song).extend(representer) }
|
|
28
|
-
|
|
29
|
-
it "calls prepare:, nothing else" do
|
|
30
|
-
# render(hit).must_equal_document(output)
|
|
31
|
-
_(hit.to_hash).must_equal({"song" => PreparerClass.new(song)})
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# it "calls #from_hash on the existing song instance, nothing else" do
|
|
35
|
-
# song_id = hit.song.object_id
|
|
36
|
-
|
|
37
|
-
# parse(hit, input)
|
|
38
|
-
|
|
39
|
-
# hit.song.title.must_equal "Suffer"
|
|
40
|
-
# hit.song.object_id.must_equal song_id
|
|
41
|
-
# end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe "#from_hash" do
|
|
45
|
-
representer! do
|
|
46
|
-
property :song,
|
|
47
|
-
:prepare => ->(options) { options[:binding][:arbitrary].new(options[:input]) },
|
|
48
|
-
:arbitrary => PreparerClass,
|
|
49
|
-
# :extend => true, # TODO: typed: true would be better.
|
|
50
|
-
:instance => String.new, # pass_fragment
|
|
51
|
-
:pass_options => true,
|
|
52
|
-
:representable => false # don't call #to_hash.
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
let(:hit) { Struct.new(:song).new.extend(representer) }
|
|
56
|
-
|
|
57
|
-
it "calls prepare:, nothing else" do
|
|
58
|
-
# render(hit).must_equal_document(output)
|
|
59
|
-
hit.from_hash("song" => {})
|
|
60
|
-
|
|
61
|
-
_(hit.song).must_equal(PreparerClass.new(String.new))
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
# TODO: move me to separate file.
|
|
4
|
-
class PrivateOptionsTest < Minitest::Spec
|
|
5
|
-
representer!(decorator: true) do
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
options = {exclude: "name"}
|
|
9
|
-
|
|
10
|
-
it "render: doesn't modify options" do
|
|
11
|
-
representer.new(nil).to_hash(options)
|
|
12
|
-
_(options).must_equal({exclude: "name"})
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "parse: doesn't modify options" do
|
|
16
|
-
representer.new(nil).from_hash(options)
|
|
17
|
-
_(options).must_equal({exclude: "name"})
|
|
18
|
-
end
|
|
19
|
-
end
|
data/test/reader_writer_test.rb
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class ReaderWriterTest < BaseTest
|
|
4
|
-
representer! do
|
|
5
|
-
property :name,
|
|
6
|
-
:writer => ->(options) { options[:doc]["title"] = "#{options[:user_options][:nr]}) #{options[:input]}" },
|
|
7
|
-
:reader => ->(options) { self.name = options[:doc]["title"].split(") ").last }
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
subject { OpenStruct.new(:name => "Disorder And Disarray").extend(representer) }
|
|
11
|
-
|
|
12
|
-
it "uses :writer when rendering" do
|
|
13
|
-
_(subject.to_hash(user_options: {nr: 14})).must_equal({"title" => "14) Disorder And Disarray"})
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it "uses :reader when parsing" do
|
|
17
|
-
_(subject.from_hash({"title" => "15) The Wars End"}).name).must_equal "The Wars End"
|
|
18
|
-
end
|
|
19
|
-
end
|
data/test/realistic_benchmark.rb
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
require "benchmark"
|
|
3
|
-
|
|
4
|
-
SONG_PROPERTIES = 50.times.collect do |i|
|
|
5
|
-
"song_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 NestedProperty < Representable::Decorator
|
|
15
|
-
include Representable::JSON
|
|
16
|
-
|
|
17
|
-
SONG_PROPERTIES.each { |p| property p }
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
class SongDecorator < Representable::Decorator
|
|
21
|
-
include Representable::JSON
|
|
22
|
-
|
|
23
|
-
SONG_PROPERTIES.each { |p| property p, extend: NestedProperty }
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
class AlbumRepresenter < Representable::Decorator
|
|
27
|
-
include Representable::JSON
|
|
28
|
-
|
|
29
|
-
# collection :songs, extend: SongRepresenter
|
|
30
|
-
collection :songs, extend: SongDecorator
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
Song = Struct.new(*SONG_PROPERTIES.map(&:to_sym))
|
|
34
|
-
Album = Struct.new(:songs)
|
|
35
|
-
|
|
36
|
-
def random_song
|
|
37
|
-
Song.new(*SONG_PROPERTIES.collect { |_p| Song.new(*SONG_PROPERTIES) })
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
times = []
|
|
41
|
-
|
|
42
|
-
3.times.each do
|
|
43
|
-
album = Album.new(100.times.collect { random_song })
|
|
44
|
-
|
|
45
|
-
times << Benchmark.measure do
|
|
46
|
-
puts "================ next!"
|
|
47
|
-
AlbumRepresenter.new(album).to_json
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
puts times.join("")
|
|
52
|
-
|
|
53
|
-
album = Album.new(100.times.collect { random_song })
|
|
54
|
-
require "ruby-prof"
|
|
55
|
-
RubyProf.start
|
|
56
|
-
AlbumRepresenter.new(album).to_hash
|
|
57
|
-
res = RubyProf.stop
|
|
58
|
-
printer = RubyProf::FlatPrinter.new(res)
|
|
59
|
-
printer.print(array = [])
|
|
60
|
-
|
|
61
|
-
array[0..60].each { |a| puts a }
|
|
62
|
-
|
|
63
|
-
# 100 songs, 100 attrs
|
|
64
|
-
# 0.050000 0.000000 0.050000 ( 0.093157)
|
|
65
|
-
|
|
66
|
-
## 100 songs, 1000 attrs
|
|
67
|
-
# 0.470000 0.010000 0.480000 ( 0.483708)
|
|
68
|
-
|
|
69
|
-
### without binding cache:
|
|
70
|
-
# 2.790000 0.030000 2.820000 ( 2.820190)
|
|
71
|
-
|
|
72
|
-
### with extend: on Song, with binding cache>
|
|
73
|
-
# 2.490000 0.030000 2.520000 ( 2.517433) 2.4-3.0
|
|
74
|
-
### without skip?
|
|
75
|
-
# 2.030000 0.020000 2.050000 ( 2.050796) 2.1-2.3
|
|
76
|
-
|
|
77
|
-
### without :writer
|
|
78
|
-
# 2.270000 0.010000 2.280000 ( 2.284530 1.9-2.2
|
|
79
|
-
### without :render_filter
|
|
80
|
-
# 2.020000 0.000000 2.020000 ( 2.030234) 1.5-2.0
|
|
81
|
-
# ##without default_for and skipable?
|
|
82
|
-
# 1.730000 0.010000 1.740000 ( 1.735597 1.4-1.7
|
|
83
|
-
### without :serialize
|
|
84
|
-
# 1.780000 0.010000 1.790000 ( 1.786791) 1.4-1.7
|
|
85
|
-
### using decorator
|
|
86
|
-
# 1.400000 0.030000 1.430000 ( 1.434206) 1.4-1.6
|
|
87
|
-
### with prepare AFTER representable?
|
|
88
|
-
# 1.330000 0.010000 1.340000 ( 1.335900) 1.1-1.3
|
|
89
|
-
|
|
90
|
-
# representable 2.0
|
|
91
|
-
# 3.000000 0.020000 3.020000 ( 3.013031) 2.7-3.0
|
|
92
|
-
|
|
93
|
-
# no method missing
|
|
94
|
-
# 2.280000 0.030000 2.310000 ( 2.313522) 2.2-2.5
|
|
95
|
-
# no def_delegator in Definition
|
|
96
|
-
# 2.130000 0.010000 2.140000 ( 2.136115) 1.7-2.1
|
data/test/render_nil_test.rb
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class RenderNilTest < Minitest::Spec
|
|
4
|
-
Song = Struct.new(:title)
|
|
5
|
-
|
|
6
|
-
describe "render_nil: true" do
|
|
7
|
-
representer! do
|
|
8
|
-
property :title, render_nil: true
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it { _(Song.new.extend(representer).to_hash).must_equal({"title"=>nil}) }
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
describe "with :extend it shouldn't extend nil" do
|
|
15
|
-
representer! do
|
|
16
|
-
property :title, render_nil: true, extend: Class
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it { _(Song.new.extend(representer).to_hash).must_equal({"title"=>nil}) }
|
|
20
|
-
end
|
|
21
|
-
end
|
data/test/represent_test.rb
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
require "test_helper"
|
|
2
|
-
|
|
3
|
-
class RepresentTest < Minitest::Spec
|
|
4
|
-
let(:songs) { [song, Song.new("Can't Take Them All")] }
|
|
5
|
-
let(:song) { Song.new("Days Go By") }
|
|
6
|
-
|
|
7
|
-
for_formats(
|
|
8
|
-
:hash => [Representable::Hash, out = [{"name" => "Days Go By"}, {"name"=>"Can't Take Them All"}], out]
|
|
9
|
-
# :json => [Representable::JSON, out="[{\"name\":\"Days Go By\"},{\"name\":\"Can't Take Them All\"}]", out],
|
|
10
|
-
# :xml => [Representable::XML, out="<a><song></song><song></song></a>", out]
|
|
11
|
-
) do |format, mod, output, input|
|
|
12
|
-
# Representer.represents detects collection.
|
|
13
|
-
describe "Module#to_/from_#{format}" do
|
|
14
|
-
let(:format) { format }
|
|
15
|
-
|
|
16
|
-
let(:representer) do
|
|
17
|
-
Module.new do
|
|
18
|
-
include mod
|
|
19
|
-
property :name
|
|
20
|
-
|
|
21
|
-
collection_representer :class => Song # TODOOOOOOOOOOOO: test without Song and fix THIS FUCKINGNoMethodError: undefined method `name=' for {"name"=>"Days Go By"}:Hash ERROR!!!!!!!!!!!!!!!
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it { render(representer.represent(songs)).must_equal_document output }
|
|
26
|
-
it { _(parse(representer.represent([]), input)).must_equal songs }
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Decorator.represents detects collection.
|
|
30
|
-
describe "Decorator#to_/from_#{format}" do
|
|
31
|
-
let(:format) { format }
|
|
32
|
-
let(:representer) do
|
|
33
|
-
Class.new(Representable::Decorator) do
|
|
34
|
-
include mod
|
|
35
|
-
property :name
|
|
36
|
-
|
|
37
|
-
collection_representer :class => Song
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it { render(representer.represent(songs)).must_equal_document output }
|
|
42
|
-
it("ficken") { _(parse(representer.represent([]), input)).must_equal songs }
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
for_formats(
|
|
47
|
-
:hash => [Representable::Hash, out = {"name" => "Days Go By"}, out],
|
|
48
|
-
:json => [Representable::JSON, out = "{\"name\":\"Days Go By\"}", out]
|
|
49
|
-
# :xml => [Representable::XML, out="<a><song></song><song></song></a>", out]
|
|
50
|
-
) do |format, mod, output, input|
|
|
51
|
-
# Representer.represents detects singular.
|
|
52
|
-
describe "Module#to_/from_#{format}" do
|
|
53
|
-
let(:format) { format }
|
|
54
|
-
|
|
55
|
-
let(:representer) do
|
|
56
|
-
Module.new do
|
|
57
|
-
include mod
|
|
58
|
-
property :name
|
|
59
|
-
|
|
60
|
-
collection_representer :class => Song
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
it { render(representer.represent(song)).must_equal_document output }
|
|
65
|
-
it { _(parse(representer.represent(Song.new), input)).must_equal song }
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Decorator.represents detects singular.
|
|
69
|
-
describe "Decorator#to_/from_#{format}" do
|
|
70
|
-
let(:format) { format }
|
|
71
|
-
let(:representer) do
|
|
72
|
-
Class.new(Representable::Decorator) do
|
|
73
|
-
include mod
|
|
74
|
-
property :name
|
|
75
|
-
|
|
76
|
-
collection_representer :class => Song
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
it { render(representer.represent(song)).must_equal_document output }
|
|
81
|
-
it { _(parse(representer.represent(Song.new), input)).must_equal song }
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|