protobuf-activerecord 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
- source 'http://gems.moneydesktop.com'
2
1
  source 'https://rubygems.org'
3
2
 
4
3
  # Specify your gem's dependencies in protobuf-activerecord.gemspec
data/README.md CHANGED
@@ -102,11 +102,11 @@ end
102
102
 
103
103
  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
104
 
105
- ### Field & column converters
105
+ ### Field & attribute converters
106
106
 
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 `convert_column` methods. Field converters are used when creating or updating objects from a protobuf message and column converters are used when serializing objects to protobuf messages.
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.
108
108
 
109
- `convert_field` and `convert_column` both take the name of the field/column 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.
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.
110
110
 
111
111
  **Converting fields**
112
112
 
@@ -123,21 +123,21 @@ class User < ActiveRecord::Base
123
123
  end
124
124
  ```
125
125
 
126
- **Converting columns**
126
+ **Converting attributes**
127
127
 
128
128
  ```Ruby
129
129
  class User < ActiveRecord::Base
130
130
  include Protoable
131
131
 
132
132
  # Calls the lambda when serializing objects to protobuf messages, passing it
133
- # the value of the status column from the database.
134
- convert_column :status, lambda { |column_value| column_value_.to_s }
133
+ # the value of the status attribute.
134
+ protoable_attribute :status, lambda { |attribute_value| attribute_value_.to_s }
135
135
  end
136
136
  ```
137
137
 
138
- ### Column transformers
138
+ ### Attribute transformers
139
139
 
140
- Protoable handles mapping protobuf message fields to object attributes, but what happens when an attribute doesn't have a matching field? Using the `transform_column` method, you can define custom column transformations. Simply call `transform_column`, passing it the name of the column and a method name or callable (lambda or proc). When creating or updating objects, Protoable will call the transformer, passing it the protobuf message.
140
+ Protoable 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, Protoable will call the transformer, passing it the protobuf message.
141
141
 
142
142
  ```Ruby
143
143
  class User < ActiveRecord::Base
@@ -145,9 +145,54 @@ class User < ActiveRecord::Base
145
145
 
146
146
  # Calls the lambda when creating/updating objects, passing it the protobuf
147
147
  # message.
148
- transform_column :account_id, lambda { |protobuf_message| # Some custom transformation... }
148
+ attribute_from_proto :account_id, lambda { |protobuf_message| # Some custom transformation... }
149
149
  end
150
150
  ```
151
+ #### Searching
152
+
153
+ Protoable's `search_scope` method takes the protobuf message and builds ARel scopes from it.
154
+
155
+ Before you can use `search_scope`, you'll need to tell Protoable which fields should be searchable and what scope should be used to search with that field.
156
+
157
+ Consider this protobuf message:
158
+
159
+ ```
160
+ message UserSearchRequest {
161
+ repeated string guid = 1;
162
+ repeated string name = 2;
163
+ repeated string email = 3;
164
+ }
165
+ ```
166
+
167
+ To make the `name` field searchable, use the `field_scope` method:
168
+
169
+ ```Ruby
170
+ class User < ActiveRecord::Base
171
+ include Protoable
172
+
173
+ scope :by_name, lambda { |*values| where(:name => values) }
174
+
175
+ field_scope :name, :by_name
176
+ end
177
+ ```
178
+
179
+ This tells Protoable that the name field should be searchable and that the scope with the given name should be used to build the search scope.
180
+
181
+ 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.
182
+
183
+ `search_scope` is chainable just like regular ARel scopes. It takes a protobuf messages and will build search scopes from any searchable fields that have values.
184
+
185
+ Picking up our User class again:
186
+
187
+ ```Ruby
188
+ # Build a search scope from the given protobuf message
189
+ User.search_scope(request)
190
+
191
+ # It's chainable too
192
+ User.limit(10).search_scope(request)
193
+ ```
194
+
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`.
151
196
 
152
197
  ## Contributing
153
198
 
@@ -1,5 +1,5 @@
1
- require 'protobuf_ext/message'
2
-
3
- require 'protoable'
4
-
1
+ require 'heredity'
2
+ require 'protobuf'
5
3
  require 'protobuf/activerecord/version'
4
+ require 'protobuf/activerecord/protobuf_ext/message'
5
+ require 'protobuf/activerecord/protoable'
@@ -0,0 +1,17 @@
1
+ require 'protobuf/activerecord/protoable/convert'
2
+ require 'protobuf/activerecord/protoable/errors'
3
+ require 'protobuf/activerecord/protoable/fields'
4
+ require 'protobuf/activerecord/protoable/persistence'
5
+ require 'protobuf/activerecord/protoable/scope'
6
+ require 'protobuf/activerecord/protoable/serialization'
7
+
8
+ module Protoable
9
+ def self.included(klass)
10
+ klass.extend Protoable::Fields
11
+ klass.extend Protoable::Scope
12
+
13
+ klass.__send__(:include, Protoable::Convert)
14
+ klass.__send__(:include, Protoable::Persistence)
15
+ klass.__send__(:include, Protoable::Serialization)
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'protobuf'
2
+
3
+ class Protobuf::Message
4
+ unless method_defined?(:respond_to_and_has?)
5
+ def respond_to_and_has?(key)
6
+ self.respond_to?(key) && self.has_field?(key)
7
+ end
8
+ end
9
+
10
+ unless method_defined?(:respond_to_and_has_and_present?)
11
+ def respond_to_and_has_and_present?(key)
12
+ self.respond_to_and_has?(key) &&
13
+ (self.__send__(key).present? || [true, false].include?(self.__send__(key)))
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "1.1.0"
3
+ VERSION = "1.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-10 00:00:00.000000000 Z
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -202,16 +202,16 @@ files:
202
202
  - LICENSE.txt
203
203
  - README.md
204
204
  - Rakefile
205
- - lib/protoable.rb
206
- - lib/protoable/convert.rb
207
- - lib/protoable/errors.rb
208
- - lib/protoable/fields.rb
209
- - lib/protoable/persistence.rb
210
- - lib/protoable/scope.rb
211
- - lib/protoable/serialization.rb
212
205
  - lib/protobuf-activerecord.rb
206
+ - lib/protobuf/activerecord/protoable.rb
207
+ - lib/protobuf/activerecord/protoable/convert.rb
208
+ - lib/protobuf/activerecord/protoable/errors.rb
209
+ - lib/protobuf/activerecord/protoable/fields.rb
210
+ - lib/protobuf/activerecord/protoable/persistence.rb
211
+ - lib/protobuf/activerecord/protoable/scope.rb
212
+ - lib/protobuf/activerecord/protoable/serialization.rb
213
+ - lib/protobuf/activerecord/protobuf_ext/message.rb
213
214
  - lib/protobuf/activerecord/version.rb
214
- - lib/protobuf_ext/message.rb
215
215
  - protobuf-activerecord.gemspec
216
216
  - spec/protoable/convert_spec.rb
217
217
  - spec/protoable/fields_spec.rb
@@ -240,7 +240,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
240
240
  version: '0'
241
241
  segments:
242
242
  - 0
243
- hash: -185204863767589185
243
+ hash: 471675617945359118
244
244
  required_rubygems_version: !ruby/object:Gem::Requirement
245
245
  none: false
246
246
  requirements:
@@ -249,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
249
  version: '0'
250
250
  segments:
251
251
  - 0
252
- hash: -185204863767589185
252
+ hash: 471675617945359118
253
253
  requirements: []
254
254
  rubyforge_project:
255
255
  rubygems_version: 1.8.24
data/lib/protoable.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'protoable/convert'
2
- require 'protoable/errors'
3
- require 'protoable/fields'
4
- require 'protoable/persistence'
5
- require 'protoable/scope'
6
- require 'protoable/serialization'
7
-
8
- module Protoable
9
- def self.included(klass)
10
- klass.extend Protoable::Fields
11
- klass.extend Protoable::Scope
12
-
13
- klass.__send__(:include, Protoable::Convert)
14
- klass.__send__(:include, Protoable::Persistence)
15
- klass.__send__(:include, Protoable::Serialization)
16
- end
17
- end
@@ -1,12 +0,0 @@
1
- require 'protobuf'
2
-
3
- class Protobuf::Message
4
- def respond_to_and_has?(key)
5
- self.respond_to?(key) && self.has_field?(key)
6
- end
7
-
8
- def respond_to_and_has_and_present?(key)
9
- self.respond_to_and_has?(key) &&
10
- (self.__send__(key).present? || [true, false].include?(self.__send__(key)))
11
- end
12
- end