restpack_serializer 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 637bfafd73311f2c1dc436e0e39c6b8a50c1be80
|
4
|
+
data.tar.gz: c289ea1c60720cde17d4d42b5cf01d7badc85fba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 877854ce2c5087a9ff2d932df36405f7ee9463de7a9372e66bd5f66a23b9f830259451f6b9bde92f20189dead4c7f2a7790ea926a489ee1cafe19fe231752842
|
7
|
+
data.tar.gz: 825c0a666ed9b6afb31346083e7536a233a5ce654ac2a4efe3d0ef73b5926616621d0586162ea71fd8c59fc1231d6bae743a9be73482f208713941c643d59662
|
@@ -25,11 +25,13 @@ module RestPack::Serializer::Attributes
|
|
25
25
|
def transform_attribute(name, transform_lambda, options = {})
|
26
26
|
add_to_serializable(name, options)
|
27
27
|
|
28
|
+
self.track_defined_methods = false
|
28
29
|
define_method name do
|
29
30
|
transform_lambda.call(name, @model)
|
30
31
|
end
|
31
32
|
|
32
33
|
define_include_method name
|
34
|
+
self.track_defined_methods = true
|
33
35
|
end
|
34
36
|
|
35
37
|
def attribute(name, options={})
|
@@ -46,6 +48,7 @@ module RestPack::Serializer::Attributes
|
|
46
48
|
|
47
49
|
def define_attribute_method(name)
|
48
50
|
unless method_defined?(name)
|
51
|
+
self.track_defined_methods = false
|
49
52
|
define_method name do
|
50
53
|
value = self.default_href if name == :href
|
51
54
|
if @model.is_a?(Hash)
|
@@ -57,6 +60,7 @@ module RestPack::Serializer::Attributes
|
|
57
60
|
value = value.to_s if name == :id
|
58
61
|
value
|
59
62
|
end
|
63
|
+
self.track_defined_methods = true
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
@@ -68,11 +72,7 @@ module RestPack::Serializer::Attributes
|
|
68
72
|
method = "include_#{name}?".to_sym
|
69
73
|
|
70
74
|
unless method_defined?(method)
|
71
|
-
|
72
|
-
define_method method do
|
73
|
-
@context[method].nil? || @context[method]
|
74
|
-
end
|
75
|
-
else
|
75
|
+
unless include_by_default
|
76
76
|
define_method method do
|
77
77
|
@context[method].present?
|
78
78
|
end
|
@@ -84,7 +84,10 @@ module RestPack::Serializer::Attributes
|
|
84
84
|
options[:key] ||= name.to_sym
|
85
85
|
|
86
86
|
@serializable_attributes ||= {}
|
87
|
-
@serializable_attributes[options[:key]] =
|
87
|
+
@serializable_attributes[options[:key]] = {
|
88
|
+
name: name,
|
89
|
+
include_method_name: "include_#{options[:key]}?".to_sym
|
90
|
+
}
|
88
91
|
end
|
89
92
|
end
|
90
93
|
end
|
@@ -38,11 +38,22 @@ module RestPack
|
|
38
38
|
end
|
39
39
|
|
40
40
|
@model, @context = model, context
|
41
|
+
user_defined_methods = self.class.user_defined_methods
|
41
42
|
|
42
43
|
data = {}
|
43
44
|
if self.class.serializable_attributes.present?
|
44
|
-
self.class.serializable_attributes.each do |key,
|
45
|
-
|
45
|
+
self.class.serializable_attributes.each do |key, attribute|
|
46
|
+
method_name = attribute[:include_method_name]
|
47
|
+
name = attribute[:name]
|
48
|
+
|
49
|
+
if user_defined_methods.include?(method_name)
|
50
|
+
data[key] = self.send(name) if self.send(method_name)
|
51
|
+
else
|
52
|
+
#the default implementation of `include_abc?`
|
53
|
+
if @context[method_name].nil? || @context[method_name]
|
54
|
+
data[key] = self.send(name)
|
55
|
+
end
|
56
|
+
end
|
46
57
|
end
|
47
58
|
end
|
48
59
|
|
@@ -53,7 +64,7 @@ module RestPack
|
|
53
64
|
end
|
54
65
|
|
55
66
|
def custom_attributes
|
56
|
-
|
67
|
+
nil
|
57
68
|
end
|
58
69
|
|
59
70
|
private
|
@@ -83,12 +94,16 @@ module RestPack
|
|
83
94
|
data
|
84
95
|
end
|
85
96
|
|
86
|
-
def include_attribute?(name)
|
87
|
-
self.send("include_#{name}?".to_sym)
|
88
|
-
end
|
89
|
-
|
90
97
|
module ClassMethods
|
91
|
-
attr_accessor :model_class, :href_prefix, :key
|
98
|
+
attr_accessor :model_class, :href_prefix, :key, :user_defined_methods, :track_defined_methods
|
99
|
+
|
100
|
+
def method_added(name)
|
101
|
+
#we track used defined methods so that we can make quick decisions at runtime
|
102
|
+
@user_defined_methods ||= []
|
103
|
+
if @track_defined_methods
|
104
|
+
@user_defined_methods << name
|
105
|
+
end
|
106
|
+
end
|
92
107
|
|
93
108
|
def array_as_json(models, context = {})
|
94
109
|
new.as_json(models, context)
|
data/performance/perf.rb
CHANGED
@@ -6,7 +6,14 @@ class SimpleSerializer
|
|
6
6
|
attributes :id, :title
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
class ComplexSerializer
|
10
|
+
include RestPack::Serializer
|
11
|
+
|
12
|
+
attributes :a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t
|
13
|
+
end
|
14
|
+
|
15
|
+
iterations = 180_000
|
16
|
+
|
10
17
|
Benchmark.bm(22) do |bm|
|
11
18
|
bm.report('simple serializer') do
|
12
19
|
|
@@ -19,4 +26,16 @@ Benchmark.bm(22) do |bm|
|
|
19
26
|
SimpleSerializer.as_json(model)
|
20
27
|
end
|
21
28
|
end
|
29
|
+
|
30
|
+
bm.report('complex serializer') do
|
31
|
+
|
32
|
+
model = {
|
33
|
+
a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10,
|
34
|
+
k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20,
|
35
|
+
}
|
36
|
+
|
37
|
+
iterations.times do
|
38
|
+
ComplexSerializer.as_json(model)
|
39
|
+
end
|
40
|
+
end
|
22
41
|
end
|
@@ -18,12 +18,18 @@ describe RestPack::Serializer::Attributes do
|
|
18
18
|
|
19
19
|
it "correctly maps normal attributes" do
|
20
20
|
[:a, :b, :c, :d, :e, :f?].each do |attr|
|
21
|
-
expect(attributes[attr]).to eq(
|
21
|
+
expect(attributes[attr]).to eq({
|
22
|
+
name: attr,
|
23
|
+
include_method_name: "include_#{attr}?".to_sym
|
24
|
+
})
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
28
|
it "correctly maps attribute with :key options" do
|
26
|
-
expect(attributes[:new_key]).to eq(
|
29
|
+
expect(attributes[:new_key]).to eq({
|
30
|
+
name: :old_attribute,
|
31
|
+
include_method_name: :include_new_key?
|
32
|
+
})
|
27
33
|
end
|
28
34
|
|
29
35
|
describe "optional attributes" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restpack_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Joyce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|