passive_record 0.3.4 → 0.3.5

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: 18a1452f744235927c24058ecabbe3e0314a21f6
4
- data.tar.gz: e62c71b8af66ad7e22ce0d9ecae97d2c5b541ac1
3
+ metadata.gz: 291605bad255c42e5aeb6a91e58738cb4d0ac646
4
+ data.tar.gz: dbb92d140be813f5374c219d5eb37731d112f1e1
5
5
  SHA512:
6
- metadata.gz: e00bbcd3a411a87b31937a7bba75268568c451ca78b645da61e818e6fa27ddc269e25fc9a951bd087b620995c739d396c436301518fffc97ac7dac6d060a7662
7
- data.tar.gz: 426d3d9ec2026ac6a10c53ffac4d6ef35e5f919c3e5019faf9e676233c687b5320a2f35e8561e1722a78961169c6783e7652920621751218dfc6bfb300a834a3
6
+ metadata.gz: 24297308bdad6a014e87126f7dcf7334d24ba87e0638bc6c97bf6189114f580918fb0bb47f6af8941f974ed07058562c5276940db40d8301c1c2f9bf5864ffe6
7
+ data.tar.gz: 23e3adc6f921481527447089a6bbb0af921bd5cde00ec051fd272595570e916a124bc184f80c02c972e65a35ae2e1f5c42e5ee11da2afaa87731e50d3daa1036
@@ -45,7 +45,7 @@ module PassiveRecord
45
45
  end
46
46
  end
47
47
 
48
- def where(conditions)
48
+ def where(conditions={})
49
49
  Query.new(self, conditions)
50
50
  end
51
51
 
@@ -9,6 +9,11 @@ module PassiveRecord
9
9
  def initialize(klass,conditions={})
10
10
  @klass = klass
11
11
  @conditions = conditions
12
+ @subqueries = []
13
+ end
14
+
15
+ def not(conditions={})
16
+ NegatedQuery.new(@klass, conditions)
12
17
  end
13
18
 
14
19
  def all
@@ -45,6 +50,10 @@ module PassiveRecord
45
50
  end
46
51
 
47
52
  protected
53
+ def negated?
54
+ false
55
+ end
56
+
48
57
  def evaluate_nested_conditions(instance, field, value)
49
58
  association = instance.send(field)
50
59
  association && value.all? do |(association_field,val)|
@@ -56,5 +65,26 @@ module PassiveRecord
56
65
  end
57
66
  end
58
67
  end
68
+
69
+ 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
82
+ end
83
+ end
84
+
85
+ def negated?
86
+ true
87
+ end
88
+ end
59
89
  end
60
90
  end
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.3.4"
3
+ VERSION = "0.3.5"
4
4
  end
@@ -176,7 +176,16 @@ describe "passive record models" do
176
176
  Model.create(id: 12)
177
177
  expect(Model.find_all_by(id: [10,11])).to eq([model_a, model_b])
178
178
  end
179
+ end
180
+
181
+ context 'queries with negations' do
182
+ it 'should find where attribute value is NOT equal' do
183
+ model_a = Model.create(id: 'alpha')
184
+ model_b = Model.create(id: 'beta')
179
185
 
186
+ expect(Model.where.not(id: 'alpha').first).to eq(model_b)
187
+ expect(Model.where.not(id: 'beta').first).to eq(model_a)
188
+ end
180
189
  end
181
190
  end
182
191
  end
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.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman