opium 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/opium/model/queryable.rb +53 -39
- data/lib/opium/version.rb +1 -1
- data/opium.gemspec +3 -3
- data/spec/opium/model/queryable_spec.rb +69 -63
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0f70beb198984ac00eabd7d1846b019aaac4b88
|
4
|
+
data.tar.gz: a3c498197bab34dfdf975896ca7433d27c9674f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e19ebe57797de055860c1e4a0007bb65857a9fa2a84e25bc0cd506859b7397c98578b98f4cdb10105cdc37156b9fed1c163992959d251e1bfaa51695d83d6c94
|
7
|
+
data.tar.gz: a7426e0281a31ea40c4c24615bf9e37eba51b1409fd7025847868f2670bff81588af480b53648e3cf635102186e3c33fb3a466a324a36496adabcb530769a3bc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## 1.3.2
|
2
|
+
### Resolved Issues
|
3
|
+
- #51: Queryable#translate_to_parse now attempts to ensure that constraint values
|
4
|
+
are converted to the associated field type.
|
5
|
+
|
1
6
|
## 1.3.1
|
2
7
|
### Resolved Issues
|
3
8
|
- #50: Model::Dirty#save! now overridden to apply dirty changes when invoked.
|
@@ -2,79 +2,79 @@ module Opium
|
|
2
2
|
module Model
|
3
3
|
module Queryable
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
module ClassMethods
|
7
7
|
delegate :count, :total_count, to: :criteria
|
8
|
-
|
8
|
+
|
9
9
|
def all( constraints = nil )
|
10
10
|
constraints ? imbued_where( arrayize( constraints ), '$all' ) : criteria
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
alias_method :all_in, :all
|
14
|
-
|
14
|
+
|
15
15
|
def between( constraints )
|
16
16
|
start = constraints.map {|key, range| [key, range.begin]}
|
17
17
|
inclusive = constraints.reject {|_, range| range.exclude_end?}.map {|key, range| [key, range.end]}
|
18
18
|
exclusive = constraints.select {|_, range| range.exclude_end?}.map {|key, range| [key, range.end]}
|
19
19
|
gte( start ).lte( inclusive ).lt( exclusive )
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def exists( constraints )
|
23
|
-
imbued_where( constraints.map {|key, value| [key, value.to_bool] }, '$exists' )
|
23
|
+
imbued_where( constraints.map {|key, value| [key, value.to_bool.freeze] }, '$exists' )
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def gt( constraints )
|
27
27
|
imbued_where( constraints, '$gt' )
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def gte( constraints )
|
31
31
|
imbued_where( constraints, '$gte' )
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def lt( constraints )
|
35
35
|
imbued_where( constraints, '$lt' )
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def lte( constraints )
|
39
39
|
imbued_where( constraints, '$lte' )
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def in( constraints )
|
43
43
|
imbued_where( arrayize( constraints ), '$in' )
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
alias_method :any_in, :in
|
47
|
-
|
47
|
+
|
48
48
|
def nin( constraints )
|
49
49
|
imbued_where( arrayize( constraints ), '$nin' )
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
def ne( constraints )
|
53
53
|
imbued_where( constraints, '$ne' )
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
def or( *subqueries )
|
57
|
-
where( '$or' => subqueries )
|
57
|
+
where( '$or' => subqueries.map {|query| translate_to_parse( query )}.freeze )
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
def select( constraints )
|
61
61
|
imbued_where( constraints, '$select' )
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
def dont_select( constraints )
|
65
65
|
imbued_where( constraints, '$dontSelect' )
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
def keys( *field_names )
|
69
69
|
validate_fields_exist( field_names )
|
70
70
|
criteria.update_constraint( :keys, field_names.map(&method(:translate_name)).join(',') )
|
71
71
|
end
|
72
|
-
|
73
|
-
# Should be noted that pluck is an immediate query execution, so doesn't play well with further chainable criteria
|
72
|
+
|
73
|
+
# Should be noted that pluck is an immediate query execution, so doesn't play well with further chainable criteria
|
74
74
|
def pluck( field_name )
|
75
75
|
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
def order( options )
|
79
79
|
validate_fields_exist( options )
|
80
80
|
previous = criteria.constraints[:order]
|
@@ -83,40 +83,40 @@ module Opium
|
|
83
83
|
).join(',')
|
84
84
|
criteria.update_constraint( :order, ordering )
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
def limit( value )
|
88
88
|
criteria.update_constraint( :limit, value )
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
def skip( value )
|
92
92
|
criteria.update_constraint( :skip, value )
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
def where( constraints )
|
96
96
|
validate_fields_exist( constraints )
|
97
97
|
criteria.update_constraint( :where, translate_to_parse( constraints ) )
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
alias_method :and, :where
|
101
|
-
|
101
|
+
|
102
102
|
def cache
|
103
103
|
criteria.update_variable( :cache, true )
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
def uncache
|
107
107
|
criteria.update_variable( :cache, false )
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
def cached?
|
111
111
|
criteria.variables[:cache]
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
private
|
115
|
-
|
115
|
+
|
116
116
|
def model
|
117
117
|
self
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def validate_fields_exist( field_names )
|
121
121
|
field_names = field_names.keys if field_names.respond_to? :keys
|
122
122
|
unless field_names.all? {|field_name| model.fields.key?( field_name ) || field_name =~ /^\$/ }
|
@@ -124,27 +124,41 @@ module Opium
|
|
124
124
|
raise ArgumentError, "#{not_fields.join(', ')} #{not_fields.length > 1 ? 'are not fields' : 'is not a field'} on this model"
|
125
125
|
end
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
def translate_name( field_name )
|
129
129
|
field_name =~ /^\$/ ? field_name : model.parse_canonical_field_names[ field_name ]
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
def translate_to_parse( constraints )
|
133
|
-
Hash[ *constraints.flat_map {|key, value| [translate_name( key ), value
|
133
|
+
Hash[ *constraints.flat_map {|key, value| [translate_name( key ), convert_to_field_type( key, value )]} ]
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
|
+
def convert_to_field_type( field_name, value )
|
137
|
+
return value if value.frozen?
|
138
|
+
case value
|
139
|
+
when Array
|
140
|
+
value.map {|i| convert_to_field_type( field_name, i)}
|
141
|
+
when Hash
|
142
|
+
Hash[ *value.flat_map {|k, v| [translate_name( k ), convert_to_field_type( field_name, v)]} ]
|
143
|
+
when Criteria, Pointer, Model
|
144
|
+
value.to_parse
|
145
|
+
else
|
146
|
+
model.fields[field_name].type.to_parse value
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
136
150
|
def arrayize( constraints )
|
137
151
|
constraints.map {|key, value| [key, value.to_a]}
|
138
152
|
end
|
139
|
-
|
153
|
+
|
140
154
|
def imbued_where( constraints, operator )
|
141
155
|
where( imbue_field_constraints_with_operator( constraints, operator ) )
|
142
156
|
end
|
143
|
-
|
157
|
+
|
144
158
|
def imbue_field_constraints_with_operator( constraints, operator )
|
145
159
|
Hash[ *constraints.flat_map {|key, value| [key, { operator => value }]} ]
|
146
160
|
end
|
147
161
|
end
|
148
162
|
end
|
149
163
|
end
|
150
|
-
end
|
164
|
+
end
|
data/lib/opium/version.rb
CHANGED
data/opium.gemspec
CHANGED
@@ -25,16 +25,16 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency "rspec-nc"
|
26
26
|
spec.add_development_dependency "rspec-its"
|
27
27
|
spec.add_development_dependency "webmock"
|
28
|
-
spec.add_development_dependency "guard"
|
28
|
+
spec.add_development_dependency "guard", "2.12.5"
|
29
29
|
spec.add_development_dependency "guard-rspec"
|
30
30
|
spec.add_development_dependency "pry"
|
31
31
|
spec.add_development_dependency "pry-remote"
|
32
32
|
spec.add_development_dependency "pry-nav"
|
33
33
|
spec.add_development_dependency "coveralls"
|
34
|
-
|
34
|
+
|
35
35
|
# spec.add_development_dependency "rails"
|
36
36
|
# spec.add_development_dependency "kaminari"
|
37
|
-
|
37
|
+
|
38
38
|
spec.add_dependency "activemodel", "~> 4.0"
|
39
39
|
spec.add_dependency "faraday", "~> 0.9"
|
40
40
|
spec.add_dependency "faraday_middleware", "~> 0.9"
|
@@ -5,7 +5,7 @@ describe Opium::Model::Queryable do
|
|
5
5
|
|
6
6
|
context 'when included in a class' do
|
7
7
|
subject { model }
|
8
|
-
|
8
|
+
|
9
9
|
it { is_expected.to respond_to( :all, :all_in ).with(1).argument }
|
10
10
|
it { is_expected.to respond_to( :and ).with(1).argument }
|
11
11
|
it { is_expected.to respond_to( :between ).with(1).argument }
|
@@ -23,62 +23,68 @@ describe Opium::Model::Queryable do
|
|
23
23
|
it { is_expected.to respond_to( :cache, :uncache, :cached? ) }
|
24
24
|
it { is_expected.to respond_to( :count, :total_count ) }
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
context 'within a model' do
|
28
28
|
before do
|
29
29
|
stub_const( 'Game', Class.new do
|
30
30
|
include Opium::Model
|
31
31
|
field :title, type: String
|
32
32
|
field :price, type: Float
|
33
|
-
|
33
|
+
|
34
34
|
stub(:model_name).and_return( 'Game' )
|
35
|
-
|
35
|
+
|
36
36
|
default_scope order( title: :asc )
|
37
37
|
end )
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
after do
|
41
41
|
Opium::Model::Criteria.models.clear
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
subject { Game }
|
45
|
-
|
45
|
+
|
46
46
|
describe '.where' do
|
47
47
|
it 'returns a criteria' do
|
48
48
|
subject.where( price: { '$lte' => 5 } ).should be_a( Opium::Model::Criteria )
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it 'sets the "where" constraint to the provided value' do
|
52
52
|
subject.where( price: { '$lte' => 5 } ).tap do |criteria|
|
53
53
|
criteria.constraints.should have_key( 'where' )
|
54
54
|
criteria.constraints['where'].should =~ { 'price' => { '$lte' => 5 } }
|
55
55
|
end
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
it 'deep merges the "where" constraint on successive calls' do
|
59
59
|
subject.where( price: { '$lte' => 5 } ).where( price: { '$gte' => 1 } ).tap do |criteria|
|
60
60
|
criteria.constraints['where'].should =~ { 'price' => { '$lte' => 5, '$gte' => 1 } }
|
61
61
|
end
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
it 'ensures that specified fields exist on the model' do
|
65
65
|
expect { subject.where( does_not_exist: true ) }.to raise_exception
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
it 'maps ruby names to parse names and ruby values to parse values' do
|
69
69
|
time = Time.now - 1000
|
70
70
|
subject.where( created_at: { '$gte' => time } ).tap do |criteria|
|
71
71
|
criteria.constraints['where'].should =~ { 'createdAt' => { '$gte' => time.to_parse } }
|
72
72
|
end
|
73
73
|
end
|
74
|
+
|
75
|
+
it 'converts constraint values to the field type' do
|
76
|
+
subject.where( price: '14.99' ).tap do |criteria|
|
77
|
+
expect( criteria.constraints['where']['price'] ).to eq 14.99
|
78
|
+
end
|
79
|
+
end
|
74
80
|
end
|
75
|
-
|
81
|
+
|
76
82
|
shared_examples_for 'a chainable criteria clause' do |method|
|
77
83
|
describe ".#{method}" do
|
78
84
|
it 'should return a criteria' do
|
79
85
|
subject.send( method, price: 5, title: 'Skyrim' ).should be_a( Opium::Model::Criteria )
|
80
86
|
end
|
81
|
-
|
87
|
+
|
82
88
|
it "should add a \"$#{method}\" clause to the where constraint for each member of the hash" do
|
83
89
|
subject.send( method, price: 5, title: 'Skyrim' ).tap do |criteria|
|
84
90
|
criteria.constraints.should have_key( 'where' )
|
@@ -87,19 +93,19 @@ describe Opium::Model::Queryable do
|
|
87
93
|
end
|
88
94
|
end
|
89
95
|
end
|
90
|
-
|
96
|
+
|
91
97
|
it_should_behave_like 'a chainable criteria clause', :gte
|
92
98
|
it_should_behave_like 'a chainable criteria clause', :lte
|
93
99
|
it_should_behave_like 'a chainable criteria clause', :gt
|
94
100
|
it_should_behave_like 'a chainable criteria clause', :lt
|
95
101
|
it_should_behave_like 'a chainable criteria clause', :ne
|
96
|
-
|
102
|
+
|
97
103
|
shared_examples_for 'a chainable array-valued criteria clause' do |method|
|
98
104
|
describe ":#{method}" do
|
99
105
|
it 'should return a criteria' do
|
100
106
|
subject.send( method, price: [1, 2, 3, 4] ).should be_a( Opium::Model::Criteria )
|
101
107
|
end
|
102
|
-
|
108
|
+
|
103
109
|
it "should add a \"$#{method}\" clause to the where constraint for each member of the hash, converting pair values to arrays" do
|
104
110
|
subject.send( method, price: 1..4, title: ['Skyrim', 'Oblivion'] ).tap do |criteria|
|
105
111
|
criteria.constraints.should have_key( 'where' )
|
@@ -108,7 +114,7 @@ describe Opium::Model::Queryable do
|
|
108
114
|
end
|
109
115
|
end
|
110
116
|
end
|
111
|
-
|
117
|
+
|
112
118
|
it_should_behave_like 'a chainable array-valued criteria clause', :all
|
113
119
|
it_should_behave_like 'a chainable array-valued criteria clause', :in
|
114
120
|
it_should_behave_like 'a chainable array-valued criteria clause', :nin
|
@@ -120,26 +126,26 @@ describe Opium::Model::Queryable do
|
|
120
126
|
end
|
121
127
|
end
|
122
128
|
end
|
123
|
-
|
129
|
+
|
124
130
|
it_should_behave_like 'an aliased method', :and, :where
|
125
131
|
it_should_behave_like 'an aliased method', :all_in, :all
|
126
132
|
it_should_behave_like 'an aliased method', :any_in, :in
|
127
|
-
|
133
|
+
|
128
134
|
describe '.all' do
|
129
135
|
context 'when no parameter is given' do
|
130
136
|
let(:result) { subject.all }
|
131
|
-
|
137
|
+
|
132
138
|
it { expect { result }.to_not raise_exception }
|
133
139
|
it { expect( result ).to be_a Opium::Model::Criteria }
|
134
140
|
it { expect( result ).to eq subject.criteria }
|
135
141
|
end
|
136
142
|
end
|
137
|
-
|
143
|
+
|
138
144
|
describe ':exists' do
|
139
145
|
it 'should return a criteria' do
|
140
146
|
subject.exists( price: true ).should be_a( Opium::Model::Criteria )
|
141
147
|
end
|
142
|
-
|
148
|
+
|
143
149
|
it 'should add a "$exist" clause for each hash pair, set to the truthiness of the pair value' do
|
144
150
|
subject.exists( price: true, title: 'no' ).tap do |criteria|
|
145
151
|
criteria.constraints.should have_key( 'where' )
|
@@ -147,61 +153,61 @@ describe Opium::Model::Queryable do
|
|
147
153
|
end
|
148
154
|
end
|
149
155
|
end
|
150
|
-
|
156
|
+
|
151
157
|
describe '#between' do
|
152
158
|
context 'with a inclusive range' do
|
153
159
|
subject { Game.between( price: 5..10 ) }
|
154
|
-
|
160
|
+
|
155
161
|
it { is_expected.to be_an( Opium::Model::Criteria ) }
|
156
|
-
|
162
|
+
|
157
163
|
it { expect( subject.criteria.constraints ).to include(:where) }
|
158
|
-
|
164
|
+
|
159
165
|
it 'adds a clause on the key' do
|
160
166
|
expect( subject.criteria.constraints[:where] ).to include(:price)
|
161
167
|
end
|
162
|
-
|
168
|
+
|
163
169
|
it 'adds a "$gte" clause to the key' do
|
164
170
|
expect( subject.criteria.constraints[:where][:price] ).to include( '$gte' => 5 )
|
165
171
|
end
|
166
|
-
|
172
|
+
|
167
173
|
it 'adds an "$lte" clause to the key' do
|
168
174
|
expect( subject.criteria.constraints[:where][:price] ).to include( '$lte' => 10 )
|
169
175
|
end
|
170
|
-
|
176
|
+
|
171
177
|
it 'does not add an "$lt" clause to the key' do
|
172
178
|
expect( subject.criteria.constraints[:where][:price].keys ).to_not include( '$lt' )
|
173
179
|
end
|
174
180
|
end
|
175
|
-
|
181
|
+
|
176
182
|
context 'with an exclusive range' do
|
177
183
|
subject { Game.between( price: 5...10 ) }
|
178
|
-
|
184
|
+
|
179
185
|
it 'adds a "$gte" clause to the key' do
|
180
186
|
expect( subject.criteria.constraints[:where][:price] ).to include( '$gte' => 5 )
|
181
187
|
end
|
182
|
-
|
188
|
+
|
183
189
|
it 'adds an "$lt" clause to the key' do
|
184
190
|
expect( subject.criteria.constraints[:where][:price] ).to include( '$lt' => 10 )
|
185
191
|
end
|
186
|
-
|
192
|
+
|
187
193
|
it 'does not add an "$lte" clause to the key' do
|
188
194
|
expect( subject.criteria.constraints[:where][:price].keys ).to_not include( '$lte' )
|
189
195
|
end
|
190
196
|
end
|
191
197
|
end
|
192
|
-
|
198
|
+
|
193
199
|
describe ':select' do
|
194
200
|
it 'should return a criteria' do
|
195
201
|
subject.select( title: subject.between( price: 5..10 ).keys( :title ) ).should be_a( Opium::Model::Criteria )
|
196
202
|
end
|
197
|
-
|
203
|
+
|
198
204
|
it 'should add a "$select" clause for each hash key, setting the value to a query expression based off the pair-value criteria' do
|
199
205
|
subject.select( title: subject.between( price: 5..10 ).keys( :title ) ).tap do |criteria|
|
200
206
|
criteria.constraints.should have_key( 'where' )
|
201
207
|
criteria.constraints['where'].should =~ {
|
202
|
-
'title' => { '$select' => {
|
203
|
-
'query' => { 'className' => 'Game', 'where' => { 'price' => { '$gte' => 5, '$lte' => 10 } } },
|
204
|
-
'key' => 'title'
|
208
|
+
'title' => { '$select' => {
|
209
|
+
'query' => { 'className' => 'Game', 'where' => { 'price' => { '$gte' => 5, '$lte' => 10 } } },
|
210
|
+
'key' => 'title'
|
205
211
|
} }
|
206
212
|
}
|
207
213
|
end
|
@@ -212,25 +218,25 @@ describe Opium::Model::Queryable do
|
|
212
218
|
it 'should return a criteria' do
|
213
219
|
subject.dont_select( title: subject.between( price: 5..10 ).keys( :title ) ).should be_a( Opium::Model::Criteria )
|
214
220
|
end
|
215
|
-
|
221
|
+
|
216
222
|
it 'should add a "$donSelect" clause for each hash key, setting the value to a query expression based off the pair-value criteria' do
|
217
223
|
subject.dont_select( title: subject.between( price: 5..10 ).keys( :title ) ).tap do |criteria|
|
218
224
|
criteria.constraints.should have_key( 'where' )
|
219
225
|
criteria.constraints['where'].should =~ {
|
220
|
-
'title' => { '$dontSelect' => {
|
221
|
-
'query' => { 'className' => 'Game', 'where' => { 'price' => { '$gte' => 5, '$lte' => 10 } } },
|
222
|
-
'key' => 'title'
|
226
|
+
'title' => { '$dontSelect' => {
|
227
|
+
'query' => { 'className' => 'Game', 'where' => { 'price' => { '$gte' => 5, '$lte' => 10 } } },
|
228
|
+
'key' => 'title'
|
223
229
|
} }
|
224
230
|
}
|
225
231
|
end
|
226
232
|
end
|
227
233
|
end
|
228
|
-
|
234
|
+
|
229
235
|
describe ':or' do
|
230
236
|
it 'should return a criteria' do
|
231
237
|
subject.or( { title: 'Skyrim' }, { title: 'Oblivion' } ).should be_a( Opium::Model::Criteria )
|
232
238
|
end
|
233
|
-
|
239
|
+
|
234
240
|
it 'should add an "$or" clause to the "where" constraint, whose contents are an array of all specified subqueries' do
|
235
241
|
subject.or( { title: 'Skyrim' }, { title: 'Oblivion' } ).tap do |criteria|
|
236
242
|
criteria.constraints.should have_key( 'where' )
|
@@ -240,29 +246,29 @@ describe Opium::Model::Queryable do
|
|
240
246
|
end
|
241
247
|
end
|
242
248
|
end
|
243
|
-
|
249
|
+
|
244
250
|
describe ':keys' do
|
245
251
|
it 'should return a criteria' do
|
246
252
|
subject.keys( :price ).should be_a( Opium::Model::Criteria )
|
247
253
|
end
|
248
|
-
|
254
|
+
|
249
255
|
it 'should set the "keys" constraint to the provided set of fields, whose names should be parsized' do
|
250
256
|
subject.keys( :price, :updated_at ).tap do |criteria|
|
251
257
|
criteria.constraints.should have_key( 'keys' )
|
252
258
|
criteria.constraints['keys'].should == 'price,updatedAt'
|
253
259
|
end
|
254
260
|
end
|
255
|
-
|
261
|
+
|
256
262
|
it 'should raise if a specified field does not exist' do
|
257
263
|
expect { subject.keys( :does_not_exist ) }.to raise_exception
|
258
264
|
end
|
259
265
|
end
|
260
|
-
|
266
|
+
|
261
267
|
describe ':limit' do
|
262
268
|
it 'should return a criteria' do
|
263
269
|
subject.limit( 5 ).should be_a( Opium::Model::Criteria )
|
264
270
|
end
|
265
|
-
|
271
|
+
|
266
272
|
it 'should set the "limit" constraint to the provided value' do
|
267
273
|
subject.limit( 100 ).tap do |criteria|
|
268
274
|
criteria.constraints.should have_key( 'limit' )
|
@@ -270,12 +276,12 @@ describe Opium::Model::Queryable do
|
|
270
276
|
end
|
271
277
|
end
|
272
278
|
end
|
273
|
-
|
279
|
+
|
274
280
|
describe ':skip' do
|
275
281
|
it 'should return a criteria' do
|
276
282
|
subject.skip( 5 ).should be_a( Opium::Model::Criteria )
|
277
283
|
end
|
278
|
-
|
284
|
+
|
279
285
|
it 'should set the "skip" constraint to the provided value' do
|
280
286
|
subject.skip( 100 ).tap do |criteria|
|
281
287
|
criteria.constraints.should have_key( 'skip' )
|
@@ -283,19 +289,19 @@ describe Opium::Model::Queryable do
|
|
283
289
|
end
|
284
290
|
end
|
285
291
|
end
|
286
|
-
|
292
|
+
|
287
293
|
describe ':order' do
|
288
294
|
it 'should return a criteria' do
|
289
295
|
subject.order( title: :asc ).should be_a( Opium::Model::Criteria )
|
290
296
|
end
|
291
|
-
|
297
|
+
|
292
298
|
it 'should set the "order" constraint to a string' do
|
293
299
|
subject.unscoped.order( title: :asc ).tap do |criteria|
|
294
300
|
criteria.constraints.should have_key( 'order' )
|
295
301
|
criteria.constraints['order'].should == 'title'
|
296
302
|
end
|
297
303
|
end
|
298
|
-
|
304
|
+
|
299
305
|
it 'should negate the field if given something which evaluates to "desc", "-1", or "-"' do
|
300
306
|
[:desc, -1, '-'].each do |direction|
|
301
307
|
subject.unscoped.order( title: direction ).tap do |criteria|
|
@@ -303,46 +309,46 @@ describe Opium::Model::Queryable do
|
|
303
309
|
end
|
304
310
|
end
|
305
311
|
end
|
306
|
-
|
312
|
+
|
307
313
|
it 'should combine multiple orderings via a comma' do
|
308
314
|
subject.unscoped.order( title: 1, price: -1 ).tap do |criteria|
|
309
315
|
criteria.constraints['order'].should == 'title,-price'
|
310
316
|
end
|
311
317
|
end
|
312
|
-
|
318
|
+
|
313
319
|
it 'should concatenate successive orderings' do
|
314
320
|
subject.unscoped.order( title: 1 ).order( price: -1 ).tap do |criteria|
|
315
321
|
criteria.constraints['order'].should == 'title,-price'
|
316
322
|
end
|
317
323
|
end
|
318
324
|
end
|
319
|
-
|
325
|
+
|
320
326
|
describe ':cached?' do
|
321
327
|
it 'should be false on a non-cached criteria' do
|
322
328
|
Game.criteria.cached?.should be_falsey
|
323
329
|
end
|
324
330
|
end
|
325
|
-
|
331
|
+
|
326
332
|
describe ':cache' do
|
327
333
|
after { subject.uncache }
|
328
|
-
|
334
|
+
|
329
335
|
it 'should return a criteria' do
|
330
336
|
subject.cache.should be_an( Opium::Model::Criteria )
|
331
337
|
end
|
332
|
-
|
338
|
+
|
333
339
|
it 'should cause :cached? to return true' do
|
334
340
|
subject.cache.cached?.should == true
|
335
341
|
end
|
336
342
|
end
|
337
|
-
|
343
|
+
|
338
344
|
describe ':uncache' do
|
339
345
|
it 'should return a criteria' do
|
340
346
|
subject.uncache.should be_an( Opium::Model::Criteria )
|
341
347
|
end
|
342
|
-
|
348
|
+
|
343
349
|
it 'should cause :cached? to return false' do
|
344
350
|
subject.uncache.cached?.should == false
|
345
351
|
end
|
346
352
|
end
|
347
353
|
end
|
348
|
-
end
|
354
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Bowers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -98,16 +98,16 @@ dependencies:
|
|
98
98
|
name: guard
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - '='
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
103
|
+
version: 2.12.5
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - '='
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: 2.12.5
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: guard-rspec
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|