protobuf-activerecord 3.3.1 → 3.3.2

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: 83d490440de55e5f819bad525efef1571449f9f0
4
- data.tar.gz: 7f1048b2c82821f5646c61c0332797c2dc4d6cb5
3
+ metadata.gz: 1dd165bad09c99c5b394865faaaa00efaffcb3fb
4
+ data.tar.gz: 88c7bb55c4408f66397ffb8ca5f4fda21c0e4a64
5
5
  SHA512:
6
- metadata.gz: 0dcd9ec4c112cff39b1eb6996d2509ae1a3b07ec10d4b3ed0ffaf67921bd02b699e715053013e37687f7823e0fcfca2e57a762e816103d09c7ce3cdf63ba1afc
7
- data.tar.gz: 62848e072057f8f64c875529f50927381d4917d3c034e248caa509f3a037ee13b4742d5fc3962a52ba8a38a339986704245469dd8bd96629bde525b2c2920646
6
+ metadata.gz: 5b89216c06f6a28dc2f0af37bacf0e5cad14b104305a545241a867086458d7f6b60610566e22dbd2010c0e91468647bcc2b437cfd63c0fc99229518d5f61826a
7
+ data.tar.gz: 5cbdf81b2078d11c22d05d9924dc819ad7bbaf4739efad1dd60a37298544aab140474d2a00aea2e91cfa9af61f63c47b18674f587839f75b49a47439898a1781
@@ -1,16 +1,21 @@
1
1
  require 'set'
2
2
  require 'active_support/concern'
3
+ require 'thread'
3
4
 
4
5
  module Protobuf
5
6
  module ActiveRecord
6
7
  module Columns
7
8
  extend ::ActiveSupport::Concern
8
9
 
10
+ COLUMN_TYPE_MAP_MUTEX = ::Mutex.new
11
+ DATE_OR_TIME_TYPES = ::Set.new([:date, :datetime, :time, :timestamp])
12
+
9
13
  included do
10
14
  include ::Heredity
11
15
 
12
16
  inheritable_attributes :_protobuf_columns,
13
17
  :_protobuf_column_types,
18
+ :_protobuf_date_datetime_time_or_timestamp_column,
14
19
  :_protobuf_mapped_columns
15
20
  end
16
21
 
@@ -27,20 +32,31 @@ module Protobuf
27
32
  @_protobuf_column_types
28
33
  end
29
34
 
35
+ def _protobuf_date_datetime_time_or_timestamp_column
36
+ _protobuf_map_columns unless _protobuf_mapped_columns?
37
+
38
+ @_protobuf_date_datetime_time_or_timestamp_column
39
+ end
40
+
30
41
  # :nodoc:
31
42
  def _protobuf_date_column?(key)
32
- _protobuf_column_types.fetch(:date, false) && _protobuf_column_types[:date].include?(key)
43
+ _protobuf_column_types[:date].include?(key)
44
+ end
45
+
46
+ # :nodoc:
47
+ def _protobuf_date_datetime_time_or_timestamp_column?(key)
48
+ _protobuf_date_datetime_time_or_timestamp_column.include?(key)
33
49
  end
34
50
 
35
51
  # :nodoc:
36
52
  def _protobuf_datetime_column?(key)
37
- _protobuf_column_types.fetch(:datetime, false) && _protobuf_column_types[:datetime].include?(key)
53
+ _protobuf_column_types[:datetime].include?(key)
38
54
  end
39
55
 
40
56
  # Map out the columns for future reference on type conversion
41
57
  # :nodoc:
42
58
  def _protobuf_map_columns(force = false)
43
- ::Thread.exclusive do
59
+ COLUMN_TYPE_MAP_MUTEX.synchronize do
44
60
  @_protobuf_mapped_columns = false if force
45
61
 
46
62
  return unless table_exists?
@@ -48,10 +64,17 @@ module Protobuf
48
64
 
49
65
  @_protobuf_columns = {}
50
66
  @_protobuf_column_types = ::Hash.new { |h,k| h[k] = ::Set.new }
67
+ @_protobuf_date_datetime_time_or_timestamp_column = ::Set.new
51
68
 
52
69
  columns.map do |column|
53
- @_protobuf_columns[column.name.to_sym] = column
54
- @_protobuf_column_types[column.type.to_sym] << column.name.to_sym
70
+ column_name_symbol = column.name.to_sym
71
+ column_type_symbol = column.type.to_sym
72
+ @_protobuf_columns[column_name_symbol] = column
73
+ @_protobuf_column_types[column_type_symbol] << column_name_symbol
74
+
75
+ if DATE_OR_TIME_TYPES.include?(column_type_symbol)
76
+ @_protobuf_date_datetime_time_or_timestamp_column << column_name_symbol
77
+ end
55
78
  end
56
79
 
57
80
  @_protobuf_mapped_columns = true
@@ -64,12 +87,12 @@ module Protobuf
64
87
 
65
88
  # :nodoc:
66
89
  def _protobuf_time_column?(key)
67
- _protobuf_column_types.fetch(:time, false) && _protobuf_column_types[:time].include?(key)
90
+ _protobuf_column_types[:time].include?(key)
68
91
  end
69
92
 
70
93
  # :nodoc:
71
94
  def _protobuf_timestamp_column?(key)
72
- _protobuf_column_types.fetch(:timestamp, false) && _protobuf_column_types[:timestamp].include?(key)
95
+ _protobuf_column_types[:timestamp].include?(key)
73
96
  end
74
97
  end
75
98
  end
@@ -8,12 +8,14 @@ module Protobuf
8
8
 
9
9
  included do
10
10
  class << self
11
- attr_writer :_protobuf_field_transformers,
11
+ attr_writer :_protobuf_field_symbol_transformers,
12
+ :_protobuf_field_transformers,
12
13
  :_protobuf_field_options,
13
14
  :protobuf_message
14
15
  end
15
16
 
16
17
  private :_protobuf_convert_attributes_to_fields
18
+ private :_protobuf_field_symbol_transformers
17
19
  private :_protobuf_field_transformers
18
20
  private :_protobuf_message
19
21
  end
@@ -21,26 +23,20 @@ module Protobuf
21
23
  module ClassMethods
22
24
  # :nodoc:
23
25
  def _protobuf_convert_attributes_to_fields(key, value)
24
- case
25
- when value.nil? then
26
- value
27
- when _protobuf_date_column?(key) then
28
- value.to_time(:utc).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
26
+ return value unless _protobuf_date_datetime_time_or_timestamp_column?(key)
27
+ return value.to_i unless _protobuf_date_column?(key)
28
+
29
+ value.to_time(:utc).to_i
38
30
  end
39
31
 
40
32
  def _protobuf_field_options
41
33
  @_protobuf_field_options ||= {}
42
34
  end
43
35
 
36
+ def _protobuf_field_symbol_transformers
37
+ @_protobuf_field_symbol_transformers ||= {}
38
+ end
39
+
44
40
  def _protobuf_field_transformers
45
41
  @_protobuf_field_transformers ||= {}
46
42
  end
@@ -93,14 +89,13 @@ module Protobuf
93
89
  # end
94
90
  #
95
91
  def field_from_record(field, transformer = nil, &block)
96
- transformer ||= block
97
-
98
92
  if transformer.is_a?(Symbol)
99
- callable = lambda { |value| self.__send__(transformer, value) }
100
- else
101
- callable = transformer
93
+ _protobuf_field_symbol_transformers[field] = transformer
94
+ return
102
95
  end
103
96
 
97
+ transformer ||= block
98
+ callable = transformer
104
99
  unless callable.respond_to?(:call)
105
100
  raise FieldTransformerError
106
101
  end
@@ -225,6 +220,8 @@ module Protobuf
225
220
  while attribute_number < limit
226
221
  field = field_attributes[attribute_number]
227
222
  hash[field] = case
223
+ when _protobuf_field_symbol_transformers.has_key?(field) then
224
+ self.__send__(_protobuf_field_symbol_transformers[field], self)
228
225
  when _protobuf_field_transformers.has_key?(field) then
229
226
  _protobuf_field_transformers[field].call(self)
230
227
  when self.class._protobuf_instance_respond_to_from_cache?(field) then
@@ -247,6 +244,11 @@ module Protobuf
247
244
  self.class._protobuf_convert_attributes_to_fields(field, value)
248
245
  end
249
246
 
247
+ # :nodoc:
248
+ def _protobuf_field_symbol_transformers
249
+ self.class._protobuf_field_symbol_transformers
250
+ end
251
+
250
252
  # :nodoc:
251
253
  def _protobuf_field_transformers
252
254
  self.class._protobuf_field_transformers
@@ -62,6 +62,8 @@ module Protobuf
62
62
  return value if value.nil?
63
63
 
64
64
  value = case
65
+ when !_protobuf_date_datetime_time_or_timestamp_column?(key) then
66
+ value
65
67
  when _protobuf_date_column?(key) then
66
68
  convert_int64_to_date(value)
67
69
  when _protobuf_datetime_column?(key) then
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "3.3.1"
3
+ VERSION = "3.3.2"
4
4
  end
5
5
  end
@@ -23,7 +23,9 @@ describe Protobuf::ActiveRecord::Columns do
23
23
  end
24
24
 
25
25
  it "maps column names by column type" do
26
- expect(User._protobuf_column_types).to eq expected_column_types
26
+ expected_column_types.each do |expected_column_type, value|
27
+ expect(User._protobuf_column_types).to include expected_column_type => value
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -13,7 +13,10 @@ describe Protobuf::ActiveRecord::Serialization do
13
13
  let(:date) { Date.new(2015, 10, 1) }
14
14
  let(:integer) { 1_443_657_600 }
15
15
 
16
- before { allow(User).to receive(:_protobuf_date_column?).and_return(true) }
16
+ before {
17
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
18
+ allow(User).to receive(:_protobuf_date_column?).and_return(true)
19
+ }
17
20
 
18
21
  it "converts the given value to an integer" do
19
22
  expect(User._protobuf_convert_attributes_to_fields(:foo_date, date)).to eq integer
@@ -24,7 +27,10 @@ describe Protobuf::ActiveRecord::Serialization do
24
27
  let(:datetime) { DateTime.current }
25
28
  let(:integer) { datetime.to_time.to_i }
26
29
 
27
- before { allow(User).to receive(:_protobuf_datetime_column?).and_return(true) }
30
+ before {
31
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
32
+ allow(User).to receive(:_protobuf_datetime_column?).and_return(true)
33
+ }
28
34
 
29
35
  it "converts the given value to an integer" do
30
36
  expect(User._protobuf_convert_attributes_to_fields(:foo_datetime, datetime)).to eq integer
@@ -35,7 +41,10 @@ describe Protobuf::ActiveRecord::Serialization do
35
41
  let(:time) { Time.current }
36
42
  let(:integer) { time.to_time.to_i }
37
43
 
38
- before { allow(User).to receive(:_protobuf_time_column?).and_return(true) }
44
+ before {
45
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
46
+ allow(User).to receive(:_protobuf_time_column?).and_return(true)
47
+ }
39
48
 
40
49
  it "converts the given value to an integer" do
41
50
  expect(User._protobuf_convert_attributes_to_fields(:foo_time, time)).to eq integer
@@ -46,7 +55,10 @@ describe Protobuf::ActiveRecord::Serialization do
46
55
  let(:timestamp) { Time.current }
47
56
  let(:integer) { timestamp.to_time.to_i }
48
57
 
49
- before { allow(User).to receive(:_protobuf_timestamp_column?).and_return(true) }
58
+ before {
59
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
60
+ allow(User).to receive(:_protobuf_timestamp_column?).and_return(true)
61
+ }
50
62
 
51
63
  it "converts the given value to an integer" do
52
64
  expect(User._protobuf_convert_attributes_to_fields(:foo_timestamp, timestamp)).to eq integer
@@ -64,13 +76,10 @@ describe Protobuf::ActiveRecord::Serialization do
64
76
 
65
77
  describe ".field_from_record" do
66
78
  context "when the given converter is a symbol" do
67
- let(:callable) { lambda { |value| User.__send__(:extract_first_name) } }
68
-
69
79
  before { User.field_from_record :first_name, :extract_first_name }
70
80
 
71
- it "creates a callable method object from the converter" do
72
- expect(User).to receive(:extract_first_name)
73
- User._protobuf_field_transformers[:first_name].call(1)
81
+ it "creates a symbol transformer from the converter" do
82
+ expect(User._protobuf_field_symbol_transformers[:first_name]).to eq :extract_first_name
74
83
  end
75
84
  end
76
85
 
@@ -34,7 +34,10 @@ describe Protobuf::ActiveRecord::Transformation do
34
34
  let(:date) { Date.current }
35
35
  let(:value) { date.to_time.to_i }
36
36
 
37
- before { allow(User).to receive(:_protobuf_date_column?).and_return(true) }
37
+ before {
38
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
39
+ allow(User).to receive(:_protobuf_date_column?).and_return(true)
40
+ }
38
41
 
39
42
  it "converts the given value to a Date object" do
40
43
  expect(User._protobuf_convert_fields_to_attributes(:foo_date, value)).to eq date
@@ -45,7 +48,10 @@ describe Protobuf::ActiveRecord::Transformation do
45
48
  let(:datetime) { DateTime.current }
46
49
  let(:value) { datetime.to_i }
47
50
 
48
- before { allow(User).to receive(:_protobuf_datetime_column?).and_return(true) }
51
+ before {
52
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
53
+ allow(User).to receive(:_protobuf_datetime_column?).and_return(true)
54
+ }
49
55
 
50
56
  it "converts the given value to a DateTime object" do
51
57
  expect(User._protobuf_convert_fields_to_attributes(:foo_datetime, value)).to be_a(DateTime)
@@ -60,7 +66,10 @@ describe Protobuf::ActiveRecord::Transformation do
60
66
  let(:time) { Time.current }
61
67
  let(:value) { time.to_i }
62
68
 
63
- before { allow(User).to receive(:_protobuf_time_column?).and_return(true) }
69
+ before {
70
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
71
+ allow(User).to receive(:_protobuf_time_column?).and_return(true)
72
+ }
64
73
 
65
74
  it "converts the given value to a Time object" do
66
75
  expect(User._protobuf_convert_fields_to_attributes(:foo_time, value)).to be_a(Time)
@@ -75,7 +84,10 @@ describe Protobuf::ActiveRecord::Transformation do
75
84
  let(:time) { Time.current }
76
85
  let(:value) { time.to_i }
77
86
 
78
- before { allow(User).to receive(:_protobuf_timestamp_column?).and_return(true) }
87
+ before {
88
+ allow(User).to receive(:_protobuf_date_datetime_time_or_timestamp_column?).and_return(true)
89
+ allow(User).to receive(:_protobuf_timestamp_column?).and_return(true)
90
+ }
79
91
 
80
92
  it "converts the given value to a Time object" do
81
93
  expect(User._protobuf_convert_fields_to_attributes(:foo_time, value)).to be_a(Time)
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.3.1
4
+ version: 3.3.2
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-15 00:00:00.000000000 Z
11
+ date: 2016-02-01 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.4.8
237
+ rubygems_version: 2.2.0
238
238
  signing_key:
239
239
  specification_version: 4
240
240
  summary: Google Protocol Buffers integration for Active Record