protobuf-activerecord 1.2.6 → 2.0.0.beta

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.
@@ -2,7 +2,7 @@ message UserMessage {
2
2
  optional string guid = 1;
3
3
  optional string name = 2;
4
4
  optional string email = 3;
5
- optional string email_domain = 4;
5
+ optional string email_domain = 4 [deprecated = true];
6
6
  }
7
7
 
8
8
  message UserSearchMessage {
@@ -7,14 +7,6 @@ class User < ActiveRecord::Base
7
7
  attribute_from_proto :first_name, :extract_first_name
8
8
  attribute_from_proto :last_name, :extract_last_name
9
9
 
10
- def self.convert_base64_to_string(value)
11
- value
12
- end
13
-
14
- def self.convert_email_to_lowercase(value)
15
- value
16
- end
17
-
18
10
  def self.extract_first_name(proto)
19
11
  if proto.has_field?(:name)
20
12
  names = proto.name.split(" ")
@@ -34,6 +26,10 @@ class User < ActiveRecord::Base
34
26
  last_name
35
27
  end
36
28
 
29
+ def token
30
+ "key"
31
+ end
32
+
37
33
  def name
38
34
  "#{first_name} #{last_name}"
39
35
  end
@@ -17,7 +17,7 @@ class UserMessage
17
17
  optional ::Protobuf::Field::StringField, :guid, 1
18
18
  optional ::Protobuf::Field::StringField, :name, 2
19
19
  optional ::Protobuf::Field::StringField, :email, 3
20
- optional ::Protobuf::Field::StringField, :email_domain, 4
20
+ optional ::Protobuf::Field::StringField, :email_domain, 4, :deprecated => true
21
21
  end
22
22
 
23
23
  class UserSearchMessage
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.6
5
- prerelease:
4
+ version: 2.0.0.beta
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Adam Hutchison
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-11 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '2.0'
53
+ version: 2.1.3
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '2.0'
61
+ version: 2.1.3
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -203,20 +203,20 @@ files:
203
203
  - Rakefile
204
204
  - lib/protobuf-activerecord.rb
205
205
  - lib/protobuf/activerecord/protoable.rb
206
- - lib/protobuf/activerecord/protoable/convert.rb
206
+ - lib/protobuf/activerecord/protoable/columns.rb
207
207
  - lib/protobuf/activerecord/protoable/errors.rb
208
- - lib/protobuf/activerecord/protoable/fields.rb
209
208
  - lib/protobuf/activerecord/protoable/persistence.rb
210
209
  - lib/protobuf/activerecord/protoable/scope.rb
211
210
  - lib/protobuf/activerecord/protoable/serialization.rb
211
+ - lib/protobuf/activerecord/protoable/transformation.rb
212
212
  - lib/protobuf/activerecord/protobuf_ext/message.rb
213
213
  - lib/protobuf/activerecord/version.rb
214
214
  - protobuf-activerecord.gemspec
215
- - spec/protoable/convert_spec.rb
216
- - spec/protoable/fields_spec.rb
215
+ - spec/protoable/columns_spec.rb
217
216
  - spec/protoable/persistence_spec.rb
218
217
  - spec/protoable/scope_spec.rb
219
218
  - spec/protoable/serialization_spec.rb
219
+ - spec/protoable/transformation_spec.rb
220
220
  - spec/spec_helper.rb
221
221
  - spec/support/db.rb
222
222
  - spec/support/db/setup.rb
@@ -239,28 +239,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: 3337264365514211016
242
+ hash: -908655066687931093
243
243
  required_rubygems_version: !ruby/object:Gem::Requirement
244
244
  none: false
245
245
  requirements:
246
- - - ! '>='
246
+ - - ! '>'
247
247
  - !ruby/object:Gem::Version
248
- version: '0'
249
- segments:
250
- - 0
251
- hash: 3337264365514211016
248
+ version: 1.3.1
252
249
  requirements: []
253
250
  rubyforge_project:
254
- rubygems_version: 1.8.24
251
+ rubygems_version: 1.8.25
255
252
  signing_key:
256
253
  specification_version: 3
257
254
  summary: Google Protocol Buffers integration for Active Record
258
255
  test_files:
259
- - spec/protoable/convert_spec.rb
260
- - spec/protoable/fields_spec.rb
256
+ - spec/protoable/columns_spec.rb
261
257
  - spec/protoable/persistence_spec.rb
262
258
  - spec/protoable/scope_spec.rb
263
259
  - spec/protoable/serialization_spec.rb
260
+ - spec/protoable/transformation_spec.rb
264
261
  - spec/spec_helper.rb
265
262
  - spec/support/db.rb
266
263
  - spec/support/db/setup.rb
@@ -1,79 +0,0 @@
1
- module Protoable
2
- module Convert
3
- def self.included(klass)
4
- klass.extend Protoable::Convert::ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- def convert_int64_to_time(int64)
9
- Time.at(int64.to_i)
10
- end
11
-
12
- def convert_int64_to_date(int64)
13
- convert_int64_to_time(int64).utc.to_date
14
- end
15
-
16
- def convert_int64_to_datetime(int64)
17
- convert_int64_to_time(int64).to_datetime
18
- end
19
-
20
- def _protobuf_convert_attributes_to_fields(key, value)
21
- return value if value.nil?
22
-
23
- value = case
24
- when _protobuf_attribute_converters.has_key?(key.to_sym) then
25
- _protobuf_attribute_converters[key.to_sym].call(value)
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
39
- end
40
-
41
- def _protobuf_convert_fields_to_columns(key, value)
42
- return value if value.nil?
43
-
44
- value = case
45
- when _protobuf_field_converters.has_key?(key.to_sym) then
46
- _protobuf_field_converters[key.to_sym].call(value)
47
- when _protobuf_date_column?(key) then
48
- convert_int64_to_date(value)
49
- when _protobuf_datetime_column?(key) then
50
- convert_int64_to_datetime(value)
51
- when _protobuf_time_column?(key) then
52
- convert_int64_to_time(value)
53
- when _protobuf_timestamp_column?(key) then
54
- convert_int64_to_time(value)
55
- else
56
- value
57
- end
58
-
59
- return value
60
- end
61
-
62
- def _protobuf_date_column?(key)
63
- _protobuf_column_types.fetch(:date, false) && _protobuf_column_types[:date].include?(key)
64
- end
65
-
66
- def _protobuf_datetime_column?(key)
67
- _protobuf_column_types.fetch(:datetime, false) && _protobuf_column_types[:datetime].include?(key)
68
- end
69
-
70
- def _protobuf_time_column?(key)
71
- _protobuf_column_types.fetch(:time, false) && _protobuf_column_types[:time].include?(key)
72
- end
73
-
74
- def _protobuf_timestamp_column?(key)
75
- _protobuf_column_types.fetch(:timestamp, false) && _protobuf_column_types[:timestamp].include?(key)
76
- end
77
- end
78
- end
79
- end
@@ -1,109 +0,0 @@
1
- require 'heredity/inheritable_class_instance_variables'
2
-
3
- module Protoable
4
- module Fields
5
- def self.extended(klass)
6
- klass.extend Protoable::Fields::ClassMethods
7
- klass.__send__(:include, ::Heredity::InheritableClassInstanceVariables)
8
-
9
- klass.class_eval do
10
- class << self
11
- attr_accessor :_protobuf_columns, :_protobuf_column_types,
12
- :_protobuf_attribute_transformers, :_protobuf_field_converters
13
- end
14
-
15
- @_protobuf_attribute_transformers = {}
16
- @_protobuf_columns = {}
17
- @_protobuf_column_types = Hash.new { |h,k| h[k] = [] }
18
- @_protobuf_field_converters = {}
19
-
20
- # NOTE: Make sure each inherited object has the database layout
21
- inheritable_attributes :_protobuf_attribute_transformers, :_protobuf_columns,
22
- :_protobuf_column_types, :_protobuf_field_converters
23
- end
24
-
25
- _protobuf_map_columns(klass)
26
- end
27
-
28
- # Map out the columns for future reference on type conversion
29
- #
30
- def self._protobuf_map_columns(klass)
31
- return unless klass.table_exists?
32
- klass.columns.map do |column|
33
- klass._protobuf_columns[column.name.to_sym] = column
34
- klass._protobuf_column_types[column.type.to_sym] << column.name.to_sym
35
- end
36
- end
37
-
38
- module ClassMethods
39
- # Define a field conversion from protobuf to db. Accepts a Symbol,
40
- # Hash, callable or block.
41
- #
42
- # When given a callable or block, it is directly used to convert the field.
43
- #
44
- # When a Hash is given, :from and :to keys are expected and expand
45
- # to extracting a class method in the format of
46
- # "convert_#{from}_to_#{to}".
47
- #
48
- # When a symbol is given, it extracts the method with the same name.
49
- #
50
- # Examples:
51
- # convert_field :public_key, :extract_public_key_from_proto
52
- # convert_field :symmetric_key, :from => :base64, :to => :encoded_string
53
- # convert_field :status, lambda { |proto_field| # Do some stuff... }
54
- # convert_field :status do |proto_field|
55
- # # Do some blocky stuff...
56
- # end
57
- #
58
- def convert_field(field, converter = nil, &blk)
59
- converter ||= blk
60
- converter = :"convert_#{converter[:from]}_to_#{converter[:to]}" if converter.is_a?(Hash)
61
-
62
- if converter.is_a?(Symbol)
63
- callable = lambda { |value| self.__send__(converter, value) }
64
- else
65
- callable = converter
66
- end
67
-
68
- unless callable.respond_to?(:call)
69
- raise FieldConverterError, 'Field converters must be a callable or block!'
70
- end
71
-
72
- _protobuf_field_converters[field.to_sym] = callable
73
- end
74
-
75
- # Define an attribute transformation from protobuf. Accepts a Symbol,
76
- # callable, or block.
77
- #
78
- # When given a callable or block, it is directly used to convert the field.
79
- #
80
- # When a symbol is given, it extracts the method with the same name.
81
- #
82
- # The callable or method must accept a single parameter, which is the
83
- # proto message.
84
- #
85
- # Examples:
86
- # attribute_from_proto :public_key, :extract_public_key_from_proto
87
- # attribute_from_proto :status, lambda { |proto| # Do some stuff... }
88
- # attribute_from_proto :status do |proto|
89
- # # Do some blocky stuff...
90
- # end
91
- #
92
- def attribute_from_proto(attribute, transformer = nil, &blk)
93
- transformer ||= blk
94
-
95
- if transformer.is_a?(Symbol)
96
- callable = lambda { |value| self.__send__(transformer, value) }
97
- else
98
- callable = transformer
99
- end
100
-
101
- unless callable.respond_to?(:call)
102
- raise AttributeTransformerError, 'Attribute transformers need a callable or block!'
103
- end
104
-
105
- _protobuf_attribute_transformers[attribute.to_sym] = callable
106
- end
107
- end
108
- end
109
- end
@@ -1,265 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Protoable::Convert do
4
- describe ".convert_int64_to_date" do
5
- let(:date) { Date.current }
6
- let(:int64) { date.to_time.to_i }
7
-
8
- it "initializes a new Date object from the value" do
9
- Timecop.freeze(Date.current) do
10
- User.convert_int64_to_date(int64).should eq date
11
- end
12
- end
13
- end
14
-
15
- describe ".convert_int64_to_datetime" do
16
- let(:datetime) { DateTime.current }
17
- let(:int64) { datetime.to_time.to_i }
18
-
19
- it "initializes a new DateTime object from the value" do
20
- Timecop.freeze(DateTime.current) do
21
- User.convert_int64_to_datetime(int64).should eq datetime
22
- end
23
- end
24
- end
25
-
26
- describe ".convert_int64_to_time" do
27
- let(:time) { Time.current }
28
- let(:int64) { time.to_time.to_i }
29
-
30
- it "initializes a new Time object from the value" do
31
- Timecop.freeze(Time.current) do
32
- User.convert_int64_to_time(int64).should be_within(1).of(time)
33
- end
34
- end
35
- end
36
-
37
- describe "._protobuf_convert_attributes_to_fields" do
38
- context "when there is a attribute converter for the field" do
39
- let(:email_value) { "foo@test.co" }
40
- let(:email_converter) { User.method(:convert_email_to_lowercase) }
41
-
42
- before { User.stub(:_protobuf_attribute_converters).and_return({ :email => email_converter }) }
43
-
44
- it "calls the attribute converter with the given value" do
45
- email_converter.should_receive(:call).with(email_value)
46
- User._protobuf_convert_attributes_to_fields(:email, email_value)
47
- end
48
-
49
- context "and it's corresponding column type has a default converter" do
50
- before { User.stub(:_protobuf_date_column?).and_return(true) }
51
-
52
- it "calls the attribute converter with the given value" do
53
- email_converter.should_receive(:call).with(email_value)
54
- User._protobuf_convert_attributes_to_fields(:email, email_value)
55
- end
56
- end
57
- end
58
-
59
- context "when the column type is :date" do
60
- let(:date) { Date.current }
61
- let(:integer) { date.to_time.to_i }
62
-
63
- before { User.stub(:_protobuf_date_column?).and_return(true) }
64
-
65
- it "converts the given value to an integer" do
66
- User._protobuf_convert_attributes_to_fields(:foo_date, date).should eq integer
67
- end
68
- end
69
-
70
- context "when the column type is :datetime" do
71
- let(:datetime) { DateTime.current }
72
- let(:integer) { datetime.to_time.to_i }
73
-
74
- before { User.stub(:_protobuf_datetime_column?).and_return(true) }
75
-
76
- it "converts the given value to an integer" do
77
- User._protobuf_convert_attributes_to_fields(:foo_datetime, datetime).should eq integer
78
- end
79
- end
80
-
81
- context "when the column type is :time" do
82
- let(:time) { Time.current }
83
- let(:integer) { time.to_time.to_i }
84
-
85
- before { User.stub(:_protobuf_time_column?).and_return(true) }
86
-
87
- it "converts the given value to an integer" do
88
- User._protobuf_convert_attributes_to_fields(:foo_time, time).should eq integer
89
- end
90
- end
91
-
92
- context "when the column type is :timestamp" do
93
- let(:timestamp) { Time.current }
94
- let(:integer) { timestamp.to_time.to_i }
95
-
96
- before { User.stub(:_protobuf_timestamp_column?).and_return(true) }
97
-
98
- it "converts the given value to an integer" do
99
- User._protobuf_convert_attributes_to_fields(:foo_timestamp, timestamp).should eq integer
100
- end
101
- end
102
-
103
- context "when no conversion is necessary" do
104
- let(:value) { "Foo" }
105
-
106
- it "returns the given value" do
107
- User._protobuf_convert_attributes_to_fields(:foo, value).should eq value
108
- end
109
- end
110
- end
111
-
112
- describe "._protobuf_convert_fields_to_columns" do
113
- let(:email_value) { "foo@test.co" }
114
- let(:email_converter) { lambda { |value| value } }
115
-
116
- before { User.stub(:_protobuf_field_converters).and_return({ :email => email_converter }) }
117
-
118
- it "calls the field converter with the given value" do
119
- email_converter.should_receive(:call).with(email_value)
120
- User._protobuf_convert_fields_to_columns(:email, email_value)
121
- end
122
-
123
- context "and it's corresponding column type has a default converter" do
124
- before { User.stub(:_protobuf_date_column?).and_return(true) }
125
-
126
- it "calls the field converter with the given value" do
127
- email_converter.should_receive(:call).with(email_value)
128
- User._protobuf_convert_fields_to_columns(:email, email_value)
129
- end
130
- end
131
-
132
- context "when the given field's corresponding column type is :date" do
133
- let(:date) { Date.current }
134
- let(:value) { date.to_time.to_i }
135
-
136
- before { User.stub(:_protobuf_date_column?).and_return(true) }
137
-
138
- it "converts the given value to a Date object" do
139
- User._protobuf_convert_fields_to_columns(:foo_date, value).should eq date
140
- end
141
- end
142
-
143
- context "when given field's corresponding the column type is :datetime" do
144
- let(:datetime) { DateTime.current }
145
- let(:value) { datetime.to_i }
146
-
147
- before { User.stub(:_protobuf_datetime_column?).and_return(true) }
148
-
149
- it "converts the given value to a DateTime object" do
150
- User._protobuf_convert_fields_to_columns(:foo_datetime, value).should be_a(DateTime)
151
- end
152
-
153
- it "converts the given value to a DateTime object of the same value" do
154
- User._protobuf_convert_fields_to_columns(:foo_datetime, value).should be_within(1).of(datetime)
155
- end
156
- end
157
-
158
- context "when given field's corresponding the column type is :time" do
159
- let(:time) { Time.current }
160
- let(:value) { time.to_i }
161
-
162
- before { User.stub(:_protobuf_time_column?).and_return(true) }
163
-
164
- it "converts the given value to a Time object" do
165
- User._protobuf_convert_fields_to_columns(:foo_time, value).should be_a(Time)
166
- end
167
-
168
- it "converts the given value to a Time object of the same value" do
169
- User._protobuf_convert_fields_to_columns(:foo_time, value).should be_within(1).of(time)
170
- end
171
- end
172
-
173
- context "when given field's corresponding the column type is :timestamp" do
174
- let(:time) { Time.current }
175
- let(:value) { time.to_i }
176
-
177
- before { User.stub(:_protobuf_timestamp_column?).and_return(true) }
178
-
179
- it "converts the given value to a Time object" do
180
- User._protobuf_convert_fields_to_columns(:foo_time, value).should be_a(Time)
181
- end
182
-
183
- it "converts the given value to a Time object of the same value" do
184
- User._protobuf_convert_fields_to_columns(:foo_timestamp, value).should be_within(1).of(time)
185
- end
186
- end
187
-
188
- context "when no conversion is necessary" do
189
- let(:value) { "Foo" }
190
-
191
- it "returns the given value" do
192
- User._protobuf_convert_fields_to_columns(:foo, value).should eq value
193
- end
194
- end
195
- end
196
-
197
- context "column type predicates" do
198
- before { User.stub(:_protobuf_column_types).and_return(Hash.new) }
199
- after { User.unstub(:_protobuf_column_types) }
200
-
201
- describe "._protobuf_date_column?" do
202
- before { User._protobuf_column_types[:date] = [ :foo_date ] }
203
-
204
- context "when the column type is :date" do
205
- it "is true" do
206
- User._protobuf_date_column?(:foo_date).should be_true
207
- end
208
- end
209
-
210
- context "when the column type is not :date" do
211
- it "is false" do
212
- User._protobuf_date_column?(:bar_date).should be_false
213
- end
214
- end
215
- end
216
-
217
- describe "._protobuf_datetime_column?" do
218
- before { User._protobuf_column_types[:datetime] = [ :foo_datetime ] }
219
-
220
- context "when the column type is :datetime" do
221
- it "is true" do
222
- User._protobuf_datetime_column?(:foo_datetime).should be_true
223
- end
224
- end
225
-
226
- context "when the column type is not :datetime" do
227
- it "is false" do
228
- User._protobuf_datetime_column?(:bar_datetime).should be_false
229
- end
230
- end
231
- end
232
-
233
- describe "._protobuf_time_column?" do
234
- before { User._protobuf_column_types[:time] = [ :foo_time ] }
235
-
236
- context "when the column type is :time" do
237
- it "is true" do
238
- User._protobuf_time_column?(:foo_time).should be_true
239
- end
240
- end
241
-
242
- context "when the column type is not :time" do
243
- it "is false" do
244
- User._protobuf_time_column?(:bar_time).should be_false
245
- end
246
- end
247
- end
248
-
249
- describe "._protobuf_timestamp_column?" do
250
- before { User._protobuf_column_types[:timestamp] = [ :foo_timestamp ] }
251
-
252
- context "when the column type is :timestamp" do
253
- it "is true" do
254
- User._protobuf_timestamp_column?(:foo_timestamp).should be_true
255
- end
256
- end
257
-
258
- context "when the column type is not :timestamp" do
259
- it "is false" do
260
- User._protobuf_timestamp_column?(:bar_timestamp).should be_false
261
- end
262
- end
263
- end
264
- end
265
- end