passive_record 0.3.17 → 0.3.18
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 +1 -1
- data/lib/passive_record.rb +1 -1
- data/lib/passive_record/core/identifier.rb +2 -1
- data/lib/passive_record/core/query.rb +59 -22
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +30 -4
- data/spec/spec_helper.rb +5 -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: 55612b91f34d4ac833ad318343274c028a7d9db3
|
4
|
+
data.tar.gz: ee4c132e59ab5c9c2eef23fa7ad6a643b50e9419
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aad41fefd6877ee5d43bbba3f42efd03c89e86e2600aca84f83a69a66a1186f974beb731c8a2ae2e9aa4de7b5b1958b10df557d95bf00c6b0303cda4bfa664ad
|
7
|
+
data.tar.gz: e331d278b6dcc08bfdb31312018dc8aa727dc7af6d7dd0a49e7f1205e672c6589fdd7d24672dde8d2bae09c4dee07d60158c5385c9d4a90ce6daf2f512c2952d
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
* [Email](mailto:joe at deepc.io)
|
6
6
|
|
7
7
|
[](https://codeclimate.com/github/deepcerulean/passive_record)
|
8
|
-
[](https://codeship.com/projects/
|
8
|
+
[](https://codeship.com/projects/135673)
|
9
9
|
[](https://codeclimate.com/github/deepcerulean/passive_record/coverage)
|
10
10
|
[](https://badge.fury.io/rb/passive_record)
|
11
11
|
[](https://gitter.im/deepcerulean/passive_record?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
data/lib/passive_record.rb
CHANGED
@@ -4,41 +4,43 @@ module PassiveRecord
|
|
4
4
|
include Enumerable
|
5
5
|
extend Forwardable
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(klass,conditions={})
|
7
|
+
def initialize(klass,conditions={},scope=nil)
|
10
8
|
@klass = klass
|
11
9
|
@conditions = conditions
|
12
|
-
@scope =
|
10
|
+
@scope = scope
|
11
|
+
end
|
12
|
+
|
13
|
+
def not(new_conditions={})
|
14
|
+
NegatedQuery.new(@klass, new_conditions)
|
13
15
|
end
|
14
16
|
|
15
|
-
def
|
16
|
-
|
17
|
+
def or(query=nil)
|
18
|
+
DisjoinedQuery.new(@klass, self, query)
|
17
19
|
end
|
18
20
|
|
19
21
|
def all
|
20
|
-
return [] unless conditions
|
22
|
+
return [] unless @conditions
|
21
23
|
matching = method(:matching_instances)
|
22
24
|
if @scope
|
23
25
|
if negated?
|
24
|
-
klass.reject(&@scope.method(:matching_instances))
|
26
|
+
@klass.reject(&@scope.method(:matching_instances))
|
25
27
|
else
|
26
|
-
klass.select(&@scope.method(:matching_instances))
|
28
|
+
@klass.select(&@scope.method(:matching_instances))
|
27
29
|
end
|
28
30
|
else
|
29
|
-
klass.select(&matching)
|
31
|
+
@klass.select(&matching)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
def_delegators :all, :each
|
33
35
|
|
34
36
|
def matching_instances(instance)
|
35
|
-
conditions.all? do |(field,value)|
|
37
|
+
@conditions.all? do |(field,value)|
|
36
38
|
evaluate_condition(instance, field, value)
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
42
|
def create(attrs={})
|
41
|
-
klass.create(conditions.merge(attrs))
|
43
|
+
@klass.create(@conditions.merge(attrs))
|
42
44
|
end
|
43
45
|
|
44
46
|
def first_or_create
|
@@ -46,24 +48,31 @@ module PassiveRecord
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def where(new_conditions={})
|
49
|
-
@conditions = new_conditions.merge(conditions)
|
51
|
+
@conditions = new_conditions.merge(@conditions)
|
50
52
|
self
|
51
53
|
end
|
52
54
|
|
53
|
-
def
|
54
|
-
|
55
|
+
def negated?
|
56
|
+
false
|
55
57
|
end
|
56
58
|
|
57
|
-
def
|
59
|
+
def disjoined?
|
58
60
|
false
|
59
61
|
end
|
60
62
|
|
63
|
+
def and(scope_query)
|
64
|
+
ConjoinedQuery.new(@klass, self, scope_query)
|
65
|
+
end
|
66
|
+
|
61
67
|
def method_missing(meth,*args,&blk)
|
62
|
-
if klass.methods.include?(meth)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
68
|
+
if @klass.methods.include?(meth)
|
69
|
+
scope_query = @klass.send(meth,*args,&blk)
|
70
|
+
if negated? && @scope.nil? && @conditions.empty?
|
71
|
+
@scope = scope_query
|
72
|
+
self
|
73
|
+
else
|
74
|
+
scope_query.and(self)
|
75
|
+
end
|
67
76
|
else
|
68
77
|
super(meth,*args,&blk)
|
69
78
|
end
|
@@ -94,7 +103,7 @@ module PassiveRecord
|
|
94
103
|
|
95
104
|
class NegatedQuery < Query
|
96
105
|
def matching_instances(instance)
|
97
|
-
conditions.none? do |(field,value)|
|
106
|
+
@conditions.none? do |(field,value)|
|
98
107
|
evaluate_condition(instance, field, value)
|
99
108
|
end
|
100
109
|
end
|
@@ -103,5 +112,33 @@ module PassiveRecord
|
|
103
112
|
true
|
104
113
|
end
|
105
114
|
end
|
115
|
+
|
116
|
+
class DisjoinedQuery < Query
|
117
|
+
def initialize(klass, first_query, second_query)
|
118
|
+
@klass = klass
|
119
|
+
@first_query = first_query
|
120
|
+
@second_query = second_query
|
121
|
+
end
|
122
|
+
|
123
|
+
def all
|
124
|
+
(@first_query.all + @second_query.all).uniq
|
125
|
+
end
|
126
|
+
|
127
|
+
def disjoined?
|
128
|
+
true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class ConjoinedQuery < Query
|
133
|
+
def initialize(klass, first_query, second_query)
|
134
|
+
@klass = klass
|
135
|
+
@first_query = first_query
|
136
|
+
@second_query = second_query
|
137
|
+
end
|
138
|
+
|
139
|
+
def all
|
140
|
+
@first_query.all & @second_query.all
|
141
|
+
end
|
142
|
+
end
|
106
143
|
end
|
107
144
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -205,9 +205,35 @@ describe "passive record models" do
|
|
205
205
|
end
|
206
206
|
end
|
207
207
|
|
208
|
+
context 'queries with disjunctions' do
|
209
|
+
it 'should find where attributes match EITHER query' do
|
210
|
+
pom = Family::Dog.create breed: 'pom'
|
211
|
+
pug = Family::Dog.create breed: 'pug'
|
212
|
+
Family::Dog.create breed: 'mutt'
|
213
|
+
Family::Dog.create breed: 'lab'
|
214
|
+
Family::Dog.create breed: 'papillon'
|
215
|
+
|
216
|
+
expect(
|
217
|
+
Family::Dog.
|
218
|
+
where(breed: 'pom').or(Family::Dog.where(breed: 'pug')).all
|
219
|
+
).to eq([pom, pug])
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'query chaining' do
|
224
|
+
it 'should handle conjoining scopes together' do
|
225
|
+
Post.create published_at: 10.days.ago, active: true
|
226
|
+
Post.create active: false
|
227
|
+
recent_and_active = Post.create active: true
|
228
|
+
|
229
|
+
expect(Post.active.recent.all).to eq([recent_and_active])
|
230
|
+
expect(Post.recent.active.all).to eq([recent_and_active])
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
208
234
|
context 'queries with scopes' do
|
209
|
-
let(:post) { Post.create(published_at: 10.days.ago) }
|
210
|
-
let(:another_post) {Post.create(published_at: 2.days.ago)}
|
235
|
+
let!(:post) { Post.create(published_at: 10.days.ago) }
|
236
|
+
let!(:another_post) {Post.create(published_at: 2.days.ago)}
|
211
237
|
|
212
238
|
describe 'should restrict using class method' do
|
213
239
|
it 'should use a class method as a scope' do
|
@@ -341,11 +367,11 @@ describe "passive record models" do
|
|
341
367
|
|
342
368
|
expect(
|
343
369
|
child.dogs.
|
344
|
-
where(breed: 'mutt')
|
370
|
+
where(breed: 'mutt').all
|
345
371
|
).to eq(
|
346
372
|
Family::Dog.
|
347
373
|
where(child_id: child.id).
|
348
|
-
where(breed: 'mutt')
|
374
|
+
where(breed: 'mutt').all
|
349
375
|
)
|
350
376
|
end
|
351
377
|
|
data/spec/spec_helper.rb
CHANGED
@@ -105,9 +105,13 @@ class Post < Model
|
|
105
105
|
has_many :comments
|
106
106
|
has_many :commenters, :through => :comments, :class_name => "User"
|
107
107
|
|
108
|
-
attr_accessor :published_at
|
108
|
+
attr_accessor :active, :published_at
|
109
109
|
before_create { @published_at = Time.now }
|
110
110
|
|
111
|
+
def self.active
|
112
|
+
where(active: true)
|
113
|
+
end
|
114
|
+
|
111
115
|
def self.recent
|
112
116
|
where(:published_at => 3.days.ago..Time.now)
|
113
117
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: passive_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Weissman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|