restpack_serializer 0.6.5 → 0.6.6

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: 7b7353b8336e2522e29fd3f7d784cb3d0b7700aa
4
- data.tar.gz: d82b914f3692048c54b3725f8909c40c469b4a0f
3
+ metadata.gz: 33de851fe3b900b57c1cd36c7146dd6d7e799ee5
4
+ data.tar.gz: fc2674844a0ad0a839cab52e2182c0661611a763
5
5
  SHA512:
6
- metadata.gz: 3e188183204596a3010a77922b7ce1bf7567dc5a2e9935f503bab9b10147f9cf5be77cdd678dead2ce697bfaefeb8f02641049329526af0a84a73efc035f5d40
7
- data.tar.gz: f2da61ed3c6ec04db90bd9a5720e1d983f9a71c341d355e474f3849ec85e77fa0963b79177f8019397ccd8ab775a65b9d5987d12d4c10b865cffa783628c16b3
6
+ metadata.gz: 94794f6281fbd6c7efe7d36ab39439d462ba0200605c1c488dee9522311e4fc5a917e7b660eab673e41d96339ec35a3d9eb67fac87b48dcfb908d8a9705dff9e
7
+ data.tar.gz: 7bec627210c72d263d706a53391e48db1721dc9a6ea754a2e31043fa036e5f8cbdd629896d0b42a72062230a9ce2b8d838c2ff85b1ee89902ba79be3068d9203
@@ -38,15 +38,13 @@ module RestPack
38
38
  end
39
39
 
40
40
  @model, @context = model, context
41
- user_defined_methods = self.class.user_defined_methods || []
42
41
 
43
42
  data = {}
44
43
  if self.class.serializable_attributes.present?
45
44
  self.class.serializable_attributes.each do |key, attribute|
46
45
  method_name = attribute[:include_method_name]
47
46
  name = attribute[:name]
48
-
49
- if user_defined_methods.include?(method_name)
47
+ if self.class.memoized_has_user_defined_method?(method_name)
50
48
  data[key] = self.send(name) if self.send(method_name)
51
49
  else
52
50
  #the default implementation of `include_abc?`
@@ -105,6 +103,24 @@ module RestPack
105
103
  end
106
104
  end
107
105
 
106
+ def has_user_defined_method?(method_name)
107
+ user_defined_methods = self.user_defined_methods || []
108
+ return true if user_defined_methods.include?(method_name)
109
+ return self.superclass.try(:has_user_defined_method?, method_name)
110
+ end
111
+
112
+ def memoized_has_user_defined_method?(method_name)
113
+ @memoized_user_defined_methods ||= {}
114
+
115
+ if @memoized_user_defined_methods.has_key? method_name
116
+ return @memoized_user_defined_methods[method_name]
117
+ else
118
+ has_method = has_user_defined_method?(method_name)
119
+ @memoized_user_defined_methods[method_name] = has_method
120
+ return has_method
121
+ end
122
+ end
123
+
108
124
  def array_as_json(models, context = {})
109
125
  new.as_json(models, context)
110
126
  end
@@ -1,5 +1,5 @@
1
1
  module RestPack
2
2
  module Serializer
3
- VERSION = '0.6.5'
3
+ VERSION = '0.6.6'
4
4
  end
5
5
  end
@@ -34,27 +34,48 @@ describe RestPack::Serializer do
34
34
  context "serializer inheritance" do
35
35
  class BaseSerializer
36
36
  include RestPack::Serializer
37
- attributes :name
37
+ attributes :name, :colour
38
+ optional :count
38
39
 
39
40
  def name
40
- "Ben"
41
+ @context[:name]
42
+ end
43
+
44
+ def count
45
+ 99
41
46
  end
42
47
 
43
48
  def age
44
49
  -2
45
50
  end
51
+
52
+ def colour
53
+ 'purple'
54
+ end
55
+
56
+ def include_colour?
57
+ false
58
+ end
46
59
  end
47
60
 
48
61
  class DerivedSerializer < BaseSerializer
49
- attributes :name, :age
62
+ attributes :name, :age, :food, :colour, :count
50
63
 
51
64
  def age
52
- 1
65
+ @context[:age]
66
+ end
67
+
68
+ def food
69
+ 'crackers'
53
70
  end
54
71
  end
55
72
 
56
73
  it ".as_json serializes" do
57
- DerivedSerializer.as_json(person).should == { name: "Ben", age: 1 }
74
+ serialized = DerivedSerializer.as_json({}, { include_food?: false, name: 'Ben', age: 1 })
75
+ serialized.should == { #NOTE: I think this should include colour as DerivedSerializer defines it, but this would be a big breaking change
76
+ name: "Ben",
77
+ age: 1
78
+ }
58
79
  end
59
80
  end
60
81
 
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.5
4
+ version: 0.6.6
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-10 00:00:00.000000000 Z
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport