protobuf-activerecord 2.0.1 → 2.1.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.
@@ -10,18 +10,47 @@ module Protoable
10
10
  end
11
11
 
12
12
  # Define fields that should be searchable via `search_scope`. Accepts a
13
- # protobuf field and an already defined scope.
13
+ # protobuf field and an already defined scope. If no scope is specified,
14
+ # the scope will be the field name, prefixed with `by_` (e.g. when the
15
+ # field is :guid, the scope will be :by_guid).
16
+ #
17
+ # Optionally, a parser can be provided that will be called, passing the
18
+ # field value as an argument. This allows custom data parsers to be used
19
+ # so that they don't have to be handled by scopes. Parsers must respond
20
+ # to `call` and accept a single parameter.
14
21
  #
15
22
  # Examples:
16
23
  #
17
24
  # class User < ActiveRecord::Base
18
25
  # scope :by_guid, lambda { |*guids| where(:guid => guids) }
26
+ # scope :custom_guid_scope, lambda { |*guids| where(:guid => guids) }
27
+ #
28
+ # # Equivalent to `field_scope :guid, :by_guid`
29
+ # field_scope :guid
30
+ #
31
+ # # With a custom scope
32
+ # field_scope :guid, :custom_guid_scope
19
33
  #
20
- # field_scope :guid, :by_guid
34
+ # # With a custom parser that converts the value to an integer
35
+ # field_scope :guid, :by_guid, lambda { |value| value.to_i }
21
36
  # end
22
37
  #
23
- def field_scope(field, scope_name)
38
+ def field_scope(field, scope_name = nil, parser = nil)
39
+ scope_name ||= :"by_#{field}"
24
40
  searchable_fields[field] = scope_name
41
+
42
+ # When no parser is defined, define one that simply returns the value
43
+ searchable_field_parsers[field] = parser || proc { |value| value }
44
+ end
45
+
46
+ # :noapi:
47
+ def parse_search_values(proto, field)
48
+ value = proto.__send__(field)
49
+ value = searchable_field_parsers[field].call(value)
50
+
51
+ values = [ value ].flatten
52
+ values.map!(&:to_i) if proto.get_field_by_name(field).enum?
53
+ values
25
54
  end
26
55
 
27
56
  # Builds and returns a Arel relation based on the fields that are present
@@ -45,10 +74,8 @@ module Protoable
45
74
  raise Protoable::SearchScopeError, "Undefined scope :#{scope_name}."
46
75
  end
47
76
 
48
- values = [ proto.__send__(field) ].flatten
49
- values.map!(&:to_i) if proto.get_field_by_name(field).enum?
50
-
51
- relation = relation.__send__(scope_name, *values)
77
+ search_values = parse_search_values(proto, field)
78
+ relation = relation.__send__(scope_name, *search_values)
52
79
  end
53
80
 
54
81
  return relation
@@ -58,5 +85,10 @@ module Protoable
58
85
  def searchable_fields
59
86
  @_searchable_fields ||= {}
60
87
  end
88
+
89
+ # :noapi:
90
+ def searchable_field_parsers
91
+ @_searchable_field_parsers ||= {}
92
+ end
61
93
  end
62
94
  end
@@ -1,5 +1,5 @@
1
1
  module Protobuf
2
2
  module ActiveRecord
3
- VERSION = "2.0.1"
3
+ VERSION = "2.1.0.beta"
4
4
  end
5
5
  end
@@ -4,6 +4,10 @@ describe Protoable::Scope do
4
4
  describe ".search_scope" do
5
5
  let(:request) { UserSearchMessage.new(:guid => ["foo"], :email => ["foo@test.co"]) }
6
6
 
7
+ before {
8
+ User.stub(:searchable_field_parsers).and_return({ :email => proc { |val| val } })
9
+ }
10
+
7
11
  it "builds scopes for searchable fields" do
8
12
  User.stub(:searchable_fields).and_return({ :email => :by_email })
9
13
  User.search_scope(request).should eq User.by_email("foo@test.co")
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.1
5
- prerelease:
4
+ version: 2.1.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-25 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -239,16 +239,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  segments:
241
241
  - 0
242
- hash: -468184549556839603
242
+ hash: 2337364999688507357
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: -468184549556839603
248
+ version: 1.3.1
252
249
  requirements: []
253
250
  rubyforge_project:
254
251
  rubygems_version: 1.8.25