protobuf-activerecord 3.0.0.rc4 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -49
- data/lib/protobuf/active_record/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3d7459c7c110bd97134b19fcb6e03c50d703cf0
|
4
|
+
data.tar.gz: a9f5db72facad0d01bf4ef55ed1fd21a80f35ac0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fadf115d0f333b94d6bee3940f23b1938ae0fa74ff16c3d1edda12acfcbac70f6c39717de88b716fbf3e51cbe2586ce98812c9a26c5a44abdaf241170a1e9d9
|
7
|
+
data.tar.gz: dfb1883e308d92f1b34cd1e038fbfeebf3b9594f1b35e2ba4a7a5130153c877a892e1d8d48bf9d77d8fb20e880d32e12b42b40e8fe283d6e4242e6c7fb7d2e04
|
data/README.md
CHANGED
@@ -18,22 +18,11 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
Protobuf Active Record
|
22
|
-
|
23
|
-
```Ruby
|
24
|
-
class User < ActiveRecord::Base
|
25
|
-
include Protoable
|
26
|
-
|
27
|
-
# Your awesome methods...
|
28
|
-
#
|
29
|
-
end
|
30
|
-
```
|
31
|
-
|
32
|
-
Now you can pass protobuf messages to your user model just like you would attributes. Protoable will take care of converting the protobuf message to attributes and continue on with Active Record's normal behavior.
|
21
|
+
Protobuf Active Record is automatically available in any Active Record model. Once installed, you can pass protobuf messages to your user model just like you would attributes. It will take care of converting the protobuf message to attributes and continue on with Active Record's normal behavior.
|
33
22
|
|
34
23
|
### Field/Attribute mapping
|
35
24
|
|
36
|
-
Just like Active Record maps database columns to your model's attributes,
|
25
|
+
Just like Active Record maps database columns to your model's attributes, Protobuf Active Record maps protobuf fields to your model's attributes.
|
37
26
|
|
38
27
|
Given a table that looks like this:
|
39
28
|
|
@@ -59,13 +48,13 @@ class UserMessage < ::Protobuf::Message
|
|
59
48
|
end
|
60
49
|
```
|
61
50
|
|
62
|
-
|
51
|
+
Protobuf Active Record will map the `first_name`, `last_name`, `email`, & `account_id` columns, skipping the timestamp columns. Repeated fields and fields that are nil will not be mapped.
|
63
52
|
|
64
53
|
**Dates**
|
65
54
|
|
66
|
-
Since Protocol Buffer messages don't support sending date, time, or datetime fields,
|
55
|
+
Since Protocol Buffer messages don't support sending date, time, or datetime fields, Protobuf Active Record expects date, time, and datetime fields to be sent as integers. Just like Active Record handles translating Ruby dates, times, and datetimes into the proper database column types, Protobuf Active Record will handle converting dates, times, and dateimes to and from integers mapping protobuf message fields.
|
67
56
|
|
68
|
-
Picking up our users table example again, if you wanted to add a `created_at` field to your protobuf message, if you add it as an integer field,
|
57
|
+
Picking up our users table example again, if you wanted to add a `created_at` field to your protobuf message, if you add it as an integer field, Protobuf Active Record will handle the conversions for you:
|
69
58
|
|
70
59
|
```Ruby
|
71
60
|
class UserMessage < ::Protobuf::Message
|
@@ -74,39 +63,35 @@ class UserMessage < ::Protobuf::Message
|
|
74
63
|
optional ::Protobuf::Field::StringField, :email, 3
|
75
64
|
optional ::Protobuf::Field::IntegerField, :account_id, 4
|
76
65
|
|
77
|
-
# Add a datetime field as an integer and
|
66
|
+
# Add a datetime field as an integer and Protobuf Active Record will map it for you
|
78
67
|
optional ::Protobuf::Field::IntegerField, :created_at, 5
|
79
68
|
end
|
80
69
|
```
|
81
70
|
|
82
71
|
### Creating/Updating
|
83
72
|
|
84
|
-
|
73
|
+
Protobuf Active Record doesn't alter Active Record's normal persistence methods. It simply adds to ability to pass protobuf messages to them in place of an attributes hash.
|
85
74
|
|
86
75
|
### Serialization to protobuf
|
87
76
|
|
88
|
-
In addition to mapping protobuf message fields to Active Record objects when creating or updating records,
|
77
|
+
In addition to mapping protobuf message fields to Active Record objects when creating or updating records, Active Record objects can also be serialized to protobuf messages. Simply specify the protobuf message that should be used and Protobuf Active Record will take care of the rest:
|
89
78
|
|
90
79
|
```Ruby
|
91
80
|
class User < ActiveRecord::Base
|
92
|
-
|
93
|
-
|
94
|
-
# Configures Protoable to use the UserMessage class and adds :to_proto.
|
81
|
+
# Configures Protobuf Active Record to use the UserMessage class and adds :to_proto.
|
95
82
|
protobuf_message :user_message
|
96
83
|
end
|
97
84
|
```
|
98
85
|
|
99
|
-
Once the desired protobuf message has been specified,
|
86
|
+
Once the desired protobuf message has been specified, a `to_proto` method will be added to the model. Calling `to_proto` will automatically convert the model to the specified protobuf message using the same attribute to field mapping it uses to create and update objects from protobuf messages.
|
100
87
|
|
101
88
|
### Choosing serializable fields
|
102
89
|
|
103
|
-
|
90
|
+
Protobuf Active Record also provides a mechanism for choosing which fields should be included when serializing objects to protobuf messages by passing additional options to `protobuf_message`:
|
104
91
|
|
105
92
|
```Ruby
|
106
93
|
class User < ActiveRecord::Base
|
107
|
-
|
108
|
-
|
109
|
-
# Passing :only => ... configures Protoable to only serialize the given fields
|
94
|
+
# Passing :only => ... configures Protobuf Active Record to only serialize the given fields
|
110
95
|
protobuf_message :user_message, :only => [ :first_name, :last_name, :email ]
|
111
96
|
end
|
112
97
|
```
|
@@ -117,9 +102,7 @@ Conversely, the `:except` option allows the fields that should be excluded to be
|
|
117
102
|
|
118
103
|
```Ruby
|
119
104
|
class User < ActiveRecord::Base
|
120
|
-
|
121
|
-
|
122
|
-
# Passing :except => ... configures Protoable to serialize everything except the given fields
|
105
|
+
# Passing :except => ... configures Protobuf Active Record to serialize everything except the given fields
|
123
106
|
protobuf_message :user_message, :except => [ :account_id, :created_at ]
|
124
107
|
end
|
125
108
|
```
|
@@ -136,20 +119,18 @@ user.to_proto(:include => :email) # Start with the class-level settings, but add
|
|
136
119
|
|
137
120
|
### Serializing deprecated fields
|
138
121
|
|
139
|
-
By default,
|
122
|
+
By default, deprecated fields are included when mapping protobuf message to Active Record objects. To exclude deprecated fields, simply pass the `:deprecated` option:
|
140
123
|
|
141
124
|
```Ruby
|
142
125
|
class User < ActiveRecord::Base
|
143
|
-
|
144
|
-
|
145
|
-
# Passing :deprecated => false configures Protoable to exclude deprecated fields.
|
126
|
+
# Passing :deprecated => false configures Protobuf Active Record to exclude deprecated fields.
|
146
127
|
protobuf_message :user_message, :deprecated => false
|
147
128
|
end
|
148
129
|
```
|
149
130
|
|
150
131
|
### Field transformers
|
151
132
|
|
152
|
-
Field transformers are used when serializing objects to protobuf messages.
|
133
|
+
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.
|
153
134
|
|
154
135
|
`field_from_record` takes the name of the field being transformed and a method name or callable (lambda or proc). When transforming that field, it calls the given callable, passing it the object being serialized.
|
155
136
|
|
@@ -157,8 +138,6 @@ Field transformers are used when serializing objects to protobuf messages. Proto
|
|
157
138
|
|
158
139
|
```Ruby
|
159
140
|
class User < ActiveRecord::Base
|
160
|
-
include Protoable
|
161
|
-
|
162
141
|
# Calls the lambda when serializing objects to protobuf messages, passing it
|
163
142
|
# the object being serialized.
|
164
143
|
field_from_record :status, lambda { |object_being_serlialized| # Some custom behavior }
|
@@ -167,12 +146,10 @@ end
|
|
167
146
|
|
168
147
|
### Attribute transformers
|
169
148
|
|
170
|
-
|
149
|
+
Protobuf Active Record handles mapping protobuf message fields to object attributes, but what happens when an attribute doesn't have a matching field? Using the `attribute_from_proto` method, you can define custom attribute transformations. Simply call `attribute_from_prot`, passing it the name of the attribute and a method name or callable (lambda or proc). When creating or updating objects, the transformer will be called and passed the protobuf message.
|
171
150
|
|
172
151
|
```Ruby
|
173
152
|
class User < ActiveRecord::Base
|
174
|
-
include Protoable
|
175
|
-
|
176
153
|
# Calls the lambda when creating/updating objects, passing it the protobuf
|
177
154
|
# message.
|
178
155
|
attribute_from_proto :account_id, lambda { |protobuf_message| # Some custom transformation... }
|
@@ -180,9 +157,9 @@ end
|
|
180
157
|
```
|
181
158
|
#### Searching
|
182
159
|
|
183
|
-
|
160
|
+
Protobuf Active Record's `search_scope` method takes the protobuf message and builds ARel scopes from it.
|
184
161
|
|
185
|
-
Before you can use `search_scope`, you'll need to tell
|
162
|
+
Before you can use `search_scope`, you'll need to tell Protobuf Active Record which fields should be searchable and what scope should be used to search with that field.
|
186
163
|
|
187
164
|
Consider this protobuf message:
|
188
165
|
|
@@ -198,29 +175,25 @@ To make the `name` field searchable, use the `field_scope` method:
|
|
198
175
|
|
199
176
|
```Ruby
|
200
177
|
class User < ActiveRecord::Base
|
201
|
-
include Protoable
|
202
|
-
|
203
178
|
scope :by_name, lambda { |*values| where(:name => values) }
|
204
179
|
|
205
180
|
field_scope :name, :scope => :by_name
|
206
181
|
end
|
207
182
|
```
|
208
183
|
|
209
|
-
This tells
|
184
|
+
This tells Protobuf Active Record that the name field should be searchable and that the :scope with the given name should be used to build the search scope.
|
210
185
|
|
211
186
|
`field_scope` can also be called with just a field name:
|
212
187
|
|
213
188
|
```Ruby
|
214
189
|
class User < ActiveRecord::Base
|
215
|
-
include Protoable
|
216
|
-
|
217
190
|
scope :by_name, lambda { |*values| where(:name => values) }
|
218
191
|
|
219
192
|
field_scope :name
|
220
193
|
end
|
221
194
|
```
|
222
195
|
|
223
|
-
If no scope is given,
|
196
|
+
If no scope is given, Protobuf Active Record assumes that a scope matching the given field prefixed with `by_`, in this case `by_name`.
|
224
197
|
|
225
198
|
Now that your class is configured with some searchable fields, you can use the `search_scope` method to build ARel scopes from a protobuf message.
|
226
199
|
|
@@ -236,7 +209,7 @@ User.search_scope(request)
|
|
236
209
|
User.limit(10).search_scope(request)
|
237
210
|
```
|
238
211
|
|
239
|
-
|
212
|
+
Protobuf Active Record also provides some aliases for the `search_scope` method in the event that you'd like something a little more descriptive: `by_fields` and `scope_from_proto` are all aliases of `search_scope`.
|
240
213
|
|
241
214
|
## Contributing
|
242
215
|
|
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.0.0
|
4
|
+
version: 3.0.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: 2013-10-
|
11
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -223,9 +223,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
223
223
|
version: '0'
|
224
224
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
225
225
|
requirements:
|
226
|
-
- - '
|
226
|
+
- - '>='
|
227
227
|
- !ruby/object:Gem::Version
|
228
|
-
version:
|
228
|
+
version: '0'
|
229
229
|
requirements: []
|
230
230
|
rubyforge_project:
|
231
231
|
rubygems_version: 2.0.6
|