johnbender-rquery 0.1.2 → 0.2.0
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.
- data/README.markdown +58 -11
- data/examples/user.rb +49 -0
- data/lib/rquery/active_record/base.rb +23 -21
- data/lib/rquery/adapters/sql.rb +8 -2
- data/lib/rquery/attribute.rb +14 -0
- data/lib/rquery/attribute_collection.rb +35 -0
- data/lib/rquery/declarations.rb +30 -35
- data/lib/rquery/serializers.rb +173 -99
- data/lib/rquery.rb +14 -6
- data/spec/active_record_base_spec_attribute_collection.rb +216 -0
- data/spec/active_record_base_spec_symbols.rb +214 -0
- data/spec/declarations_spec.rb +1 -25
- data/spec/mock_active_record.rb +13 -0
- data/spec/or_and_operations_spec.rb +54 -0
- data/spec/serializers_spec.rb +26 -22
- metadata +11 -5
- data/spec/active_record_base_spec.rb +0 -199
data/lib/rquery.rb
CHANGED
@@ -1,17 +1,25 @@
|
|
1
1
|
$: << File.expand_path(File.dirname(__FILE__) + "/../lib/")
|
2
2
|
|
3
|
+
#RQuery is a small DSL for building queries in query languages like SQL. It is meant to be concise, easy to read
|
4
|
+
#and expressive.
|
5
|
+
|
3
6
|
require "rquery/serializers.rb"
|
4
7
|
require "rquery/declarations.rb"
|
8
|
+
require "rquery/attribute.rb"
|
9
|
+
require "rquery/attribute_collection.rb"
|
5
10
|
require "rquery/adapters.rb"
|
6
11
|
require "rquery/active_record.rb"
|
7
12
|
|
8
13
|
module RQuery
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@@adapter
|
14
|
+
class << self
|
15
|
+
attr_accessor :adapter
|
16
|
+
|
17
|
+
def use_symbols
|
18
|
+
Symbol.send(:include, RQuery::Declarations)
|
15
19
|
end
|
20
|
+
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
24
|
+
##default adapter
|
25
|
+
RQuery.adapter = RQuery::Adapters::Sqlite
|
@@ -0,0 +1,216 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/mock_active_record.rb")
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/rquery.rb")
|
3
|
+
|
4
|
+
describe ActiveRecord do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
RQuery.adapter = RQuery::Adapters::Sqlite
|
8
|
+
end
|
9
|
+
|
10
|
+
#should really set up the find method defined above to use the ruby db libraries and
|
11
|
+
#create the final sql string
|
12
|
+
#all run with default adapter
|
13
|
+
|
14
|
+
it "should set up a where method" do
|
15
|
+
ActiveRecord::MockObject.respond_to?(:where).should == true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return sql with foo, the operations, and the values for mock.foo.is <operation> <value>" do
|
19
|
+
|
20
|
+
ActiveRecord::MockObject.where{ |mock|
|
21
|
+
mock.foo.is == "bar"
|
22
|
+
}.should == [:all, {:conditions => ["(foo = ?)", "bar"]}]
|
23
|
+
|
24
|
+
ActiveRecord::MockObject.where{ |mock|
|
25
|
+
mock.foo.is > 1
|
26
|
+
}.should == [:all, {:conditions => ["(foo > ?)", 1]}]
|
27
|
+
|
28
|
+
ActiveRecord::MockObject.where{ |mock|
|
29
|
+
mock.foo.is < 2
|
30
|
+
}.should == [:all, {:conditions => ["(foo < ?)", 2]}]
|
31
|
+
|
32
|
+
ActiveRecord::MockObject.where{ |mock|
|
33
|
+
mock.foo.is >= 3
|
34
|
+
}.should == [:all, {:conditions => ["(foo >= ?)", 3]}]
|
35
|
+
|
36
|
+
ActiveRecord::MockObject.where{ |mock|
|
37
|
+
mock.foo.is <= 4
|
38
|
+
}.should == [:all, {:conditions => ["(foo <= ?)", 4]}]
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return sql with foo, the operations, and the values for mock.foo.is_not <operation> <value>" do
|
43
|
+
ActiveRecord::MockObject.where{ |mock|
|
44
|
+
mock.foo.is_not == "bar"
|
45
|
+
}.should == [:all, {:conditions => ["(foo <> ?)", "bar"]}]
|
46
|
+
|
47
|
+
ActiveRecord::MockObject.where{ |mock|
|
48
|
+
mock.foo.is_not.in 1,2
|
49
|
+
}.should == [:all, {:conditions => ["(foo not in (?))", [1,2]]}]
|
50
|
+
|
51
|
+
ActiveRecord::MockObject.where{ |mock|
|
52
|
+
mock.foo.is_not.between 1..3
|
53
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", 1, 3]}]
|
54
|
+
|
55
|
+
ActiveRecord::MockObject.where{ |mock|
|
56
|
+
mock.foo.is_not.from 1..3
|
57
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", 1, 3]}]
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return sql with foo, the operations, and values for mock.foo.is.in and mock.foo.in when used with a list of args, array, and range" do
|
62
|
+
|
63
|
+
resulting_conditions = [:all, {:conditions => ["(foo in (?))", [1,2,3,4]]}]
|
64
|
+
|
65
|
+
ActiveRecord::MockObject.where{ |mock|
|
66
|
+
mock.foo.is.in 1,2,3,4
|
67
|
+
}.should == resulting_conditions
|
68
|
+
|
69
|
+
ActiveRecord::MockObject.where{ |mock|
|
70
|
+
mock.foo.is.in [1,2,3,4]
|
71
|
+
}.should == resulting_conditions
|
72
|
+
|
73
|
+
ActiveRecord::MockObject.where{ |mock|
|
74
|
+
mock.foo.is.in 1..4
|
75
|
+
}.should == [:all, {:conditions => ["(foo in (?))", 1..4]}]
|
76
|
+
|
77
|
+
ActiveRecord::MockObject.where{ |mock|
|
78
|
+
mock.foo.in 1,2,3,4
|
79
|
+
}.should == resulting_conditions
|
80
|
+
|
81
|
+
ActiveRecord::MockObject.where{ |mock|
|
82
|
+
mock.foo.in [1,2,3,4]
|
83
|
+
}.should == resulting_conditions
|
84
|
+
|
85
|
+
ActiveRecord::MockObject.where{ |mock|
|
86
|
+
mock.foo.in 1..4
|
87
|
+
}.should == [:all, {:conditions => ["(foo in (?))", 1..4]}]
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should return sql with foo, operations, and values for mock.foo.is.between and mock.foo.between when used with a list of args, array, and range" do
|
92
|
+
|
93
|
+
resulting_conditions = [:all, {:conditions => ["(foo between ? and ?)", 1, 2]}]
|
94
|
+
|
95
|
+
ActiveRecord::MockObject.where{ |mock|
|
96
|
+
mock.foo.is.between 1,2
|
97
|
+
}.should == resulting_conditions
|
98
|
+
|
99
|
+
ActiveRecord::MockObject.where{ |mock|
|
100
|
+
mock.foo.is.between [1,2]
|
101
|
+
}.should == resulting_conditions
|
102
|
+
|
103
|
+
ActiveRecord::MockObject.where{ |mock|
|
104
|
+
mock.foo.is.between 1..2
|
105
|
+
}.should == resulting_conditions
|
106
|
+
|
107
|
+
ActiveRecord::MockObject.where{ |mock|
|
108
|
+
mock.foo.between 1,2
|
109
|
+
}.should == resulting_conditions
|
110
|
+
|
111
|
+
ActiveRecord::MockObject.where{ |mock|
|
112
|
+
mock.foo.between [1,2]
|
113
|
+
}.should == resulting_conditions
|
114
|
+
|
115
|
+
ActiveRecord::MockObject.where{ |mock|
|
116
|
+
mock.foo.between 1..2
|
117
|
+
}.should == resulting_conditions
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should return sql with foo, operations, and values for mock.foo.is.from when used with a list of args, array, and range" do
|
122
|
+
|
123
|
+
resulting_conditions = [:all, {:conditions => ["(foo between ? and ?)", 1, 2]}]
|
124
|
+
|
125
|
+
ActiveRecord::MockObject.where{ |mock|
|
126
|
+
mock.foo.is.from 1,2
|
127
|
+
}.should == resulting_conditions
|
128
|
+
|
129
|
+
ActiveRecord::MockObject.where{ |mock|
|
130
|
+
mock.foo.is.from [1,2]
|
131
|
+
}.should == resulting_conditions
|
132
|
+
|
133
|
+
ActiveRecord::MockObject.where{ |mock|
|
134
|
+
mock.foo.is.from 1..2
|
135
|
+
}.should == resulting_conditions
|
136
|
+
|
137
|
+
ActiveRecord::MockObject.where{ |mock|
|
138
|
+
mock.foo.from 1,2
|
139
|
+
}.should == resulting_conditions
|
140
|
+
|
141
|
+
ActiveRecord::MockObject.where{ |mock|
|
142
|
+
mock.foo.from [1,2]
|
143
|
+
}.should == resulting_conditions
|
144
|
+
|
145
|
+
ActiveRecord::MockObject.where{ |mock|
|
146
|
+
mock.foo.from 1..2
|
147
|
+
}.should == resulting_conditions
|
148
|
+
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should return sql with foo, operations, and values for mock.foo.contains when used with a range, array, and list" do
|
153
|
+
|
154
|
+
resulting_conditions = [:all, {:conditions => ["(foo like '%' || ? || '%')", "bar"]}]
|
155
|
+
|
156
|
+
ActiveRecord::MockObject.where{ |mock|
|
157
|
+
mock.foo.contains "bar"
|
158
|
+
}.should == resulting_conditions
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
it "should return return the correct group of joined sql after multiple operations" do
|
164
|
+
|
165
|
+
ActiveRecord::MockObject.where{ |mock|
|
166
|
+
mock.foo.is == "bar"
|
167
|
+
mock.foo.is_not.in 1,2,3,4,5
|
168
|
+
}.should == [:all, {:conditions => ["(foo = ? and foo not in (?))", "bar", [1,2,3,4,5]]}]
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should return return the correct limit value passed" do
|
173
|
+
|
174
|
+
ActiveRecord::MockObject.where(:first){ |mock|
|
175
|
+
mock.foo.is == "bar"
|
176
|
+
mock.foo.is_not.in 1,2,3,4,5
|
177
|
+
}.should == [:first, {:conditions => ["(foo = ? and foo not in (?))", "bar", [1,2,3,4,5]]}]
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should have the correct 'not' keywords in alternating operations" do
|
182
|
+
|
183
|
+
ActiveRecord::MockObject.where(:first){ |mock|
|
184
|
+
mock.foo.is == "bar"
|
185
|
+
mock.foo.is_not.in 1,2,3,4,5
|
186
|
+
mock.foo.is > 3
|
187
|
+
}.should == [:first, {:conditions => ["(foo = ? and foo not in (?) and foo > ?)", "bar", [1,2,3,4,5], 3]}]
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should return return strings as arguments when passed to between, in, and from (used for date strings)" do
|
192
|
+
|
193
|
+
ActiveRecord::MockObject.where{ |mock|
|
194
|
+
mock.foo.is.between "some string", "2007-01-01"
|
195
|
+
}.should == [:all, {:conditions => ["(foo between ? and ?)", "some string", "2007-01-01"]}]
|
196
|
+
|
197
|
+
ActiveRecord::MockObject.where{ |mock|
|
198
|
+
mock.foo.is_not.between "some string", "2007-01-01"
|
199
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", "some string", "2007-01-01"]}]
|
200
|
+
|
201
|
+
ActiveRecord::MockObject.where{ |mock|
|
202
|
+
mock.foo.is.from "some string", "2007-01-01"
|
203
|
+
}.should == [:all, {:conditions => ["(foo between ? and ?)", "some string", "2007-01-01"]}]
|
204
|
+
|
205
|
+
ActiveRecord::MockObject.where{ |mock|
|
206
|
+
mock.foo.in "some string", "2007-01-01"
|
207
|
+
}.should == [:all, {:conditions => ["(foo in (?))", ["some string", "2007-01-01"]]}]
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should throw and exception when trying to use a field not in the objects attributes" do
|
212
|
+
attribute = "arbitrary_attribute_name"
|
213
|
+
lambda { ActiveRecord::MockObject.where{ |mock| mock.send(attribute).is == 2 }}.should raise_error(RQuery::AttributeNotFoundError)
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/mock_active_record.rb")
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/rquery.rb")
|
3
|
+
|
4
|
+
describe ActiveRecord do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
RQuery.adapter = RQuery::Adapters::Sqlite
|
8
|
+
RQuery.use_symbols
|
9
|
+
end
|
10
|
+
|
11
|
+
#should really set up the find method defined above to use the ruby db libraries and
|
12
|
+
#create the final sql string
|
13
|
+
#all run with default adapter
|
14
|
+
|
15
|
+
it "should set up a where method" do
|
16
|
+
ActiveRecord::MockObject.respond_to?(:where).should == true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return sql with foo, the operations, and the values for :foo.is <operation> <value>" do
|
20
|
+
|
21
|
+
ActiveRecord::MockObject.where{
|
22
|
+
:foo.is == "bar"
|
23
|
+
}.should == [:all, {:conditions => ["(foo = ?)", "bar"]}]
|
24
|
+
|
25
|
+
ActiveRecord::MockObject.where{
|
26
|
+
:foo.is > 1
|
27
|
+
}.should == [:all, {:conditions => ["(foo > ?)", 1]}]
|
28
|
+
|
29
|
+
ActiveRecord::MockObject.where{
|
30
|
+
:foo.is < 2
|
31
|
+
}.should == [:all, {:conditions => ["(foo < ?)", 2]}]
|
32
|
+
|
33
|
+
ActiveRecord::MockObject.where{
|
34
|
+
:foo.is >= 3
|
35
|
+
}.should == [:all, {:conditions => ["(foo >= ?)", 3]}]
|
36
|
+
|
37
|
+
ActiveRecord::MockObject.where{
|
38
|
+
:foo.is <= 4
|
39
|
+
}.should == [:all, {:conditions => ["(foo <= ?)", 4]}]
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return sql with foo, the operations, and the values for :foo.is_not <operation> <value>" do
|
44
|
+
ActiveRecord::MockObject.where{
|
45
|
+
:foo.is_not == "bar"
|
46
|
+
}.should == [:all, {:conditions => ["(foo <> ?)", "bar"]}]
|
47
|
+
|
48
|
+
ActiveRecord::MockObject.where{
|
49
|
+
:foo.is_not.in 1,2
|
50
|
+
}.should == [:all, {:conditions => ["(foo not in (?))", [1,2]]}]
|
51
|
+
|
52
|
+
ActiveRecord::MockObject.where{
|
53
|
+
:foo.is_not.between 1..3
|
54
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", 1, 3]}]
|
55
|
+
|
56
|
+
ActiveRecord::MockObject.where{
|
57
|
+
:foo.is_not.from 1..3
|
58
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", 1, 3]}]
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return sql with foo, the operations, and values for :foo.is.in and :foo.in when used with a list of args, array, and range" do
|
63
|
+
|
64
|
+
resulting_conditions = [:all, {:conditions => ["(foo in (?))", [1,2,3,4]]}]
|
65
|
+
|
66
|
+
ActiveRecord::MockObject.where{
|
67
|
+
:foo.is.in 1,2,3,4
|
68
|
+
}.should == resulting_conditions
|
69
|
+
|
70
|
+
ActiveRecord::MockObject.where{
|
71
|
+
:foo.is.in [1,2,3,4]
|
72
|
+
}.should == resulting_conditions
|
73
|
+
|
74
|
+
ActiveRecord::MockObject.where{
|
75
|
+
:foo.is.in 1..4
|
76
|
+
}.should == [:all, {:conditions => ["(foo in (?))", 1..4]}]
|
77
|
+
|
78
|
+
ActiveRecord::MockObject.where{
|
79
|
+
:foo.in 1,2,3,4
|
80
|
+
}.should == resulting_conditions
|
81
|
+
|
82
|
+
ActiveRecord::MockObject.where{
|
83
|
+
:foo.in [1,2,3,4]
|
84
|
+
}.should == resulting_conditions
|
85
|
+
|
86
|
+
ActiveRecord::MockObject.where{
|
87
|
+
:foo.in 1..4
|
88
|
+
}.should == [:all, {:conditions => ["(foo in (?))", 1..4]}]
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return sql with foo, operations, and values for :foo.is.between and :foo.between when used with a list of args, array, and range" do
|
93
|
+
|
94
|
+
resulting_conditions = [:all, {:conditions => ["(foo between ? and ?)", 1, 2]}]
|
95
|
+
|
96
|
+
ActiveRecord::MockObject.where{
|
97
|
+
:foo.is.between 1,2
|
98
|
+
}.should == resulting_conditions
|
99
|
+
|
100
|
+
ActiveRecord::MockObject.where{
|
101
|
+
:foo.is.between [1,2]
|
102
|
+
}.should == resulting_conditions
|
103
|
+
|
104
|
+
ActiveRecord::MockObject.where{
|
105
|
+
:foo.is.between 1..2
|
106
|
+
}.should == resulting_conditions
|
107
|
+
|
108
|
+
ActiveRecord::MockObject.where{
|
109
|
+
:foo.between 1,2
|
110
|
+
}.should == resulting_conditions
|
111
|
+
|
112
|
+
ActiveRecord::MockObject.where{
|
113
|
+
:foo.between [1,2]
|
114
|
+
}.should == resulting_conditions
|
115
|
+
|
116
|
+
ActiveRecord::MockObject.where{
|
117
|
+
:foo.between 1..2
|
118
|
+
}.should == resulting_conditions
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should return sql with foo, operations, and values for :foo.is.from when used with a list of args, array, and range" do
|
123
|
+
|
124
|
+
resulting_conditions = [:all, {:conditions => ["(foo between ? and ?)", 1, 2]}]
|
125
|
+
|
126
|
+
ActiveRecord::MockObject.where{
|
127
|
+
:foo.is.from 1,2
|
128
|
+
}.should == resulting_conditions
|
129
|
+
|
130
|
+
ActiveRecord::MockObject.where{
|
131
|
+
:foo.is.from [1,2]
|
132
|
+
}.should == resulting_conditions
|
133
|
+
|
134
|
+
ActiveRecord::MockObject.where{
|
135
|
+
:foo.is.from 1..2
|
136
|
+
}.should == resulting_conditions
|
137
|
+
|
138
|
+
ActiveRecord::MockObject.where{
|
139
|
+
:foo.from 1,2
|
140
|
+
}.should == resulting_conditions
|
141
|
+
|
142
|
+
ActiveRecord::MockObject.where{
|
143
|
+
:foo.from [1,2]
|
144
|
+
}.should == resulting_conditions
|
145
|
+
|
146
|
+
ActiveRecord::MockObject.where{
|
147
|
+
:foo.from 1..2
|
148
|
+
}.should == resulting_conditions
|
149
|
+
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return sql with foo, operations, and values for :foo.contains when used with a range, array, and list" do
|
154
|
+
|
155
|
+
resulting_conditions = [:all, {:conditions => ["(foo like '%' || ? || '%')", "bar"]}]
|
156
|
+
|
157
|
+
ActiveRecord::MockObject.where{
|
158
|
+
:foo.contains "bar"
|
159
|
+
}.should == resulting_conditions
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
it "should return return the correct group of joined sql after multiple operations" do
|
165
|
+
|
166
|
+
ActiveRecord::MockObject.where{
|
167
|
+
:foo.is == "bar"
|
168
|
+
:foo.is_not.in 1,2,3,4,5
|
169
|
+
}.should == [:all, {:conditions => ["(foo = ? and foo not in (?))", "bar", [1,2,3,4,5]]}]
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should return return the correct limit value passed" do
|
174
|
+
|
175
|
+
ActiveRecord::MockObject.where(:first){
|
176
|
+
:foo.is == "bar"
|
177
|
+
:foo.is_not.in 1,2,3,4,5
|
178
|
+
}.should == [:first, {:conditions => ["(foo = ? and foo not in (?))", "bar", [1,2,3,4,5]]}]
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should have the correct 'not' keywords in alternating operations" do
|
183
|
+
|
184
|
+
ActiveRecord::MockObject.where(:first){
|
185
|
+
:foo.is == "bar"
|
186
|
+
:foo.is_not.in 1,2,3,4,5
|
187
|
+
:foo.is > 3
|
188
|
+
}.should == [:first, {:conditions => ["(foo = ? and foo not in (?) and foo > ?)", "bar", [1,2,3,4,5], 3]}]
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should return return strings as arguments when passed to between, in, and from (used for date strings)" do
|
193
|
+
|
194
|
+
ActiveRecord::MockObject.where{
|
195
|
+
:foo.is.between "some string", "2007-01-01"
|
196
|
+
}.should == [:all, {:conditions => ["(foo between ? and ?)", "some string", "2007-01-01"]}]
|
197
|
+
|
198
|
+
ActiveRecord::MockObject.where{
|
199
|
+
:foo.is_not.between "some string", "2007-01-01"
|
200
|
+
}.should == [:all, {:conditions => ["(foo not between ? and ?)", "some string", "2007-01-01"]}]
|
201
|
+
|
202
|
+
ActiveRecord::MockObject.where{
|
203
|
+
:foo.is.from "some string", "2007-01-01"
|
204
|
+
}.should == [:all, {:conditions => ["(foo between ? and ?)", "some string", "2007-01-01"]}]
|
205
|
+
|
206
|
+
ActiveRecord::MockObject.where{
|
207
|
+
:foo.in "some string", "2007-01-01"
|
208
|
+
}.should == [:all, {:conditions => ["(foo in (?))", ["some string", "2007-01-01"]]}]
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
end
|
data/spec/declarations_spec.rb
CHANGED
@@ -11,7 +11,7 @@ describe RQuery::Declarations do
|
|
11
11
|
RQuery::Serializers::Operations.clear
|
12
12
|
end
|
13
13
|
|
14
|
-
it "any
|
14
|
+
it "any symbol should respond to is, is_not, in, between, from, and contains methods" do
|
15
15
|
:foo.respond_to?(:is).should == true
|
16
16
|
:foo.respond_to?(:is_not).should == true
|
17
17
|
:foo.respond_to?(:in).should == true
|
@@ -20,28 +20,4 @@ describe RQuery::Declarations do
|
|
20
20
|
:foo.respond_to?(:from).should == true
|
21
21
|
end
|
22
22
|
|
23
|
-
it "should return an Serializer Class when is, is_not are called" do
|
24
|
-
:foo.is.class.should == Class
|
25
|
-
:foo.is_not.class.should == Class
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should add an Operation for each call to is, is_not, and in" do
|
29
|
-
:foo.is
|
30
|
-
:bar.is
|
31
|
-
RQuery::Serializers::Operations.conditions.should == ["foo and bar"]
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should add an Operation and the arguments for each call to from, contain, and between" do
|
35
|
-
:foo.between 1,2
|
36
|
-
RQuery::Serializers::Operations.conditions.should == ["foo between ? and ?", 1 , 2]
|
37
|
-
RQuery::Serializers::Operations.clear
|
38
|
-
:bar.from 1,2
|
39
|
-
RQuery::Serializers::Operations.conditions.should == ["bar between ? and ?", 1 , 2]
|
40
|
-
RQuery::Serializers::Operations.clear
|
41
|
-
:baz.contains "something"
|
42
|
-
RQuery::Serializers::Operations.conditions.should == ["baz like '%' || ? || '%'", "something"]
|
43
|
-
RQuery::Serializers::Operations.clear
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
23
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/mock_active_record.rb")
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/rquery.rb")
|
3
|
+
|
4
|
+
describe RQuery::Serializers::Operations do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
RQuery.adapter = RQuery::Adapters::Sqlite
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should group two operations on the same line with parens and the 'or' keyword when the | operator is used" do
|
11
|
+
|
12
|
+
ActiveRecord::MockObject.where{ |mock|
|
13
|
+
(mock.foo.is == 2) | (mock.foo.in 1,2,3)
|
14
|
+
}.should == [:all, {:conditions => ["((foo = ? or foo in (?)))", 2, [1,2,3]]}]
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should group two operations on the same line with parns and the 'and' keyword when the & operator is used" do
|
19
|
+
|
20
|
+
ActiveRecord::MockObject.where{ |mock|
|
21
|
+
(mock.foo.is == 2) & (mock.foo.in 1,2,3)
|
22
|
+
}.should == [:all, {:conditions => ["((foo = ? and foo in (?)))", 2, [1,2,3]]}]
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should group two operations on the same line and continue to add subsequent operations" do
|
27
|
+
|
28
|
+
ActiveRecord::MockObject.where{ |mock|
|
29
|
+
(mock.foo.is == 2) & (mock.foo.in 1,2,3)
|
30
|
+
mock.foo.is > 3
|
31
|
+
}.should == [:all, {:conditions => ["((foo = ? and foo in (?)) and foo > ?)", 2, [1,2,3], 3]}]
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should properly group multiple nested groupings on the same line" do
|
36
|
+
|
37
|
+
ActiveRecord::MockObject.where{ |mock|
|
38
|
+
(mock.foo.is == 2) & (mock.foo.in 1,2,3) | (mock.foo.contains "george")
|
39
|
+
mock.foo.is > 3
|
40
|
+
(mock.foo.is == 2) & (mock.foo.in 1,2,3)
|
41
|
+
}.should == [:all, {:conditions => ["(((foo = ? and foo in (?)) or foo like '%' || ? || '%') and foo > ? and (foo = ? and foo in (?)))", 2, [1,2,3], "george", 3, 2, [1,2,3]]}]
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
it "& should have precedence when evaluating multiple operation group types on a single line" do
|
46
|
+
|
47
|
+
ActiveRecord::MockObject.where{ |mock|
|
48
|
+
(mock.foo.is == 2) | (mock.foo.in 1,2,3) & (mock.foo.contains "george")
|
49
|
+
}.should == [:all, {:conditions => ["((foo = ? or (foo in (?) and foo like '%' || ? || '%')))", 2, [1,2,3], "george"]}]
|
50
|
+
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/spec/serializers_spec.rb
CHANGED
@@ -3,28 +3,32 @@ require File.expand_path(File.dirname(__FILE__) + "/../lib/rquery.rb")
|
|
3
3
|
|
4
4
|
describe RQuery::Serializers do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
6
|
+
before(:all) do
|
7
|
+
RQuery.use_symbols
|
8
|
+
end
|
9
|
+
|
10
|
+
it "Object.is should define ==, <, <=, >, >=, in, and between" do
|
11
|
+
:foo.is.respond_to?(:==).should == true
|
12
|
+
:foo.is.respond_to?(:<).should == true
|
13
|
+
:foo.is.respond_to?(:<=).should == true
|
14
|
+
:foo.is.respond_to?(:>).should == true
|
15
|
+
:foo.is.respond_to?(:>=).should == true
|
16
|
+
:foo.is.respond_to?(:in).should == true
|
17
|
+
:foo.is.respond_to?(:between).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "Object.is_not should define == and in" do
|
21
|
+
:foo.is_not.respond_to?(:==).should == true
|
22
|
+
:foo.is_not.respond_to?(:in).should == true
|
23
|
+
:foo.is_not.respond_to?(:between).should == true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "Object.is_not should not redefine <, <=, >, >= in the same way that .is did" do
|
27
|
+
lambda {:foo.is_not.send(:<)}.should raise_error(ArgumentError)
|
28
|
+
lambda {:foo.is_not.send(:<=)}.should raise_error(ArgumentError)
|
29
|
+
lambda {:foo.is_not.send(:>)}.should raise_error(ArgumentError)
|
30
|
+
lambda {:foo.is_not.send(:>=)}.should raise_error(ArgumentError)
|
31
|
+
end
|
28
32
|
|
29
33
|
|
30
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: johnbender-rquery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John bender
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2000-
|
12
|
+
date: 2000-04-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,19 +24,25 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- README.markdown
|
26
26
|
- rakefile.rb
|
27
|
+
- examples/user.rb
|
27
28
|
- lib/rquery.rb
|
28
29
|
- lib/rquery/declarations.rb
|
29
30
|
- lib/rquery/serializers.rb
|
31
|
+
- lib/rquery/attribute.rb
|
32
|
+
- lib/rquery/attribute_collection.rb
|
30
33
|
- lib/rquery/active_record.rb
|
31
34
|
- lib/rquery/active_record/base.rb
|
32
35
|
- lib/rquery/adapters.rb
|
33
36
|
- lib/rquery/adapters/sql.rb
|
34
37
|
- lib/rquery/adapters/sqlite.rb
|
35
|
-
- spec/
|
36
|
-
- spec/
|
38
|
+
- spec/active_record_base_spec_attribute_collection.rb
|
39
|
+
- spec/active_record_base_spec_symbols.rb
|
37
40
|
- spec/declarations_spec.rb
|
41
|
+
- spec/mock_active_record.rb
|
42
|
+
- spec/or_and_operations_spec.rb
|
43
|
+
- spec/serializers_spec.rb
|
38
44
|
- spec/sqlite_adapter_spec.rb
|
39
|
-
has_rdoc:
|
45
|
+
has_rdoc: false
|
40
46
|
homepage: http://nickelcode.com/rquery/
|
41
47
|
post_install_message:
|
42
48
|
rdoc_options: []
|