paraphrase 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ 0.3.2 / 7-5-2012
2
+
3
+ * Cache Query#results
4
+ * Setup Query#method_missing to proxy to Query#results
5
+ * Setup Query#respond_to? to check Query#results
6
+
7
+ 0.3.1 / 7-5-2012
8
+
9
+ * Fix for rails 3.0
10
+
1
11
  0.3.0 / 7-5-2012
2
12
 
3
13
  * Allow nil values to be passed to scoped using :allow_nil option.
data/README.md CHANGED
@@ -3,10 +3,9 @@
3
3
  paraphrase provides a way to map one or multiple request params to model
4
4
  scopes.
5
5
 
6
- paraphrase was designed and geared towards building a query-based public API
7
- where you may want to require certain parameters to prevent consumers from
8
- scraping all your information or to mitigate the possibility of large,
9
- performance-intensive data-dumps.
6
+ paraphrase is geared towards building a query-based public API where you may
7
+ want to require certain parameters to prevent consumers from scraping all your
8
+ information or to mitigate large, performance-intensive database queries.
10
9
 
11
10
  ## Installation
12
11
 
@@ -28,8 +27,12 @@ $ gem install paraphrase
28
27
 
29
28
  ## Usage
30
29
 
31
- paraphrase aims to be as flexible as possible for your needs.
32
- * From within an `ActiveRecord::Base` subclass:
30
+ ### Setup
31
+
32
+ paraphrase aims to be as flexible as possible. `Query` classes can be created
33
+ in the following ways:
34
+
35
+ * Calling `register_mapping` in an `ActiveRecord::Base` subclass:
33
36
 
34
37
  ```ruby
35
38
  class Post < ActiveRecord::Base
@@ -43,7 +46,7 @@ class Post < ActiveRecord::Base
43
46
  end
44
47
  ```
45
48
 
46
- * In an initializer to register multiple mappings in one place:
49
+ * A configuration block through `Paraphrase.configure`:
47
50
 
48
51
  ```ruby
49
52
  # config/initializers/paraphrase.rb
@@ -56,7 +59,7 @@ Paraphrase.configure do |mappings|
56
59
  end
57
60
  ```
58
61
 
59
- * By creating a subclass of `Paraphrase::Query`:
62
+ * Subclassing `Paraphrase::Query`:
60
63
 
61
64
  ```ruby
62
65
  class PostQuery < Paraphrase::Query
@@ -66,16 +69,15 @@ class PostQuery < Paraphrase::Query
66
69
  end
67
70
  ```
68
71
 
69
- Then in a controller you can use it in any of the following ways:
72
+ ### Making a Query
73
+
74
+ In your controller, call the relevant method based on your setup:
70
75
 
71
76
  ```ruby
72
77
  class PostsController < ApplicationController
73
78
  respond_to :html, :json
74
79
 
75
80
  def index
76
- # Filters out relevant attributes
77
- # and applies scopes relevant to each
78
- # parameter
79
81
  @posts = Post.paraphrase(params)
80
82
 
81
83
  # Or
@@ -89,44 +91,58 @@ class PostsController < ApplicationController
89
91
  end
90
92
  ```
91
93
 
94
+ ### Configuring Scopes
95
+
92
96
  In any of these contexts, the `:key` option of the `:scope` method registers
93
- attribute(s) to extract from the params supplied and what scope to pass them
94
- to. An array of keys can be supplied to pass multiple attributes to a scope.
97
+ attribute(s) to extract from the params supplied and what method to pass them
98
+ to. An array of keys can be supplied to pass multiple arguments to a scope.
95
99
 
96
100
  ```ruby
97
101
  class Post < ActiveRecord::Base
98
102
  register_mapping do
99
103
  scope :by_user, :key => [:first_name, :last_name]
104
+ scope :published_on, :key => :pub_date
105
+ end
106
+
107
+ def self.by_user(first_name, last_name)
108
+ joins(:user).where(:user => { :first_name => first_name, :last_name => last_name })
100
109
  end
101
110
 
102
- def self.by_user(name)
103
- joins(:user).where(:user => { :name => name })
111
+ def self.published_on(pub_date)
112
+ where(:published_on => pub_date)
104
113
  end
105
114
  end
106
115
  ```
107
116
 
108
- If a key is required, pass `:require => true` to the options. This will
109
- return an empty results set if value for that key is missing.
117
+ If a scope is required for a query to be considered valid, pass `:require =>
118
+ true` or `:require => [:array, :of, :keys]` to the options. If any values are
119
+ missing for the scope, an empty result set will be returned. If a key is an
120
+ array of attributes, you can specify a subset of the key to be required. The
121
+ rest of the attributes will be allowed to be nil.
110
122
 
111
123
  ```ruby
112
124
  class Post < ActiveRecord::Base
113
125
  register_mapping do
114
- scope :by_author, :key => :author, :require => true
115
- scope :published_after, :key => :pub_date
126
+ scope :published_on, :key => :pub_date, :require => true
127
+ scope :by_author, :key => [:first_name, :last_name], :require => :last_name # requires :last_name, whitelists :first_name
116
128
  end
117
129
  end
118
130
 
119
- Post.paraphrase(:pub_date => '2010-10-30') # => []
131
+ Post.paraphrase.results # => []
120
132
  ```
121
133
 
122
- ## Plans
123
-
124
- * Enable requiring a subset of a compound key.
134
+ Alternatively, a scope can be whitelisted allowing nil values to be passed to the scope.
125
135
 
126
136
  ```ruby
127
- scope :by_author, :key => [:first_name, :last_name], :require => :first_name
137
+ class Post < ActiveRecord::Base
138
+ register_mapping do
139
+ scope :by_author, :key => [:first_name, :last_name], :allow_nil => :first_name # :first_name does not need to be specified
140
+ end
141
+ end
128
142
  ```
129
143
 
144
+ ## Plans / Thoughts for the Future
145
+
130
146
  * Support nested hashes in params.
131
147
 
132
148
  ```ruby
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.3.0)
4
+ paraphrase (0.3.2)
5
5
  activemodel (~> 3.0)
6
6
  activerecord (~> 3.0)
7
7
  activesupport (~> 3.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.3.0)
4
+ paraphrase (0.3.2)
5
5
  activemodel (~> 3.0)
6
6
  activerecord (~> 3.0)
7
7
  activesupport (~> 3.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/edd_d/src/paraphrase
3
3
  specs:
4
- paraphrase (0.3.0)
4
+ paraphrase (0.3.2)
5
5
  activemodel (~> 3.0)
6
6
  activerecord (~> 3.0)
7
7
  activesupport (~> 3.0)
@@ -73,11 +73,29 @@ module Paraphrase
73
73
  #
74
74
  # @return [ActiveRecord::Relation, Array]
75
75
  def results
76
- results ||= scopes.inject(source) do |query, scope|
77
- scope.chain(self, @params, query)
78
- end
76
+ @results ||= begin
77
+ results = scopes.inject(source) do |query, scope|
78
+ scope.chain(self, @params, query)
79
+ end
80
+
81
+ @errors.any? ? [] : results
82
+ end
83
+ end
84
+
79
85
 
80
- @results = @errors.any? ? [] : results
86
+ def respond_to?(name)
87
+ super || results.respond_to?(name)
88
+ end
89
+
90
+ protected
91
+
92
+ def method_missing(name, *args, &block)
93
+ if results.respond_to?(name)
94
+ self.class.delegate name, :to => :results
95
+ results.send(name, *args, &block)
96
+ else
97
+ super
98
+ end
81
99
  end
82
100
  end
83
101
  end
@@ -1,3 +1,3 @@
1
1
  module Paraphrase
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paraphrase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70300799182120 !ruby/object:Gem::Requirement
16
+ requirement: &70289301081080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70300799182120
24
+ version_requirements: *70289301081080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70300799180260 !ruby/object:Gem::Requirement
27
+ requirement: &70289301078480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70300799180260
35
+ version_requirements: *70289301078480
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &70300799177960 !ruby/object:Gem::Requirement
38
+ requirement: &70289301077280 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70300799177960
46
+ version_requirements: *70289301077280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &70300799176700 !ruby/object:Gem::Requirement
49
+ requirement: &70289301067840 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '1.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70300799176700
57
+ version_requirements: *70289301067840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &70300799167400 !ruby/object:Gem::Requirement
60
+ requirement: &70289301066540 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0.7'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70300799167400
68
+ version_requirements: *70289301066540
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &70300799166180 !ruby/object:Gem::Requirement
71
+ requirement: &70289301064900 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '2.10'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70300799166180
79
+ version_requirements: *70289301064900
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: redcarpet
82
- requirement: &70300799164540 !ruby/object:Gem::Requirement
82
+ requirement: &70289301064000 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70300799164540
90
+ version_requirements: *70289301064000
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rake
93
- requirement: &70300799163600 !ruby/object:Gem::Requirement
93
+ requirement: &70289301063120 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70300799163600
101
+ version_requirements: *70289301063120
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: sqlite3
104
- requirement: &70300799162540 !ruby/object:Gem::Requirement
104
+ requirement: &70289301061980 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70300799162540
112
+ version_requirements: *70289301061980
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: appraisal
115
- requirement: &70300799161860 !ruby/object:Gem::Requirement
115
+ requirement: &70289301061200 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70300799161860
123
+ version_requirements: *70289301061200
124
124
  description: ! "\n Build customizable classes to determine
125
125
  which scope\n methods to call based on supplied parameters.\n
126
126
  \ "