queryable 2.1.0 → 2.1.1
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.
- checksums.yaml +4 -4
- data/README.md +24 -23
- data/lib/queryable.rb +2 -1
- data/lib/queryable/chainable.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47991b347fa04e683c64a77a520c71e01891b471
|
4
|
+
data.tar.gz: 5cd9cece435510a6c432fc82230333c62d7f3b36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a647c943402b09005f4d58b14013f7e366eea865e6e86b2050bc04a5b7f26dd552874e1e2ea27ff6e22f9f0ded5d2f58a070f2e089f5abff396e27fdd9a4d233
|
7
|
+
data.tar.gz: d47b1fc13ac6a04a158f91714d85d5e6674b6e29b827d87f5a75b461c32a171086ce7d52d5c9c4a92160596038c5b4e1303edad562261e87e69252602fcb4d26
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@ Queryable
|
|
2
2
|
=====================
|
3
3
|
[](http://badge.fury.io/rb/queryable)
|
4
4
|
[](https://travis-ci.org/ElMassimo/queryable)
|
5
|
+
[](https://codeclimate.com/github/ElMassimo/queryable)
|
5
6
|
[](https://codeclimate.com/github/ElMassimo/queryable)
|
6
7
|
[](http://inch-ci.org/github/ElMassimo/queryable)
|
7
8
|
[](https://github.com/ElMassimo/queryable/blob/master/LICENSE.txt)
|
@@ -13,18 +14,6 @@ Queryable is a mixin that allows you to easily define query objects with chainab
|
|
13
14
|
|
14
15
|
Scopes serve to encapsulate reusable business rules, a method is defined with
|
15
16
|
the selected name and block (or proc)
|
16
|
-
|
17
|
-
### Delegation
|
18
|
-
|
19
|
-
By default most Array methods are delegated to the internal query. It's possible
|
20
|
-
to delegate extra methods to the query by calling `delegate`.
|
21
|
-
```ruby
|
22
|
-
def CustomersQuery
|
23
|
-
delegate :update_all, :destroy_all
|
24
|
-
end
|
25
|
-
```
|
26
|
-
|
27
|
-
## Usage
|
28
17
|
```ruby
|
29
18
|
class CustomersQuery
|
30
19
|
include Queryable
|
@@ -50,22 +39,26 @@ end
|
|
50
39
|
CustomerQuery.new(shop.customers).miller_fans
|
51
40
|
```
|
52
41
|
|
42
|
+
### Delegation
|
43
|
+
|
44
|
+
By default most Array methods are delegated to the internal query. It's possible
|
45
|
+
to delegate extra methods to the query by calling `delegate`.
|
46
|
+
```ruby
|
47
|
+
def CustomersQuery
|
48
|
+
delegate :update_all, :destroy_all
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
53
52
|
## Advantages
|
54
53
|
|
55
54
|
* Query objects are easy to understand.
|
56
55
|
* You can inherit, mixin, and chain queries in a very natural way.
|
57
56
|
* Increased testability, pretty close to being ORM/ODM agnostic.
|
58
57
|
|
59
|
-
## Testing
|
60
|
-
|
61
|
-
You can check the [specs](https://github.com/ElMassimo/queryable/tree/master/spec) of the project
|
62
|
-
to check how to test query objects without even having to require the ORM/ODM, or
|
63
|
-
you can test by requiring your ORM/ODM and executing queries as usual.
|
64
|
-
|
65
58
|
## Optional Modules
|
66
|
-
|
67
|
-
|
68
|
-
|
59
|
+
There are three opt-in modules that can help you when creating query objects.
|
60
|
+
These modules would need to be manually required during app initialization or
|
61
|
+
wherever necessary (in Rails, config/initializers).
|
69
62
|
|
70
63
|
### DefaultQuery
|
71
64
|
Provides default initialization for query objects, by attempting to infer the
|
@@ -89,6 +82,7 @@ OldCustomersQuery.new.queryable == ArchivedCustomers.all
|
|
89
82
|
```
|
90
83
|
If you want to use common base objects for your queries, you may want want to
|
91
84
|
delay the automatic inference:
|
85
|
+
|
92
86
|
```ruby
|
93
87
|
class BaseQuery
|
94
88
|
include Queryable
|
@@ -130,7 +124,7 @@ BigCustomersQuery.new.queryable ==
|
|
130
124
|
Customer.where(:last_purchase.gt => 7.days.ago, :total_expense.gt => 9999999)
|
131
125
|
```
|
132
126
|
|
133
|
-
### Chainable
|
127
|
+
### Chainable
|
134
128
|
|
135
129
|
While scopes are great because of their terseness, they can be limiting because
|
136
130
|
the block executes in the context of the internal query, so methods, constants,
|
@@ -175,8 +169,9 @@ To avoid repetition, it's a good idea to create a `BaseQuery` object
|
|
175
169
|
to contain both the modules inclusion, and common scopes you may reuse.
|
176
170
|
|
177
171
|
```ruby
|
178
|
-
require 'queryable/
|
172
|
+
require 'queryable/chainable'
|
179
173
|
require 'queryable/default_scope'
|
174
|
+
require 'queryable/default_query'
|
180
175
|
|
181
176
|
def BaseQuery
|
182
177
|
include Queryable
|
@@ -197,6 +192,12 @@ def CustomersQuery < BaseQuery
|
|
197
192
|
end
|
198
193
|
```
|
199
194
|
|
195
|
+
## Testing
|
196
|
+
|
197
|
+
You can check the [specs](https://github.com/ElMassimo/queryable/tree/master/spec) of the project
|
198
|
+
to check how to test query objects without even having to require the ORM/ODM, or
|
199
|
+
you can test by requiring your ORM/ODM and executing queries as usual.
|
200
|
+
|
200
201
|
## RDocs
|
201
202
|
|
202
203
|
You can view the **Queryable** documentation in RDoc format here:
|
data/lib/queryable.rb
CHANGED
@@ -38,7 +38,8 @@ module Queryable
|
|
38
38
|
|
39
39
|
# Public: Delegates the specified methods to the internal query.
|
40
40
|
def delegate(*methods)
|
41
|
-
methods.last.is_a?(Hash)
|
41
|
+
to = methods.last.is_a?(Hash) && methods.pop[:to] || :queryable
|
42
|
+
def_delegators(to == :class ? 'self.class' : to, *methods)
|
42
43
|
end
|
43
44
|
|
44
45
|
# Public: Defines a new method that executes the passed proc or block in
|
data/lib/queryable/chainable.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: queryable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Queryable is a module that encapsulates query building so you don't have
|
14
14
|
to tuck scopes inside your models.
|