protobuf-activerecord 3.2.2 → 3.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90545cb2f19b9a54086785f64eb7f6aeca67c17f
4
- data.tar.gz: da7799d0f3deb1e2ed5f95d39df427f7ac5ca2f9
3
+ metadata.gz: 199e64b43cb1b81008ac6b34da4141f4657bfcd9
4
+ data.tar.gz: 2e8291702c65ade44ad9eb2e8ff5b056d5e03b97
5
5
  SHA512:
6
- metadata.gz: 04345ee9db33e0212bfa94d12c994412ed8326c40bdc3b737735494054dd7f756c4fe0d14855a89b48d119fd57d534d53d33cd762d3ab43fb40ffb18a570e0d6
7
- data.tar.gz: 7dabbb91f14d697099776512a688611326900609366684b5dda15c37eb95cddacacd1be152fc5e516bf600bca3803b82e823ca2d5e108716999d176fb8ce33ab
6
+ metadata.gz: e26afd09e666fcbcbe940c576e0fb2ba5205eb1ca37def8a8eed16d15db3c1f556ced283bc4496b6f4572ddc226b7f815f821c3c67cd98f65f437c28269ab03a
7
+ data.tar.gz: 26290cb0c1b2a2781206966c171e754bd4260a799083f636544f8baee1525dc045010e0d5c5f0d90df4414a8a0a314688a0cbca4c7ddc08bc6f692ed4dedf2a4
@@ -1,4 +1,5 @@
1
- 'active_support/concerns'
1
+ require 'set'
2
+ require 'active_support/concern'
2
3
 
3
4
  module Protobuf
4
5
  module ActiveRecord
@@ -20,22 +21,20 @@ module Protobuf
20
21
  module ClassMethods
21
22
  # :nodoc:
22
23
  def _protobuf_convert_attributes_to_fields(key, value)
23
- return value if value.nil?
24
-
25
- value = case
26
- when _protobuf_date_column?(key) then
27
- value.to_time.to_i
28
- when _protobuf_datetime_column?(key) then
29
- value.to_i
30
- when _protobuf_time_column?(key) then
31
- value.to_i
32
- when _protobuf_timestamp_column?(key) then
33
- value.to_i
34
- else
35
- value
36
- end
37
-
38
- return value
24
+ case
25
+ when value.nil? then
26
+ value
27
+ when _protobuf_date_column?(key) then
28
+ value.to_time.to_i
29
+ when _protobuf_datetime_column?(key) then
30
+ value.to_i
31
+ when _protobuf_time_column?(key) then
32
+ value.to_i
33
+ when _protobuf_timestamp_column?(key) then
34
+ value.to_i
35
+ else
36
+ value
37
+ end
39
38
  end
40
39
 
41
40
  def _protobuf_field_options
@@ -46,6 +45,36 @@ module Protobuf
46
45
  @_protobuf_field_transformers ||= {}
47
46
  end
48
47
 
48
+ def _protobuf_instance_respond_to_from_cache?(key)
49
+ _protobuf_respond_to_cache.include?(key)
50
+ end
51
+
52
+ def _protobuf_message_deprecated_fields
53
+ @_protobuf_message_deprecated_fields ||= begin
54
+ self.protobuf_message.all_fields.map do |field|
55
+ next if field.nil?
56
+ next unless field.deprecated?
57
+
58
+ field.name.to_sym
59
+ end
60
+ end
61
+ end
62
+
63
+ def _protobuf_message_non_deprecated_fields
64
+ @_protobuf_message_non_deprecated_fields ||= begin
65
+ self.protobuf_message.all_fields.map do |field|
66
+ next if field.nil?
67
+ next if field.deprecated?
68
+
69
+ field.name.to_sym
70
+ end
71
+ end
72
+ end
73
+
74
+ def _protobuf_respond_to_cache
75
+ @_protobuf_respond_to_cache ||= ::Set.new
76
+ end
77
+
49
78
  # Define a field transformation from a record. Accepts a Symbol,
50
79
  # callable, or block that is called with the record being serialized.
51
80
  #
@@ -146,16 +175,12 @@ module Protobuf
146
175
 
147
176
  # :nodoc:
148
177
  def _filtered_fields(options = {})
149
- exclude_deprecated = ! options.fetch(:deprecated, true)
178
+ include_deprecated = options.fetch(:deprecated, true)
150
179
 
151
- fields = self.class.protobuf_message.all_fields.map do |field|
152
- next if field.nil?
153
- next if field.deprecated? && exclude_deprecated
154
-
155
- field.name.to_sym
156
- end
157
- fields += [ options.fetch(:include, nil) ]
158
- fields.flatten!
180
+ fields = []
181
+ fields.concat(self.class._protobuf_message_non_deprecated_fields)
182
+ fields.concat(self.class._protobuf_message_deprecated_fields) if include_deprecated
183
+ fields.concat([options[:include]].flatten) if options[:include].present?
159
184
  fields.compact!
160
185
  fields.uniq!
161
186
 
@@ -182,23 +207,39 @@ module Protobuf
182
207
  # fields_from_record(:except => :email_domain, :deprecated => false)
183
208
  #
184
209
  def fields_from_record(options = {})
210
+ hash = {}
185
211
  field_attributes = _filter_field_attributes(options)
186
- field_attributes += [ options.fetch(:include, []) ]
187
- field_attributes.flatten!
188
- field_attributes.compact!
189
- field_attributes.uniq!
190
-
191
- field_attributes = field_attributes.inject({}) do |hash, field|
192
- if _protobuf_field_transformers.has_key?(field)
193
- hash[field] = _protobuf_field_transformers[field].call(self)
194
- else
195
- value = respond_to?(field) ? __send__(field) : nil
196
- hash[field] = _protobuf_convert_attributes_to_fields(field, value)
197
- end
198
- hash
212
+
213
+ # Already flattened / compacted / uniqued ... unless we must include
214
+ if options[:include].present?
215
+ field_attributes.concat([ options[:include] ].flatten)
216
+ field_attributes.compact!
217
+ field_attributes.uniq!
218
+ end
219
+
220
+ attribute_number = 0
221
+ limit = field_attributes.size
222
+
223
+ # One of the very few places the diff between each/while can make a difference
224
+ # in terms of optimization (`while` is slightly faster as no block carried through)
225
+ while attribute_number < limit
226
+ field = field_attributes[attribute_number]
227
+ hash[field] = case
228
+ when _protobuf_field_transformers.has_key?(field) then
229
+ _protobuf_field_transformers[field].call(self)
230
+ when self.class._protobuf_instance_respond_to_from_cache?(field) then
231
+ _protobuf_convert_attributes_to_fields(field, __send__(field))
232
+ when respond_to?(field) then
233
+ self.class._protobuf_respond_to_cache << field
234
+ _protobuf_convert_attributes_to_fields(field, __send__(field))
235
+ else
236
+ nil
237
+ end
238
+
239
+ attribute_number += 1
199
240
  end
200
241
 
201
- field_attributes
242
+ hash
202
243
  end
203
244
 
204
245
  # :nodoc:
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.2.2"
3
+ VERSION = "3.3.0"
4
4
  end
5
5
  end
@@ -135,20 +135,20 @@ describe Protobuf::ActiveRecord::Serialization do
135
135
  context "when options has :except" do
136
136
  it "returns all except the given field(s)" do
137
137
  fields = user._filter_field_attributes(:except => :name)
138
- expect(fields).to eq [ :guid, :email, :email_domain, :password, :nullify ]
138
+ expect(fields).to match_array([ :guid, :email, :email_domain, :password, :nullify ])
139
139
  end
140
140
  end
141
141
  end
142
142
 
143
143
  describe "#_filtered_fields" do
144
144
  it "returns protobuf fields" do
145
- expect(user._filtered_fields).to eq [ :guid, :name, :email, :email_domain, :password, :nullify ]
145
+ expect(user._filtered_fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify ])
146
146
  end
147
147
 
148
148
  context "given :deprecated => false" do
149
149
  it "filters all deprecated fields" do
150
150
  fields = user._filtered_fields(:deprecated => false)
151
- expect(fields).to eq [ :guid, :name, :email, :password, :nullify ]
151
+ expect(fields).to match_array([ :guid, :name, :email, :password, :nullify ])
152
152
  end
153
153
 
154
154
  context 'and :include => :email_domain' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hutchison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-12 00:00:00.000000000 Z
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -234,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
234
234
  version: '0'
235
235
  requirements: []
236
236
  rubyforge_project:
237
- rubygems_version: 2.2.0
237
+ rubygems_version: 2.4.8
238
238
  signing_key:
239
239
  specification_version: 4
240
240
  summary: Google Protocol Buffers integration for Active Record