protobuf-activerecord 2.0.0.rc1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -79,10 +79,6 @@ class UserMessage < ::Protobuf::Message
79
79
  end
80
80
  ```
81
81
 
82
- **Mass-assignment**
83
-
84
- If a model has protected attributes defined, Protoable will skip any fields that map to them. Likewise, if there are accessible attributes defined, only they will be mapped.
85
-
86
82
  ### Creating/Updating
87
83
 
88
84
  Protoable 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.
@@ -102,27 +98,61 @@ end
102
98
 
103
99
  Once the desired protobuf message has been specified, Protoable adds a `to_proto` method 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.
104
100
 
105
- ### Field & attribute converters
101
+ ### Choosing serializable fields
102
+
103
+ Protoable 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
+
105
+ ```Ruby
106
+ class User < ActiveRecord::Base
107
+ include Protoable
108
+
109
+ # Passing :only => ... configures Protoable to only serialize the given fields
110
+ protobuf_message :user_message, :only => [ :first_name, :last_name, :email ]
111
+ end
112
+ ```
113
+
114
+ This will only include the first_name, last_name, and email fields.
115
+
116
+ Conversely, the `:except` option allows the fields that should be excluded to be specified.
117
+
118
+ ```Ruby
119
+ class User < ActiveRecord::Base
120
+ include Protoable
106
121
 
107
- Protoable will handle regular field conversions out of the box, but for those times when custom conversions are needed, they can be defined with the `convert_field` and `protoable_attribute` methods. Field converters are used when creating or updating objects from a protobuf message and attribute converters are used when serializing objects to protobuf messages.
122
+ # Passing :except => ... configures Protoable to serialize everything except the given fields
123
+ protobuf_message :user_message, :except => [ :account_id, :created_at ]
124
+ end
125
+ ```
108
126
 
109
- `convert_field` and `protoable_attribute` both take the name of the field/attribute being converted and a method name or callable (lambda or proc). when converting that field, calls the given callable, passing it the value of the field being converted.
127
+ This does pretty much the same thing, but from a different perspective.
110
128
 
111
- **Converting fields**
129
+ `to_proto` also accepts these options, so you can override the class-level serializable fields on a per-instance basis:
130
+
131
+ ```Ruby
132
+ user.to_proto(:only => :email) # Only the email
133
+ user.to_proto(:except => :email) # Everthing except the email
134
+ user.to_proto(:include => :email) # Start with the class-level settings, but add email
135
+ ```
136
+
137
+ ### Serializing deprecated fields
138
+
139
+ By default, Protoaable includes deprecated fields when mapping protobuf message to Active Record objects. To exclude deprecated fields, simply pass the `:deprecated` option:
112
140
 
113
141
  ```Ruby
114
142
  class User < ActiveRecord::Base
115
143
  include Protoable
116
144
 
117
- # Calls :map_status_from_proto when creating/updating objects, passing it the
118
- # value of the status field from the protobuf message.
119
- def self.map_status_from_proto(field_value)
120
- # Some custom mapping
121
- end
122
- convert_field :status, :map_status_from_proto
145
+ # Passing :deprecated => false configures Protoable to exclude deprecated fields.
146
+ protobuf_message :user_message, :deprecated => false
123
147
  end
124
148
  ```
125
149
 
150
+ ### Field transformers
151
+
152
+ Field transformers are used when serializing objects to protobuf messages. Protoable will handle regular field mapping and conversions out of the box, but for those times when fields don't map directly to attributes or custom behavior is needed, Protoable provides the `field_from_record` method.
153
+
154
+ `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
+
126
156
  **Converting attributes**
127
157
 
128
158
  ```Ruby
@@ -130,8 +160,8 @@ class User < ActiveRecord::Base
130
160
  include Protoable
131
161
 
132
162
  # Calls the lambda when serializing objects to protobuf messages, passing it
133
- # the value of the status attribute.
134
- protoable_attribute :status, lambda { |attribute_value| attribute_value_.to_s }
163
+ # the object being serialized.
164
+ field_from_record :status, lambda { |object_being_serlialized| # Some custom behavior }
135
165
  end
136
166
  ```
137
167
 
@@ -192,7 +222,7 @@ User.search_scope(request)
192
222
  User.limit(10).search_scope(request)
193
223
  ```
194
224
 
195
- Protoable also provides some aliases for the `search_scope` method in the event that you'd like something a little more descriptive. `by_fields`, `from_proto`, and `scope_from_proto` are all aliases of `search_scope`.
225
+ Protoable 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`.
196
226
 
197
227
  ## Contributing
198
228
 
@@ -92,7 +92,7 @@ module Protoable
92
92
  # protobuf_message :user_message
93
93
  # protobuf_message "UserMessage"
94
94
  # protobuf_message "Namespaced::UserMessage"
95
- # protobuf_message :user_message, :only => :guid, :name
95
+ # protobuf_message :user_message, :only => [ :guid, :name ]
96
96
  # protobuf_message :user_message, :except => :email_domain
97
97
  # protobuf_message :user_message, :except => :email_domain, :deprecated => false
98
98
  #
@@ -146,13 +146,20 @@ module Protoable
146
146
  end
147
147
 
148
148
  # Extracts attributes that correspond to fields on the specified protobuf
149
- # message, performing any necessary column conversions on them.
149
+ # message, performing any necessary column conversions on them. Accepts a
150
+ # hash of options for specifying which fields should be serialized.
151
+ #
152
+ # Examples:
153
+ # fields_from_record(:only => [ :guid, :name ])
154
+ # fields_from_record(:except => :email_domain)
155
+ # fields_from_record(:include => :email_domain)
156
+ # fields_from_record(:except => :email_domain, :deprecated => false)
150
157
  #
151
158
  def fields_from_record(options = {})
152
159
  field_attributes = _filter_field_attributes(options)
153
160
  field_attributes += [ options.fetch(:include, []) ]
154
161
  field_attributes.flatten!
155
- field_attributes.compact
162
+ field_attributes.compact!
156
163
  field_attributes.uniq!
157
164
 
158
165
  field_attributes = field_attributes.inject({}) do |hash, field|
@@ -170,10 +177,12 @@ module Protoable
170
177
 
171
178
  private
172
179
 
180
+ # :nodoc:
173
181
  def _protobuf_convert_attributes_to_fields(field, value)
174
182
  self.class._protobuf_convert_attributes_to_fields(field, value)
175
183
  end
176
184
 
185
+ # :nodoc:
177
186
  def _protobuf_field_transformers
178
187
  self.class._protobuf_field_transformers
179
188
  end
@@ -118,14 +118,17 @@ module Protoable
118
118
  attributes
119
119
  end
120
120
 
121
+ # :nodoc:
121
122
  def convert_int64_to_time(int64)
122
123
  Time.at(int64.to_i)
123
124
  end
124
125
 
126
+ # :nodoc:
125
127
  def convert_int64_to_date(int64)
126
128
  convert_int64_to_time(int64).utc.to_date
127
129
  end
128
130
 
131
+ # :nodoc:
129
132
  def convert_int64_to_datetime(int64)
130
133
  convert_int64_to_time(int64).to_datetime
131
134
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "2.0.0.rc1"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
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: 2.0.0.rc1
5
- prerelease: 6
4
+ version: 2.0.0
5
+ prerelease:
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-20 00:00:00.000000000 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -239,13 +239,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: 1212310308387017285
242
+ hash: 1889653125462777253
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: 1.3.1
248
+ version: '0'
249
+ segments:
250
+ - 0
251
+ hash: 1889653125462777253
249
252
  requirements: []
250
253
  rubyforge_project:
251
254
  rubygems_version: 1.8.25