passive_record 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 591c37c1582ed2d258fbbf7d3eea96f7c4de7f18
4
- data.tar.gz: 701f18bb12ea8af351ead02b2f174d7d97600b01
3
+ metadata.gz: fc8b97dbc45fa4523ccd68e4ab1fcd5e8cb7a195
4
+ data.tar.gz: 6213a7c2a314d2b6231d56708cd189fc4cc33d82
5
5
  SHA512:
6
- metadata.gz: ac2947495a7ab5167c060b406caef11a459be520e8816257f05fdeffbd4319315ead5ea55c42f02210d40ea6600bec2f1ad0d2b1d4979df0912d52969edc2f78
7
- data.tar.gz: 99ec487dc69d34798e9a742cc0972d5ad868d95f2415e9112a878e7db42afc5ed9b6f42c551d859372dd0f329ded458b95dbef0b9b21873e9ed65c20066cb42a
6
+ metadata.gz: 9ef69a79461f22a4d8f633996b7783d29eca541f448638087e57015f2513df998c24ca5208bf6cb9b46f254e0c582b27b6edc60cfdf2568830193969868f030a
7
+ data.tar.gz: 29506381b9effa9c5e2864e2ddb48137a6b3d716c39aa754ea3e8d16071e83b28331f353012728315bda8f1793bfba12d0365d2e2925bf95509e4cff8a156b58
data/README.md CHANGED
@@ -140,7 +140,7 @@ PassiveRecord may be right for you!
140
140
  - `parent.children<<` (insert a related model)
141
141
  - `parent.children.all?(&predicate)`
142
142
  - `parent.children.empty?`
143
- - `parent.children.where` (returns a `Core::Query`)
143
+ - `parent.children.where(conditions)` (returns a `Core::Query`)
144
144
 
145
145
  ### Relations
146
146
 
@@ -155,7 +155,7 @@ PassiveRecord may be right for you!
155
155
 
156
156
  ### Queries
157
157
 
158
- `Core::Query` objects acquired through `where` are chainable, accept nested queries, and have the following public methods:
158
+ `Core::Query` objects acquired through `where` are chainable, accept nested queries and scopes, and have the following public methods:
159
159
 
160
160
  - `where(conditions).all`
161
161
  - `where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
@@ -163,6 +163,7 @@ PassiveRecord may be right for you!
163
163
  - `where(conditions).first_or_create(attrs)`
164
164
  - `where(conditions).where(further_conditions)` (chaining)
165
165
  - `where.not(conditions)` (negation)
166
+ - `where.scope` (scoping with model class methods that return a query)
166
167
 
167
168
  ## Hooks
168
169
 
@@ -9,7 +9,7 @@ module PassiveRecord
9
9
  def initialize(klass,conditions={})
10
10
  @klass = klass
11
11
  @conditions = conditions
12
- @subqueries = []
12
+ @scope = nil
13
13
  end
14
14
 
15
15
  def not(conditions={})
@@ -19,7 +19,15 @@ module PassiveRecord
19
19
  def all
20
20
  return [] unless conditions
21
21
  matching = method(:matching_instances)
22
- klass.select(&matching)
22
+ if @scope
23
+ if negated?
24
+ klass.reject(&@scope.method(:matching_instances))
25
+ else
26
+ klass.select(&@scope.method(:matching_instances))
27
+ end
28
+ else
29
+ klass.select(&matching)
30
+ end
23
31
  end
24
32
  def_delegators :all, :each
25
33
 
@@ -46,19 +54,22 @@ module PassiveRecord
46
54
  @klass == other_query.klass && @conditions == other_query.conditions
47
55
  end
48
56
 
57
+ def negated?
58
+ false
59
+ end
60
+
49
61
  def method_missing(meth,*args,&blk)
50
62
  if klass.methods.include?(meth)
51
- klass.send(meth,*args,&blk)
63
+ # okay, so this thing is a query
64
+ # and we need to 'compose' with it somehow
65
+ @scope = klass.send(meth,*args,&blk)
66
+ self
52
67
  else
53
68
  super(meth,*args,&blk)
54
69
  end
55
70
  end
56
71
 
57
72
  protected
58
- def negated?
59
- false
60
- end
61
-
62
73
  def evaluate_condition(instance, field, value)
63
74
  if value.is_a?(Hash)
64
75
  evaluate_nested_conditions(instance, field, value)
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.6"
3
+ VERSION = "0.3.7"
4
4
  end
@@ -48,7 +48,7 @@ describe "passive record models" do
48
48
  child.dogs << dog
49
49
  expect(dog.inspect).to eq("Family::Dog (id: #{dog.id.inspect}, breed: \"#{dog.breed}\", created_at: #{dog.created_at}, sound: \"bark\", child_id: #{child.id.inspect})")
50
50
 
51
- expect(child.inspect).to eq("Family::Child (id: #{child.id.inspect}, created_at: #{dog.created_at}, name: \"Alice\", toy_id: nil, dog_ids: [#{dog.id.inspect}], parent_id: nil)")
51
+ expect(child.inspect).to eq("Family::Child (id: #{child.id.inspect}, created_at: #{child.created_at}, name: \"Alice\", toy_id: nil, dog_ids: [#{dog.id.inspect}], parent_id: nil)")
52
52
  end
53
53
  end
54
54
 
@@ -189,12 +189,29 @@ describe "passive record models" do
189
189
  end
190
190
 
191
191
  context 'queries with scopes' do
192
- it 'should find using class method' do
193
- post = Post.create(published_at: 10.days.ago)
194
- another_post = Post.create(published_at: 2.days.ago)
195
- expect(Post.recent).not_to include(post)
196
- expect(Post.recent).to include(another_post)
197
- expect(Post.where.not.recent).to include(another_post)
192
+ let(:post) { Post.create(published_at: 10.days.ago) }
193
+ let(:another_post) {Post.create(published_at: 2.days.ago)}
194
+
195
+ describe 'should restrict using class method' do
196
+ it 'should use a class method as a scope' do
197
+ expect(Post.recent).not_to include(post)
198
+ expect(Post.recent).to include(another_post)
199
+ end
200
+
201
+ it 'should negate a nullary scope' do
202
+ expect(Post.where.not.recent).to include(post)
203
+ expect(Post.where.not.recent).not_to include(another_post)
204
+ end
205
+
206
+ it 'should use a class method with an argument as a scope' do
207
+ expect(Post.where.published_within_days(3)).not_to include(post)
208
+ expect(Post.where.published_within_days(3)).to include(another_post)
209
+ end
210
+
211
+ it 'should negate a scope with an argument' do
212
+ expect(Post.where.not.published_within_days(3)).to include(post)
213
+ expect(Post.where.not.published_within_days(3)).not_to include(another_post)
214
+ end
198
215
  end
199
216
  end
200
217
  end
data/spec/spec_helper.rb CHANGED
@@ -87,7 +87,7 @@ class Role < Model
87
87
  has_and_belongs_to_many :users
88
88
  end
89
89
 
90
- ###
90
+ ###
91
91
 
92
92
  class Post < Model
93
93
  has_many :comments
@@ -95,7 +95,14 @@ class Post < Model
95
95
 
96
96
  attr_accessor :published_at
97
97
  before_create { @published_at = Time.now }
98
- def self.recent; where(:published_at => 3.days.ago..Time.now) end
98
+
99
+ def self.recent
100
+ where(:published_at => 3.days.ago..Time.now)
101
+ end
102
+
103
+ def self.published_within_days(n)
104
+ where(:published_at => n.days.ago..Time.now)
105
+ end
99
106
  end
100
107
 
101
108
  class User < Model
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.6
4
+ version: 0.3.7
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-02-25 00:00:00.000000000 Z
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport