passive_record 0.3.5 → 0.3.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 291605bad255c42e5aeb6a91e58738cb4d0ac646
4
- data.tar.gz: dbb92d140be813f5374c219d5eb37731d112f1e1
3
+ metadata.gz: 591c37c1582ed2d258fbbf7d3eea96f7c4de7f18
4
+ data.tar.gz: 701f18bb12ea8af351ead02b2f174d7d97600b01
5
5
  SHA512:
6
- metadata.gz: 24297308bdad6a014e87126f7dcf7334d24ba87e0638bc6c97bf6189114f580918fb0bb47f6af8941f974ed07058562c5276940db40d8301c1c2f9bf5864ffe6
7
- data.tar.gz: 23e3adc6f921481527447089a6bbb0af921bd5cde00ec051fd272595570e916a124bc184f80c02c972e65a35ae2e1f5c42e5ee11da2afaa87731e50d3daa1036
6
+ metadata.gz: ac2947495a7ab5167c060b406caef11a459be520e8816257f05fdeffbd4319315ead5ea55c42f02210d40ea6600bec2f1ad0d2b1d4979df0912d52969edc2f78
7
+ data.tar.gz: 99ec487dc69d34798e9a742cc0972d5ad868d95f2415e9112a878e7db42afc5ed9b6f42c551d859372dd0f329ded458b95dbef0b9b21873e9ed65c20066cb42a
data/README.md CHANGED
@@ -85,7 +85,7 @@ PassiveRecord may be right for you!
85
85
  => [Dog (id: 1, child_id: 1)]
86
86
  ````
87
87
 
88
- ## Interface
88
+ ## PassiveRecord API
89
89
 
90
90
  A class including PassiveRecord will gain the following new instance and class methods.
91
91
 
@@ -109,8 +109,6 @@ PassiveRecord may be right for you!
109
109
  - `User.descendants`
110
110
  - `User.destroy_all`
111
111
 
112
- ## Relationships
113
-
114
112
  ### Belongs To
115
113
 
116
114
  A model `Child` which is declared `belongs_to :parent` will gain:
@@ -144,9 +142,9 @@ PassiveRecord may be right for you!
144
142
  - `parent.children.empty?`
145
143
  - `parent.children.where` (returns a `Core::Query`)
146
144
 
147
- ### Explicit Relations
145
+ ### Relations
148
146
 
149
- Parent models which declare `has_many :children` gain a `parent.children` instance that returns an explicit PassiveRecord relation object, which has the following public interface:
147
+ Parent models which declare `has_many :children` gain a `parent.children` instance that returns an explicit PassiveRecord relation object, which has the following public methods:
150
148
 
151
149
  - `parent.children.all`
152
150
  - `parent.children.each` enumerates over `parent.children.all`, giving `parent.children.count`, `parent.children.first`, etc.
@@ -157,13 +155,14 @@ PassiveRecord may be right for you!
157
155
 
158
156
  ### Queries
159
157
 
160
- `Core::Query` objects acquired through `where` are chainable and have their own API.
158
+ `Core::Query` objects acquired through `where` are chainable, accept nested queries, and have the following public methods:
161
159
 
162
160
  - `where(conditions).all`
163
161
  - `where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
164
162
  - `where(conditions).create(attrs)`
165
163
  - `where(conditions).first_or_create(attrs)`
166
164
  - `where(conditions).where(further_conditions)` (chaining)
165
+ - `where.not(conditions)` (negation)
167
166
 
168
167
  ## Hooks
169
168
 
@@ -18,20 +18,17 @@ module PassiveRecord
18
18
 
19
19
  def all
20
20
  return [] unless conditions
21
- klass.select do |instance|
22
- conditions.all? do |(field,value)|
23
- if value.is_a?(Hash)
24
- evaluate_nested_conditions(instance, field, value)
25
- elsif value.is_a?(Range) || value.is_a?(Array)
26
- value.include?(instance.send(field))
27
- else
28
- instance.send(field) == value
29
- end
30
- end
31
- end
21
+ matching = method(:matching_instances)
22
+ klass.select(&matching)
32
23
  end
33
24
  def_delegators :all, :each
34
25
 
26
+ def matching_instances(instance)
27
+ conditions.all? do |(field,value)|
28
+ evaluate_condition(instance, field, value)
29
+ end
30
+ end
31
+
35
32
  def create(attrs={})
36
33
  klass.create(conditions.merge(attrs))
37
34
  end
@@ -49,11 +46,29 @@ module PassiveRecord
49
46
  @klass == other_query.klass && @conditions == other_query.conditions
50
47
  end
51
48
 
49
+ def method_missing(meth,*args,&blk)
50
+ if klass.methods.include?(meth)
51
+ klass.send(meth,*args,&blk)
52
+ else
53
+ super(meth,*args,&blk)
54
+ end
55
+ end
56
+
52
57
  protected
53
58
  def negated?
54
59
  false
55
60
  end
56
61
 
62
+ def evaluate_condition(instance, field, value)
63
+ if value.is_a?(Hash)
64
+ evaluate_nested_conditions(instance, field, value)
65
+ elsif value.is_a?(Range) || value.is_a?(Array)
66
+ value.include?(instance.send(field))
67
+ else
68
+ instance.send(field) == value
69
+ end
70
+ end
71
+
57
72
  def evaluate_nested_conditions(instance, field, value)
58
73
  association = instance.send(field)
59
74
  association && value.all? do |(association_field,val)|
@@ -67,18 +82,9 @@ module PassiveRecord
67
82
  end
68
83
 
69
84
  class NegatedQuery < Query
70
- def all
71
- return [] unless conditions
72
- klass.select do |instance|
73
- conditions.none? do |(field,value)|
74
- if value.is_a?(Hash)
75
- evaluate_nested_conditions(instance, field, value)
76
- elsif value.is_a?(Range) || value.is_a?(Array)
77
- value.include?(instance.send(field))
78
- else
79
- instance.send(field) == value
80
- end
81
- end
85
+ def matching_instances(instance)
86
+ conditions.none? do |(field,value)|
87
+ evaluate_condition(instance, field, value)
82
88
  end
83
89
  end
84
90
 
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.5"
3
+ VERSION = "0.3.6"
4
4
  end
@@ -187,6 +187,16 @@ describe "passive record models" do
187
187
  expect(Model.where.not(id: 'beta').first).to eq(model_a)
188
188
  end
189
189
  end
190
+
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)
198
+ end
199
+ end
190
200
  end
191
201
  end
192
202
  end
@@ -92,6 +92,10 @@ end
92
92
  class Post < Model
93
93
  has_many :comments
94
94
  has_many :commenters, :through => :comments, :class_name => "User"
95
+
96
+ attr_accessor :published_at
97
+ before_create { @published_at = Time.now }
98
+ def self.recent; where(:published_at => 3.days.ago..Time.now) end
95
99
  end
96
100
 
97
101
  class User < Model
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman