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 +10 -0
- data/README.md +41 -25
- data/gemfiles/3.0.gemfile.lock +1 -1
- data/gemfiles/3.1.gemfile.lock +1 -1
- data/gemfiles/3.2.gemfile.lock +1 -1
- data/lib/paraphrase/query.rb +22 -4
- data/lib/paraphrase/version.rb +1 -1
- metadata +21 -21
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
|
7
|
-
|
8
|
-
|
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
|
-
|
32
|
-
|
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
|
-
*
|
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
|
-
*
|
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
|
-
|
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
|
94
|
-
to. An array of keys can be supplied to pass multiple
|
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.
|
103
|
-
|
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
|
109
|
-
|
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 :
|
115
|
-
scope :
|
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
|
131
|
+
Post.paraphrase.results # => []
|
120
132
|
```
|
121
133
|
|
122
|
-
|
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
|
-
|
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
|
data/gemfiles/3.0.gemfile.lock
CHANGED
data/gemfiles/3.1.gemfile.lock
CHANGED
data/gemfiles/3.2.gemfile.lock
CHANGED
data/lib/paraphrase/query.rb
CHANGED
@@ -73,11 +73,29 @@ module Paraphrase
|
|
73
73
|
#
|
74
74
|
# @return [ActiveRecord::Relation, Array]
|
75
75
|
def results
|
76
|
-
results ||=
|
77
|
-
|
78
|
-
|
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
|
-
|
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
|
data/lib/paraphrase/version.rb
CHANGED
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.
|
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: &
|
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: *
|
24
|
+
version_requirements: *70289301081080
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
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: *
|
35
|
+
version_requirements: *70289301078480
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
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: *
|
46
|
+
version_requirements: *70289301077280
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
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: *
|
57
|
+
version_requirements: *70289301067840
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: yard
|
60
|
-
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: *
|
68
|
+
version_requirements: *70289301066540
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
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: *
|
79
|
+
version_requirements: *70289301064900
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: redcarpet
|
82
|
-
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: *
|
90
|
+
version_requirements: *70289301064000
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rake
|
93
|
-
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: *
|
101
|
+
version_requirements: *70289301063120
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: sqlite3
|
104
|
-
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: *
|
112
|
+
version_requirements: *70289301061980
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: appraisal
|
115
|
-
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: *
|
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
|
\ "
|