protobuf-activerecord 3.1.1 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/.travis.yml +8 -0
- data/README.md +44 -0
- data/lib/protobuf/active_record/serialization.rb +5 -1
- data/lib/protobuf/active_record/transformation.rb +30 -10
- data/lib/protobuf/active_record/transformer.rb +23 -0
- data/lib/protobuf/active_record/version.rb +1 -1
- data/protobuf-activerecord.gemspec +2 -2
- data/spec/protobuf/active_record/columns_spec.rb +11 -12
- data/spec/protobuf/active_record/persistence_spec.rb +10 -10
- data/spec/protobuf/active_record/scope_spec.rb +14 -23
- data/spec/protobuf/active_record/serialization_spec.rb +43 -35
- data/spec/protobuf/active_record/transformation_spec.rb +57 -33
- data/spec/protobuf/active_record/transformer_spec.rb +42 -0
- data/spec/spec_helper.rb +15 -1
- data/spec/support/definitions/user.proto +1 -0
- data/spec/support/protobuf/user.pb.rb +1 -0
- metadata +11 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a352c983607d34d912b75e84b87cfaade5fb40eb
|
4
|
+
data.tar.gz: a656aa395f61f5e6f5f3da5aa7ff2e5aa01172ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 120872dfa9ea6f4532d14de413a2e4b911e3f96c6663398f15ff18b78e3956669583420bb3bcc0aaceeec649e14d5fc1e72f67495526a4b9b16df9869bc96545
|
7
|
+
data.tar.gz: dd907e242f10564144ef5117e916ec58915abb9f5cbda7a972ab6558c9aff23b5158b90de3702c75b55a861bbc476e8755f526c533520e5f8ff70b132ee0ae88
|
data/.rspec
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/protobuf-activerecord.svg)](http://badge.fury.io/rb/protobuf-activerecord)
|
2
|
+
[![Build Status](https://travis-ci.org/liveh2o/protobuf-activerecord.svg?branch=master)](https://travis-ci.org/liveh2o/protobuf-activerecord)
|
3
|
+
|
1
4
|
# Protobuf ActiveRecord
|
2
5
|
|
3
6
|
Protobuf Active Record provides the ability to create and update Active Record objects from protobuf messages and to serialize Active Record objects to protobuf messages.
|
@@ -128,6 +131,15 @@ class User < ActiveRecord::Base
|
|
128
131
|
end
|
129
132
|
```
|
130
133
|
|
134
|
+
On the chance you want to exclude deprecated fields, but also include a specfic one, simply pass the `:include` option:
|
135
|
+
|
136
|
+
```Ruby
|
137
|
+
class User < ActiveRecord::Base
|
138
|
+
# Passing :deprecated => false configures Protobuf Active Record to exclude deprecated fields.
|
139
|
+
protobuf_message :user_message, :deprecated => false, :include => :a_specific_deprecated_field
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
131
143
|
### Field transformers
|
132
144
|
|
133
145
|
Field transformers are used when serializing objects to protobuf messages. Regular field mapping and conversions will be handled out of the box, but for those times when fields don't map directly to attributes or custom behavior is needed, use `field_from_record` method.
|
@@ -155,6 +167,38 @@ class User < ActiveRecord::Base
|
|
155
167
|
attribute_from_proto :account_id, lambda { |protobuf_message| # Some custom transformation... }
|
156
168
|
end
|
157
169
|
```
|
170
|
+
|
171
|
+
### Setting attributes to nil
|
172
|
+
|
173
|
+
The protocol buffers specification does not allow for the transport of 'null' or 'nil' values for a field. In fact, in order to keep messages small and lightweight this is desireable behavior. Fields are that are not set to a value will not be sent over the wire, but we cannot assume given a message has an absent value for a field that we should set the our attributes to nil.
|
174
|
+
|
175
|
+
In order to solve this problem, Protobuf::ActiveRecord has a convention that tells it when to set an attribute to nil. A message must define a repeated string field named 'nullify'. If an attribute has the same name as an element in the 'nullify' field, this attribute will be set to nil.
|
176
|
+
|
177
|
+
Example:
|
178
|
+
```
|
179
|
+
message UserMessage {
|
180
|
+
optional string name = 1;
|
181
|
+
repeated string nullify = 2;
|
182
|
+
}
|
183
|
+
|
184
|
+
```
|
185
|
+
```ruby
|
186
|
+
m = UserMessage.new(:nullify => [:name])
|
187
|
+
# When Protobuf::ActiveRecord maps this message, it will set the name attribute to nil overwriting any value that is set.
|
188
|
+
```
|
189
|
+
|
190
|
+
For attribute transformers, the field name will not match the attribute name so we need to give the attribute transformer a hint to instruct it on how to nullify a given attribute. When declaring an attribute transformer, you can specify a :nullify_on option. This indicates for the given attribute, if the value of 'nullify_on' is present in the nullify field, set this attribute to nil.
|
191
|
+
|
192
|
+
Example:
|
193
|
+
```Ruby
|
194
|
+
class User < ActiveRecord::Base
|
195
|
+
# When 'account_guid' is present in the nullify array, our 'account_id' attribute will be set to nil
|
196
|
+
attribute_from_proto :account_id, :nullify_on => :account_guid do
|
197
|
+
# transform
|
198
|
+
end
|
199
|
+
end
|
200
|
+
```
|
201
|
+
|
158
202
|
#### Searching
|
159
203
|
|
160
204
|
Protobuf Active Record's `search_scope` method takes the protobuf message and builds ARel scopes from it.
|
@@ -150,10 +150,14 @@ module Protobuf
|
|
150
150
|
|
151
151
|
fields = self.class.protobuf_message.all_fields.map do |field|
|
152
152
|
next if field.nil?
|
153
|
-
next if
|
153
|
+
next if field.deprecated? && exclude_deprecated
|
154
|
+
|
154
155
|
field.name.to_sym
|
155
156
|
end
|
157
|
+
fields += [ options.fetch(:include, nil) ]
|
158
|
+
fields.flatten!
|
156
159
|
fields.compact!
|
160
|
+
fields.uniq!
|
157
161
|
|
158
162
|
fields
|
159
163
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
require 'heredity/inheritable_class_instance_variables'
|
3
|
+
require 'protobuf/active_record/transformer'
|
3
4
|
|
4
5
|
module Protobuf
|
5
6
|
module ActiveRecord
|
@@ -93,20 +94,31 @@ module Protobuf
|
|
93
94
|
# # Do some blocky stuff...
|
94
95
|
# end
|
95
96
|
#
|
96
|
-
|
97
|
-
|
97
|
+
# attribute_from_proto :status, lambda { |proto| nil }, :nullify_on => :status
|
98
|
+
# attribute_from_proto :status, :nullify_on => :status do |proto|
|
99
|
+
# nil
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
def attribute_from_proto(attribute, *args, &block)
|
103
|
+
options = args.extract_options!
|
104
|
+
symbol_or_block = args.first || block
|
98
105
|
|
99
|
-
if
|
100
|
-
callable = lambda { |value| self.__send__(
|
106
|
+
if symbol_or_block.is_a?(Symbol)
|
107
|
+
callable = lambda { |value| self.__send__(symbol_or_block, value) }
|
101
108
|
else
|
102
|
-
|
109
|
+
raise AttributeTransformerError unless symbol_or_block.respond_to?(:call)
|
110
|
+
callable = symbol_or_block
|
103
111
|
end
|
104
112
|
|
105
|
-
|
106
|
-
|
113
|
+
if options[:nullify_on]
|
114
|
+
field = protobuf_message.get_field(:nullify)
|
115
|
+
unless field && field.is_a?(::Protobuf::Field::StringField) && field.repeated?
|
116
|
+
::Protobuf::Logging.logger.warn "Message: #{protobuf_message} is not compatible with :nullify_on option"
|
117
|
+
end
|
107
118
|
end
|
108
119
|
|
109
|
-
|
120
|
+
transformer = ::Protobuf::ActiveRecord::Transformer.new(callable, options)
|
121
|
+
_protobuf_attribute_transformers[attribute.to_sym] = transformer
|
110
122
|
end
|
111
123
|
|
112
124
|
|
@@ -120,8 +132,10 @@ module Protobuf
|
|
120
132
|
|
121
133
|
attributes = attribute_fields.inject({}) do |hash, (key, value)|
|
122
134
|
if _protobuf_attribute_transformers.has_key?(key)
|
123
|
-
|
135
|
+
transformer = _protobuf_attribute_transformers[key]
|
136
|
+
attribute = transformer.call(proto)
|
124
137
|
hash[key] = attribute unless attribute.nil?
|
138
|
+
hash[key] = nil if transformer.nullify?(proto)
|
125
139
|
else
|
126
140
|
hash[key] = _protobuf_convert_fields_to_attributes(key, value)
|
127
141
|
end
|
@@ -129,6 +143,12 @@ module Protobuf
|
|
129
143
|
hash
|
130
144
|
end
|
131
145
|
|
146
|
+
return attributes unless proto.field?(:nullify) && proto.nullify.is_a?(Array)
|
147
|
+
|
148
|
+
proto.nullify.each do |attribute_name|
|
149
|
+
attributes[attribute_name.to_sym] = nil if attribute_names.include?(attribute_name.to_s)
|
150
|
+
end
|
151
|
+
|
132
152
|
attributes
|
133
153
|
end
|
134
154
|
|
@@ -155,4 +175,4 @@ module Protobuf
|
|
155
175
|
end
|
156
176
|
end
|
157
177
|
end
|
158
|
-
end
|
178
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module ActiveRecord
|
3
|
+
class Transformer
|
4
|
+
attr_accessor :callable, :options
|
5
|
+
|
6
|
+
def initialize(callable, options = {})
|
7
|
+
@callable = callable
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(proto)
|
12
|
+
callable.call(proto)
|
13
|
+
end
|
14
|
+
|
15
|
+
def nullify?(proto)
|
16
|
+
return false unless options[:nullify_on]
|
17
|
+
return false unless proto.field?(:nullify) && proto.nullify.is_a?(Array)
|
18
|
+
|
19
|
+
proto.nullify.include?(options[:nullify_on].to_s)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -30,8 +30,8 @@ Gem::Specification.new do |spec|
|
|
30
30
|
# Development dependencies
|
31
31
|
#
|
32
32
|
spec.add_development_dependency "rake"
|
33
|
-
spec.add_development_dependency "rspec"
|
34
|
-
spec.add_development_dependency "rspec-pride"
|
33
|
+
spec.add_development_dependency "rspec", ">= 3.3.0"
|
34
|
+
spec.add_development_dependency "rspec-pride", ">= 3.1.0"
|
35
35
|
spec.add_development_dependency "pry-nav"
|
36
36
|
spec.add_development_dependency "simplecov"
|
37
37
|
spec.add_development_dependency "sqlite3"
|
@@ -19,31 +19,30 @@ describe Protobuf::ActiveRecord::Columns do
|
|
19
19
|
}
|
20
20
|
|
21
21
|
it "maps columns by name" do
|
22
|
-
User._protobuf_columns.
|
22
|
+
expect(User._protobuf_columns).to eq expected_column_names
|
23
23
|
end
|
24
24
|
|
25
25
|
it "maps column names by column type" do
|
26
|
-
User._protobuf_column_types.
|
26
|
+
expect(User._protobuf_column_types).to eq expected_column_types
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
context "column type predicates" do
|
32
|
-
before { User.
|
33
|
-
after { User.unstub(:_protobuf_column_types) }
|
32
|
+
before { allow(User).to receive(:_protobuf_column_types).and_return(Hash.new) }
|
34
33
|
|
35
34
|
describe "._protobuf_date_column?" do
|
36
35
|
before { User._protobuf_column_types[:date] = [ :foo_date ] }
|
37
36
|
|
38
37
|
context "when the column type is :date" do
|
39
38
|
it "is true" do
|
40
|
-
User._protobuf_date_column?(:foo_date).
|
39
|
+
expect(User._protobuf_date_column?(:foo_date)).to be true
|
41
40
|
end
|
42
41
|
end
|
43
42
|
|
44
43
|
context "when the column type is not :date" do
|
45
44
|
it "is false" do
|
46
|
-
User._protobuf_date_column?(:bar_date).
|
45
|
+
expect(User._protobuf_date_column?(:bar_date)).to be false
|
47
46
|
end
|
48
47
|
end
|
49
48
|
end
|
@@ -53,13 +52,13 @@ describe Protobuf::ActiveRecord::Columns do
|
|
53
52
|
|
54
53
|
context "when the column type is :datetime" do
|
55
54
|
it "is true" do
|
56
|
-
User._protobuf_datetime_column?(:foo_datetime).
|
55
|
+
expect(User._protobuf_datetime_column?(:foo_datetime)).to be true
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
59
|
context "when the column type is not :datetime" do
|
61
60
|
it "is false" do
|
62
|
-
User._protobuf_datetime_column?(:bar_datetime).
|
61
|
+
expect(User._protobuf_datetime_column?(:bar_datetime)).to be false
|
63
62
|
end
|
64
63
|
end
|
65
64
|
end
|
@@ -69,13 +68,13 @@ describe Protobuf::ActiveRecord::Columns do
|
|
69
68
|
|
70
69
|
context "when the column type is :time" do
|
71
70
|
it "is true" do
|
72
|
-
User._protobuf_time_column?(:foo_time).
|
71
|
+
expect(User._protobuf_time_column?(:foo_time)).to be true
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
76
75
|
context "when the column type is not :time" do
|
77
76
|
it "is false" do
|
78
|
-
User._protobuf_time_column?(:bar_time).
|
77
|
+
expect(User._protobuf_time_column?(:bar_time)).to be false
|
79
78
|
end
|
80
79
|
end
|
81
80
|
end
|
@@ -85,13 +84,13 @@ describe Protobuf::ActiveRecord::Columns do
|
|
85
84
|
|
86
85
|
context "when the column type is :timestamp" do
|
87
86
|
it "is true" do
|
88
|
-
User._protobuf_timestamp_column?(:foo_timestamp).
|
87
|
+
expect(User._protobuf_timestamp_column?(:foo_timestamp)).to be true
|
89
88
|
end
|
90
89
|
end
|
91
90
|
|
92
91
|
context "when the column type is not :timestamp" do
|
93
92
|
it "is false" do
|
94
|
-
User._protobuf_timestamp_column?(:bar_timestamp).
|
93
|
+
expect(User._protobuf_timestamp_column?(:bar_timestamp)).to be false
|
95
94
|
end
|
96
95
|
end
|
97
96
|
end
|
@@ -8,24 +8,24 @@ describe Protobuf::ActiveRecord::Persistence do
|
|
8
8
|
|
9
9
|
describe ".create" do
|
10
10
|
it "accepts a protobuf message" do
|
11
|
-
User.
|
11
|
+
expect_any_instance_of(User).to receive(:save)
|
12
12
|
User.create(proto)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "accepts a hash" do
|
16
|
-
User.
|
16
|
+
expect_any_instance_of(User).to receive(:save)
|
17
17
|
User.create(user_attributes)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
describe ".create!" do
|
22
22
|
it "accepts a protobuf message" do
|
23
|
-
User.
|
23
|
+
expect_any_instance_of(User).to receive(:save!)
|
24
24
|
User.create!(proto)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "accepts a hash" do
|
28
|
-
User.
|
28
|
+
expect_any_instance_of(User).to receive(:save!)
|
29
29
|
User.create!(user_attributes)
|
30
30
|
end
|
31
31
|
end
|
@@ -35,35 +35,35 @@ describe Protobuf::ActiveRecord::Persistence do
|
|
35
35
|
|
36
36
|
it "accepts a protobuf message" do
|
37
37
|
user.assign_attributes(proto)
|
38
|
-
user.changed
|
38
|
+
expect(user.changed?).to be true
|
39
39
|
end
|
40
40
|
|
41
41
|
it "accepts a hash" do
|
42
42
|
user.assign_attributes(user_attributes)
|
43
|
-
user.changed
|
43
|
+
expect(user.changed?).to be true
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
describe "#update_attributes" do
|
48
48
|
it "accepts a protobuf message" do
|
49
|
-
User.
|
49
|
+
expect_any_instance_of(User).to receive(:save)
|
50
50
|
user.update_attributes(proto)
|
51
51
|
end
|
52
52
|
|
53
53
|
it "accepts a hash" do
|
54
|
-
User.
|
54
|
+
expect_any_instance_of(User).to receive(:save)
|
55
55
|
user.update_attributes(user_attributes)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
describe "#update_attributes!" do
|
60
60
|
it "accepts a protobuf message" do
|
61
|
-
User.
|
61
|
+
expect_any_instance_of(User).to receive(:save!)
|
62
62
|
user.update_attributes!(proto)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "accepts a hash" do
|
66
|
-
User.
|
66
|
+
expect_any_instance_of(User).to receive(:save!)
|
67
67
|
user.update_attributes!(user_attributes)
|
68
68
|
end
|
69
69
|
end
|
@@ -16,24 +16,24 @@ describe Protobuf::ActiveRecord::Scope do
|
|
16
16
|
let(:request) { UserSearchMessage.new(:guid => ["foo"], :email => ["foo@test.co"]) }
|
17
17
|
|
18
18
|
before {
|
19
|
-
User.
|
19
|
+
allow(User).to receive(:searchable_field_parsers).and_return({ :email => proc { |val| val } })
|
20
20
|
}
|
21
21
|
|
22
22
|
it "builds scopes for searchable fields" do
|
23
|
-
User.
|
24
|
-
User.search_scope(request).
|
23
|
+
allow(User).to receive(:searchable_fields).and_return({ :email => :by_email })
|
24
|
+
expect(User.search_scope(request)).to eq User.by_email("foo@test.co")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "is chainable" do
|
28
|
-
User.limit(1).search_scope(request).order(:email).
|
28
|
+
expect(User.limit(1).search_scope(request).order(:email)).to eq User.limit(1).order(:email)
|
29
29
|
end
|
30
30
|
|
31
31
|
context "when a searchable field does not have a value" do
|
32
32
|
let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) }
|
33
33
|
|
34
34
|
it "doesn't build a scope from that field" do
|
35
|
-
User.
|
36
|
-
User.search_scope(request).
|
35
|
+
allow(User).to receive(:searchable_fields).and_return({ :email => :by_email })
|
36
|
+
expect(User.search_scope(request)).to eq User.by_email("foo@test.co")
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -41,7 +41,7 @@ describe Protobuf::ActiveRecord::Scope do
|
|
41
41
|
let(:request) { UserSearchMessage.new(:email => ["foo@test.co"]) }
|
42
42
|
|
43
43
|
it "raises an exception" do
|
44
|
-
User.
|
44
|
+
allow(User).to receive(:searchable_fields).and_return({ :email => :by_hullabaloo })
|
45
45
|
expect { User.search_scope(request) }.to raise_exception(/Undefined scope :by_hullabaloo/)
|
46
46
|
end
|
47
47
|
end
|
@@ -51,28 +51,28 @@ describe Protobuf::ActiveRecord::Scope do
|
|
51
51
|
context "when :scope is not defined" do
|
52
52
|
it "defines the given field as searchable using the `by_[:field]` as the scope" do
|
53
53
|
User.field_scope :guid
|
54
|
-
User.searchable_fields[:guid].
|
54
|
+
expect(User.searchable_fields[:guid]).to eq :by_guid
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
context "when :scope is defined" do
|
59
59
|
it "defines the given field as searchable using the given :scope" do
|
60
60
|
User.field_scope :guid, :scope => :custom_scope
|
61
|
-
User.searchable_fields[:guid].
|
61
|
+
expect(User.searchable_fields[:guid]).to eq :custom_scope
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
context "when :parser is not defined" do
|
66
66
|
it "doesn't define the given field as parseable" do
|
67
67
|
User.field_scope :guid
|
68
|
-
User.searchable_field_parsers[:guid].
|
68
|
+
expect(User.searchable_field_parsers[:guid]).to eq nil
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
context "when :parser is defined" do
|
73
73
|
it "defines the given field as parseable using the given :parser" do
|
74
74
|
User.field_scope :guid, :parser => :parser
|
75
|
-
User.searchable_field_parsers[:guid].
|
75
|
+
expect(User.searchable_field_parsers[:guid]).to eq :parser
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
@@ -83,7 +83,7 @@ describe Protobuf::ActiveRecord::Scope do
|
|
83
83
|
proto = UserMessage.new(:email => "the.email@test.in")
|
84
84
|
|
85
85
|
User.field_scope :email
|
86
|
-
User.parse_search_values(proto, :email).
|
86
|
+
expect(User.parse_search_values(proto, :email)).to eq ["the.email@test.in"]
|
87
87
|
end
|
88
88
|
|
89
89
|
context "when a field parser is defined" do
|
@@ -91,20 +91,11 @@ describe Protobuf::ActiveRecord::Scope do
|
|
91
91
|
|
92
92
|
let(:proto) { UserSearchMessage.new(:guid => ["foo"]) }
|
93
93
|
|
94
|
-
context "and the parser responds to :to_sym" do
|
95
|
-
let(:parser) { double('parser', :to_sym => :parser_to_sym) }
|
96
|
-
|
97
|
-
it "passes the value to the parser" do
|
98
|
-
User.should_receive(:parser_to_sym).with([ "foo" ])
|
99
|
-
User.parse_search_values(proto, :guid)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
94
|
context "and the parser does not respond to :to_sym" do
|
104
95
|
let(:parser) { double('parser') }
|
105
96
|
|
106
97
|
it "passes the value to the parser" do
|
107
|
-
parser.
|
98
|
+
expect(parser).to receive(:call).with(["foo"])
|
108
99
|
User.parse_search_values(proto, :guid)
|
109
100
|
end
|
110
101
|
end
|
@@ -121,7 +112,7 @@ describe Protobuf::ActiveRecord::Scope do
|
|
121
112
|
end
|
122
113
|
|
123
114
|
proto = TheMessage.new(:the_enum_value => TheEnum::VALUE)
|
124
|
-
User.parse_search_values(proto, :the_enum_value)[0].
|
115
|
+
expect(User.parse_search_values(proto, :the_enum_value)[0]).to be 1
|
125
116
|
end
|
126
117
|
end
|
127
118
|
end
|
@@ -13,10 +13,10 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
13
13
|
let(:date) { Date.current }
|
14
14
|
let(:integer) { date.to_time.to_i }
|
15
15
|
|
16
|
-
before { User.
|
16
|
+
before { allow(User).to receive(:_protobuf_date_column?).and_return(true) }
|
17
17
|
|
18
18
|
it "converts the given value to an integer" do
|
19
|
-
User._protobuf_convert_attributes_to_fields(:foo_date, date).
|
19
|
+
expect(User._protobuf_convert_attributes_to_fields(:foo_date, date)).to eq integer
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -24,10 +24,10 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
24
24
|
let(:datetime) { DateTime.current }
|
25
25
|
let(:integer) { datetime.to_time.to_i }
|
26
26
|
|
27
|
-
before { User.
|
27
|
+
before { allow(User).to receive(:_protobuf_datetime_column?).and_return(true) }
|
28
28
|
|
29
29
|
it "converts the given value to an integer" do
|
30
|
-
User._protobuf_convert_attributes_to_fields(:foo_datetime, datetime).
|
30
|
+
expect(User._protobuf_convert_attributes_to_fields(:foo_datetime, datetime)).to eq integer
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -35,10 +35,10 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
35
35
|
let(:time) { Time.current }
|
36
36
|
let(:integer) { time.to_time.to_i }
|
37
37
|
|
38
|
-
before { User.
|
38
|
+
before { allow(User).to receive(:_protobuf_time_column?).and_return(true) }
|
39
39
|
|
40
40
|
it "converts the given value to an integer" do
|
41
|
-
User._protobuf_convert_attributes_to_fields(:foo_time, time).
|
41
|
+
expect(User._protobuf_convert_attributes_to_fields(:foo_time, time)).to eq integer
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -46,10 +46,10 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
46
46
|
let(:timestamp) { Time.current }
|
47
47
|
let(:integer) { timestamp.to_time.to_i }
|
48
48
|
|
49
|
-
before { User.
|
49
|
+
before { allow(User).to receive(:_protobuf_timestamp_column?).and_return(true) }
|
50
50
|
|
51
51
|
it "converts the given value to an integer" do
|
52
|
-
User._protobuf_convert_attributes_to_fields(:foo_timestamp, timestamp).
|
52
|
+
expect(User._protobuf_convert_attributes_to_fields(:foo_timestamp, timestamp)).to eq integer
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -57,7 +57,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
57
57
|
let(:value) { "Foo" }
|
58
58
|
|
59
59
|
it "returns the given value" do
|
60
|
-
User._protobuf_convert_attributes_to_fields(:foo, value).
|
60
|
+
expect(User._protobuf_convert_attributes_to_fields(:foo, value)).to eq value
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -69,7 +69,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
69
69
|
before { User.field_from_record :first_name, :extract_first_name }
|
70
70
|
|
71
71
|
it "creates a callable method object from the converter" do
|
72
|
-
User.
|
72
|
+
expect(User).to receive(:extract_first_name)
|
73
73
|
User._protobuf_field_transformers[:first_name].call(1)
|
74
74
|
end
|
75
75
|
end
|
@@ -84,12 +84,13 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
84
84
|
let(:callable) { lambda { |proto| nil } }
|
85
85
|
|
86
86
|
before {
|
87
|
-
User.
|
87
|
+
allow(User).to receive(:_protobuf_field_transformers).and_return(Hash.new)
|
88
88
|
User.field_from_record :account_id, callable
|
89
89
|
}
|
90
90
|
|
91
|
-
it "adds the given converter to the list of protobuf field transformers" do
|
91
|
+
it "adds the given converter to the list of protobuf field transformers", :pending => 'missing expectation?' do
|
92
92
|
User._protobuf_field_transformers[:account_id] = callable
|
93
|
+
fail
|
93
94
|
end
|
94
95
|
end
|
95
96
|
end
|
@@ -102,18 +103,18 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
102
103
|
|
103
104
|
context "given a value" do
|
104
105
|
it "defines #to_proto" do
|
105
|
-
User.allocate.
|
106
|
+
expect(User.allocate).to respond_to :to_proto
|
106
107
|
end
|
107
108
|
end
|
108
109
|
|
109
110
|
context "given options" do
|
110
111
|
it "merges them with protobuf field options" do
|
111
|
-
User._protobuf_field_options.
|
112
|
+
expect(User._protobuf_field_options).to eq options
|
112
113
|
end
|
113
114
|
end
|
114
115
|
|
115
116
|
it "returns the protobuf message for this object" do
|
116
|
-
User.protobuf_message.
|
117
|
+
expect(User.protobuf_message).to eq protobuf_message
|
117
118
|
end
|
118
119
|
end
|
119
120
|
|
@@ -126,28 +127,35 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
126
127
|
describe "#_filter_field_attributes" do
|
127
128
|
context "when options has :only" do
|
128
129
|
it "only returns the given field(s)" do
|
129
|
-
fields = user._filter_field_attributes(:only => :name)
|
130
|
-
fields.
|
130
|
+
fields = user._filter_field_attributes(:only => :name)
|
131
|
+
expect(fields).to eq [ :name ]
|
131
132
|
end
|
132
133
|
end
|
133
134
|
|
134
135
|
context "when options has :except" do
|
135
136
|
it "returns all except the given field(s)" do
|
136
|
-
fields = user._filter_field_attributes(:except => :name)
|
137
|
-
fields.
|
137
|
+
fields = user._filter_field_attributes(:except => :name)
|
138
|
+
expect(fields).to eq [ :guid, :email, :email_domain, :password, :nullify ]
|
138
139
|
end
|
139
140
|
end
|
140
141
|
end
|
141
142
|
|
142
143
|
describe "#_filtered_fields" do
|
143
144
|
it "returns protobuf fields" do
|
144
|
-
user._filtered_fields.
|
145
|
+
expect(user._filtered_fields).to eq [ :guid, :name, :email, :email_domain, :password, :nullify ]
|
145
146
|
end
|
146
147
|
|
147
148
|
context "given :deprecated => false" do
|
148
149
|
it "filters all deprecated fields" do
|
149
|
-
fields = user._filtered_fields(:deprecated => false)
|
150
|
-
fields.
|
150
|
+
fields = user._filtered_fields(:deprecated => false)
|
151
|
+
expect(fields).to eq [ :guid, :name, :email, :password, :nullify ]
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'and :include => :email_domain' do
|
155
|
+
it 'includes deprecated fields that have been explicitly specified' do
|
156
|
+
fields = user._filtered_fields(:deprecated => false, :include => :email_domain)
|
157
|
+
expect(fields).to match_array([ :guid, :name, :email, :email_domain, :password, :nullify ])
|
158
|
+
end
|
151
159
|
end
|
152
160
|
end
|
153
161
|
end
|
@@ -159,7 +167,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
159
167
|
before { User.protobuf_message(protobuf_message, options) }
|
160
168
|
|
161
169
|
it "returns the class's protobuf field options" do
|
162
|
-
User.allocate._normalize_options({}).
|
170
|
+
expect(User.allocate._normalize_options({})).to eq options
|
163
171
|
end
|
164
172
|
end
|
165
173
|
|
@@ -168,7 +176,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
168
176
|
|
169
177
|
it "merges them with the class's protobuf field options" do
|
170
178
|
normalized_options = User.allocate._normalize_options(options)
|
171
|
-
normalized_options[:only].
|
179
|
+
expect(normalized_options[:only]).to eq options[:only]
|
172
180
|
end
|
173
181
|
end
|
174
182
|
|
@@ -177,7 +185,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
177
185
|
|
178
186
|
it "ensures that :except exists" do
|
179
187
|
normalized_options = User.allocate._normalize_options(options)
|
180
|
-
normalized_options[:except].
|
188
|
+
expect(normalized_options[:except]).to eq []
|
181
189
|
end
|
182
190
|
end
|
183
191
|
|
@@ -188,7 +196,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
188
196
|
|
189
197
|
it "ensures that :only exists" do
|
190
198
|
normalized_options = User.allocate._normalize_options(options)
|
191
|
-
normalized_options[:only].
|
199
|
+
expect(normalized_options[:only]).to eq []
|
192
200
|
end
|
193
201
|
end
|
194
202
|
end
|
@@ -204,27 +212,27 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
204
212
|
}
|
205
213
|
|
206
214
|
context "when a transformer is defined for the field" do
|
207
|
-
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => 'test.co', :password => nil } }
|
215
|
+
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => 'test.co', :password => nil, :nullify => nil } }
|
208
216
|
let(:transformer) { { :email_domain => lambda { |record| record.email.split('@').last } } }
|
209
217
|
|
210
218
|
before {
|
211
|
-
User.
|
219
|
+
allow(User).to receive(:_protobuf_field_transformers).and_return(transformer)
|
212
220
|
}
|
213
221
|
|
214
222
|
it "gets the field from the transformer" do
|
215
|
-
user.fields_from_record.
|
223
|
+
expect(user.fields_from_record).to eq fields_from_record
|
216
224
|
end
|
217
225
|
end
|
218
226
|
|
219
227
|
context "when a transformer is not defined for the field" do
|
220
|
-
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => nil, :password => nil } }
|
228
|
+
let(:fields_from_record) { { :guid => user.guid, :name => user.name, :email => user.email, :email_domain => nil, :password => nil, :nullify => nil } }
|
221
229
|
|
222
230
|
it "returns a hash of protobuf fields that this object has getters for" do
|
223
|
-
user.fields_from_record.
|
231
|
+
expect(user.fields_from_record).to eq fields_from_record
|
224
232
|
end
|
225
233
|
|
226
234
|
it "converts attributes values for protobuf messages" do
|
227
|
-
user.
|
235
|
+
expect(user).to receive(:_protobuf_convert_attributes_to_fields).at_least(:once)
|
228
236
|
user.fields_from_record
|
229
237
|
end
|
230
238
|
end
|
@@ -232,7 +240,7 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
232
240
|
context "given options with :include" do
|
233
241
|
it "adds the given field to the list of serialized fields" do
|
234
242
|
fields = user.fields_from_record(:include => :token)
|
235
|
-
fields.include
|
243
|
+
expect(fields).to include(:token)
|
236
244
|
end
|
237
245
|
end
|
238
246
|
end
|
@@ -242,10 +250,10 @@ describe Protobuf::ActiveRecord::Serialization do
|
|
242
250
|
let(:proto) { protobuf_message.new(proto_hash) }
|
243
251
|
let(:proto_hash) { { :name => "foo" } }
|
244
252
|
|
245
|
-
before { user.
|
253
|
+
before { allow(user).to receive(:fields_from_record).and_return(proto_hash) }
|
246
254
|
|
247
255
|
it "intializes a new protobuf message with attributes from #to_proto_hash" do
|
248
|
-
user.to_proto.
|
256
|
+
expect(user.to_proto).to eq proto
|
249
257
|
end
|
250
258
|
end
|
251
259
|
|
@@ -9,23 +9,23 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
9
9
|
describe "._filter_attribute_fields" do
|
10
10
|
it "includes fields that have values" do
|
11
11
|
attribute_fields = User._filter_attribute_fields(proto)
|
12
|
-
attribute_fields[:email].
|
12
|
+
expect(attribute_fields[:email]).to eq proto_hash[:email]
|
13
13
|
end
|
14
14
|
|
15
15
|
it "filters repeated fields" do
|
16
16
|
attribute_fields = User._filter_attribute_fields(proto)
|
17
|
-
attribute_fields.has_key?(:tags).
|
17
|
+
expect(attribute_fields.has_key?(:tags)).to be false
|
18
18
|
end
|
19
19
|
|
20
20
|
it "includes attributes that aren't fields, but have attribute transformers" do
|
21
|
-
User.
|
21
|
+
allow(User).to receive(:_protobuf_attribute_transformers).and_return({ :account_id => :fetch_account_id })
|
22
22
|
attribute_fields = User._filter_attribute_fields(proto)
|
23
|
-
attribute_fields.has_key?(:account_id).
|
23
|
+
expect(attribute_fields.has_key?(:account_id)).to be true
|
24
24
|
end
|
25
25
|
|
26
26
|
it "includes fields that aren't attributes, but have attribute transformers" do
|
27
27
|
attribute_fields = User._filter_attribute_fields(proto)
|
28
|
-
attribute_fields.has_key?(:password).
|
28
|
+
expect(attribute_fields.has_key?(:password)).to be true
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -34,10 +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 { User.
|
37
|
+
before { allow(User).to receive(:_protobuf_date_column?).and_return(true) }
|
38
38
|
|
39
39
|
it "converts the given value to a Date object" do
|
40
|
-
User._protobuf_convert_fields_to_attributes(:foo_date, value).
|
40
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_date, value)).to eq date
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -45,14 +45,14 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
45
45
|
let(:datetime) { DateTime.current }
|
46
46
|
let(:value) { datetime.to_i }
|
47
47
|
|
48
|
-
before { User.
|
48
|
+
before { allow(User).to receive(:_protobuf_datetime_column?).and_return(true) }
|
49
49
|
|
50
50
|
it "converts the given value to a DateTime object" do
|
51
|
-
User._protobuf_convert_fields_to_attributes(:foo_datetime, value).
|
51
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_datetime, value)).to be_a(DateTime)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "converts the given value to a DateTime object of the same value" do
|
55
|
-
User._protobuf_convert_fields_to_attributes(:foo_datetime, value).
|
55
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_datetime, value)).to be_within(1).of(datetime)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -60,14 +60,14 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
60
60
|
let(:time) { Time.current }
|
61
61
|
let(:value) { time.to_i }
|
62
62
|
|
63
|
-
before { User.
|
63
|
+
before { allow(User).to receive(:_protobuf_time_column?).and_return(true) }
|
64
64
|
|
65
65
|
it "converts the given value to a Time object" do
|
66
|
-
User._protobuf_convert_fields_to_attributes(:foo_time, value).
|
66
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_time, value)).to be_a(Time)
|
67
67
|
end
|
68
68
|
|
69
69
|
it "converts the given value to a Time object of the same value" do
|
70
|
-
User._protobuf_convert_fields_to_attributes(:foo_time, value).
|
70
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_time, value)).to be_within(1).of(time)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -75,14 +75,14 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
75
75
|
let(:time) { Time.current }
|
76
76
|
let(:value) { time.to_i }
|
77
77
|
|
78
|
-
before { User.
|
78
|
+
before { allow(User).to receive(:_protobuf_timestamp_column?).and_return(true) }
|
79
79
|
|
80
80
|
it "converts the given value to a Time object" do
|
81
|
-
User._protobuf_convert_fields_to_attributes(:foo_time, value).
|
81
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_time, value)).to be_a(Time)
|
82
82
|
end
|
83
83
|
|
84
84
|
it "converts the given value to a Time object of the same value" do
|
85
|
-
User._protobuf_convert_fields_to_attributes(:foo_timestamp, value).
|
85
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo_timestamp, value)).to be_within(1).of(time)
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -90,57 +90,80 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
90
90
|
let(:value) { "Foo" }
|
91
91
|
|
92
92
|
it "returns the given value" do
|
93
|
-
User._protobuf_convert_fields_to_attributes(:foo, value).
|
93
|
+
expect(User._protobuf_convert_fields_to_attributes(:foo, value)).to eq value
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
98
|
describe ".attributes_from_proto" do
|
99
|
+
let(:callable) { lambda { |proto| 1 } }
|
100
|
+
let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable) }
|
101
|
+
|
99
102
|
context "when a transformer is defined for the attribute" do
|
100
103
|
it "transforms the field value" do
|
101
104
|
attribute_fields = User.attributes_from_proto(proto)
|
102
|
-
attribute_fields[:first_name].
|
105
|
+
expect(attribute_fields[:first_name]).to eq user_attributes[:first_name]
|
103
106
|
end
|
104
107
|
end
|
105
108
|
|
106
109
|
context "when a transformer is a callable that returns nil" do
|
110
|
+
let(:callable) { lambda { |proto| nil } }
|
111
|
+
|
112
|
+
before do
|
113
|
+
transformers = User._protobuf_attribute_transformers
|
114
|
+
allow(User).to receive(:_protobuf_attribute_transformers).and_return(
|
115
|
+
{:account_id => transformer}.merge(transformers)
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "does not set the attribute" do
|
120
|
+
attribute_fields = User.attributes_from_proto(proto)
|
121
|
+
expect(attribute_fields).to eq user_attributes
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'when the transformer has a nullify_on option' do
|
126
|
+
let(:callable) { lambda { |proto| nil } }
|
127
|
+
let(:transformer) { ::Protobuf::ActiveRecord::Transformer.new(callable, :nullify_on => :account_id) }
|
128
|
+
let(:proto_hash) { { :name => 'foo bar', :email => 'foo@test.co', :nullify => [:account_id] } }
|
129
|
+
|
107
130
|
before do
|
108
131
|
transformers = User._protobuf_attribute_transformers
|
109
|
-
User.
|
110
|
-
{:account_id =>
|
132
|
+
allow(User).to receive(:_protobuf_attribute_transformers).and_return(
|
133
|
+
{:account_id => transformer}.merge(transformers)
|
111
134
|
)
|
112
135
|
end
|
113
136
|
|
114
137
|
it "does not set the attribute" do
|
115
138
|
attribute_fields = User.attributes_from_proto(proto)
|
116
|
-
attribute_fields.
|
139
|
+
expect(attribute_fields).to include(:account_id => nil)
|
117
140
|
end
|
118
141
|
end
|
119
142
|
|
120
143
|
context "when a transformer is a callable that returns a value" do
|
121
144
|
before do
|
122
145
|
transformers = User._protobuf_attribute_transformers
|
123
|
-
User.
|
124
|
-
{:account_id =>
|
146
|
+
allow(User).to receive(:_protobuf_attribute_transformers).and_return(
|
147
|
+
{:account_id => transformer}.merge(transformers)
|
125
148
|
)
|
126
149
|
end
|
127
150
|
|
128
151
|
it "sets the attribute" do
|
129
152
|
attribute_fields = User.attributes_from_proto(proto)
|
130
|
-
attribute_fields.
|
153
|
+
expect(attribute_fields).to eq user_attributes.merge(:account_id => 1)
|
131
154
|
end
|
132
155
|
end
|
133
156
|
|
134
157
|
context "when a transformer is not defined for the attribute" do
|
135
158
|
before {
|
136
|
-
User.
|
159
|
+
allow(User).to receive(:_protobuf_convert_fields_to_attributes) do |key, value|
|
137
160
|
value
|
138
161
|
end
|
139
162
|
}
|
140
163
|
|
141
164
|
it "converts the field value" do
|
142
165
|
attribute_fields = User.attributes_from_proto(proto)
|
143
|
-
attribute_fields.
|
166
|
+
expect(attribute_fields).to eq user_attributes
|
144
167
|
end
|
145
168
|
end
|
146
169
|
end
|
@@ -152,7 +175,7 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
152
175
|
before { User.attribute_from_proto :first_name, :extract_first_name }
|
153
176
|
|
154
177
|
it "creates a callable method object from the converter" do
|
155
|
-
User.
|
178
|
+
expect(User).to receive(:extract_first_name)
|
156
179
|
User._protobuf_attribute_transformers[:first_name].call(1)
|
157
180
|
end
|
158
181
|
end
|
@@ -167,12 +190,13 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
167
190
|
let(:callable) { lambda { |proto| nil } }
|
168
191
|
|
169
192
|
before {
|
170
|
-
User.
|
193
|
+
allow(User).to receive(:_protobuf_attribute_transformers).and_return(Hash.new)
|
171
194
|
User.attribute_from_proto :account_id, callable
|
172
195
|
}
|
173
196
|
|
174
|
-
it "adds the given converter to the list of protobuf field transformers" do
|
197
|
+
it "adds the given converter to the list of protobuf field transformers", :pending => 'missing expectation?' do
|
175
198
|
User._protobuf_attribute_transformers[:account_id] = callable
|
199
|
+
fail
|
176
200
|
end
|
177
201
|
end
|
178
202
|
end
|
@@ -183,7 +207,7 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
183
207
|
|
184
208
|
it "initializes a new Date object from the value" do
|
185
209
|
Timecop.freeze(Date.current) do
|
186
|
-
User.convert_int64_to_date(int64).
|
210
|
+
expect(User.convert_int64_to_date(int64)).to eq date
|
187
211
|
end
|
188
212
|
end
|
189
213
|
end
|
@@ -196,7 +220,7 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
196
220
|
Timecop.freeze(DateTime.current) do
|
197
221
|
expected_datetime = Time.at(datetime.to_i)
|
198
222
|
converted_datetime = User.convert_int64_to_datetime(int64)
|
199
|
-
converted_datetime.
|
223
|
+
expect(converted_datetime).to eq expected_datetime
|
200
224
|
end
|
201
225
|
end
|
202
226
|
end
|
@@ -207,14 +231,14 @@ describe Protobuf::ActiveRecord::Transformation do
|
|
207
231
|
|
208
232
|
it "initializes a new Time object from the value" do
|
209
233
|
Timecop.freeze(Time.current) do
|
210
|
-
User.convert_int64_to_time(int64).
|
234
|
+
expect(User.convert_int64_to_time(int64)).to be_within(1).of(time)
|
211
235
|
end
|
212
236
|
end
|
213
237
|
end
|
214
238
|
|
215
239
|
describe "#attributes_from_proto" do
|
216
240
|
it "gets attributes from the given protobuf message" do
|
217
|
-
User.
|
241
|
+
expect(User).to receive(:attributes_from_proto).with(proto)
|
218
242
|
user.attributes_from_proto(proto)
|
219
243
|
end
|
220
244
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::Protobuf::ActiveRecord::Transformer do
|
4
|
+
let(:callable) { lambda { |proto| proto.name } }
|
5
|
+
let(:proto) { ::UserMessage.new(:name => 'test', :nullify => ['name']) }
|
6
|
+
let(:options) { {} }
|
7
|
+
|
8
|
+
subject { described_class.new(callable, options) }
|
9
|
+
|
10
|
+
describe '#call' do
|
11
|
+
it 'calls the callable' do
|
12
|
+
result = subject.call(proto)
|
13
|
+
expect(result).to eq('test')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#nullify?' do
|
18
|
+
context 'no nullify_on set' do
|
19
|
+
it 'returns false' do
|
20
|
+
expect(subject.nullify?(proto)).to eq(false)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'nullify_on name' do
|
25
|
+
let(:options) { { :nullify_on => :name } }
|
26
|
+
|
27
|
+
context 'invalid message' do
|
28
|
+
let(:proto) { ::UserSearchMessage.new }
|
29
|
+
|
30
|
+
it 'returns false' do
|
31
|
+
expect(subject.nullify?(proto)).to eq(false)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'valid message' do
|
36
|
+
it 'returns true' do
|
37
|
+
expect(subject.nullify?(proto)).to eq(true)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,4 +10,18 @@ Bundler.require(:default, :development, :test)
|
|
10
10
|
|
11
11
|
require 'support/db'
|
12
12
|
require 'support/models'
|
13
|
-
require 'support/protobuf'
|
13
|
+
require 'support/protobuf'
|
14
|
+
|
15
|
+
# Silence protobuf's logger
|
16
|
+
::Protobuf::Logging.logger.level = ::Logger::FATAL
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
# Turn deprecation warnings into errors with full backtrace.
|
20
|
+
config.raise_errors_for_deprecations!
|
21
|
+
|
22
|
+
# Verifies the existance of any stubbed methods, replaces better_receive and better_stub
|
23
|
+
# https://www.relishapp.com/rspec/rspec-mocks/v/3-1/docs/verifying-doubles/partial-doubles
|
24
|
+
config.mock_with :rspec do |mocks|
|
25
|
+
mocks.verify_partial_doubles = true
|
26
|
+
end
|
27
|
+
end
|
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.
|
4
|
+
version: 3.2.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-
|
11
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -86,28 +86,28 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: 3.3.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 3.3.0
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: rspec-pride
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
103
|
+
version: 3.1.0
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 3.1.0
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: pry-nav
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -174,6 +174,7 @@ extra_rdoc_files: []
|
|
174
174
|
files:
|
175
175
|
- ".gitignore"
|
176
176
|
- ".rspec"
|
177
|
+
- ".travis.yml"
|
177
178
|
- Gemfile
|
178
179
|
- LICENSE.txt
|
179
180
|
- README.md
|
@@ -195,6 +196,7 @@ files:
|
|
195
196
|
- lib/protobuf/active_record/scope.rb
|
196
197
|
- lib/protobuf/active_record/serialization.rb
|
197
198
|
- lib/protobuf/active_record/transformation.rb
|
199
|
+
- lib/protobuf/active_record/transformer.rb
|
198
200
|
- lib/protobuf/active_record/validations.rb
|
199
201
|
- lib/protobuf/active_record/version.rb
|
200
202
|
- protobuf-activerecord.gemspec
|
@@ -203,6 +205,7 @@ files:
|
|
203
205
|
- spec/protobuf/active_record/scope_spec.rb
|
204
206
|
- spec/protobuf/active_record/serialization_spec.rb
|
205
207
|
- spec/protobuf/active_record/transformation_spec.rb
|
208
|
+
- spec/protobuf/active_record/transformer_spec.rb
|
206
209
|
- spec/spec_helper.rb
|
207
210
|
- spec/support/db.rb
|
208
211
|
- spec/support/db/setup.rb
|
@@ -231,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
231
234
|
version: '0'
|
232
235
|
requirements: []
|
233
236
|
rubyforge_project:
|
234
|
-
rubygems_version: 2.4.
|
237
|
+
rubygems_version: 2.4.8
|
235
238
|
signing_key:
|
236
239
|
specification_version: 4
|
237
240
|
summary: Google Protocol Buffers integration for Active Record
|
@@ -241,6 +244,7 @@ test_files:
|
|
241
244
|
- spec/protobuf/active_record/scope_spec.rb
|
242
245
|
- spec/protobuf/active_record/serialization_spec.rb
|
243
246
|
- spec/protobuf/active_record/transformation_spec.rb
|
247
|
+
- spec/protobuf/active_record/transformer_spec.rb
|
244
248
|
- spec/spec_helper.rb
|
245
249
|
- spec/support/db.rb
|
246
250
|
- spec/support/db/setup.rb
|