groovy 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/groovy/model.rb +22 -19
- data/lib/groovy/query.rb +14 -22
- data/lib/groovy/version.rb +1 -1
- data/spec/query_spec.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4cf906ae6b02c372764a0b38842986a3ac6b8db27207798c51db3e0708b838c
|
4
|
+
data.tar.gz: 2104bb1cc0b8b22593cf856c63f0943e7cc7abc12609676af8c3394b677fcea0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 453c18aecd83f7615dbdaf71dbd29f1be7b807e510304b46c0b25095c64a4bcf53d6b1403488df19a49997792cac43dbc162cdad9a3e1754a1d17fda2a12a933
|
7
|
+
data.tar.gz: 250930ca9234eed807582ae2292f0196168c3b76d7b9ca9797445e258539d661b6128795d99db40377ef7b49ca1c8af27d7ec7e62537ef0ce1d5ddba80b2d873
|
data/lib/groovy/model.rb
CHANGED
@@ -68,14 +68,6 @@ module Groovy
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
def scope(name, obj)
|
72
|
-
define_singleton_method(name) do |*args|
|
73
|
-
res = obj.respond_to?(:call) ? obj.call(*args) : obj
|
74
|
-
# res = obj.respond_to?(:call) ? self.instance_exec(*args) { |a| obj.call(*a) } : obj
|
75
|
-
self # return self instead of query, so we can call other scopes
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
71
|
# called from Query, so needs to be public
|
80
72
|
def new_from_record(record)
|
81
73
|
new(record.attributes, record)
|
@@ -137,14 +129,6 @@ module Groovy
|
|
137
129
|
arr.slice(2..-1) # .flatten
|
138
130
|
end
|
139
131
|
|
140
|
-
def clear_query
|
141
|
-
@query = nil
|
142
|
-
end
|
143
|
-
|
144
|
-
def query
|
145
|
-
@query ||= Query.new(self, table)
|
146
|
-
end
|
147
|
-
|
148
132
|
def all
|
149
133
|
query
|
150
134
|
end
|
@@ -163,9 +147,20 @@ module Groovy
|
|
163
147
|
where(params).limit(1).first
|
164
148
|
end
|
165
149
|
|
166
|
-
|
167
|
-
|
168
|
-
|
150
|
+
def clear_query
|
151
|
+
@query = nil
|
152
|
+
end
|
153
|
+
|
154
|
+
def query
|
155
|
+
@query ||= query_class.new(self, table)
|
156
|
+
end
|
157
|
+
|
158
|
+
def scope(name, obj)
|
159
|
+
query_class.add_scope(name, obj)
|
160
|
+
define_singleton_method(name) do |*args|
|
161
|
+
query.public_send(name, *args)
|
162
|
+
end
|
163
|
+
end
|
169
164
|
|
170
165
|
[:search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
|
171
166
|
define_method scope_method do |*args, &block|
|
@@ -198,6 +193,14 @@ module Groovy
|
|
198
193
|
|
199
194
|
private
|
200
195
|
|
196
|
+
def query_class
|
197
|
+
@query_class ||= begin
|
198
|
+
Class.new(Query).tap do |klass|
|
199
|
+
Object.const_set("#{name}Query", klass)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
201
204
|
def db_context
|
202
205
|
Groovy.contexts[context_name.to_sym] or raise "Context not defined: #{context_name}"
|
203
206
|
end
|
data/lib/groovy/query.rb
CHANGED
@@ -9,6 +9,13 @@ module Groovy
|
|
9
9
|
|
10
10
|
attr_reader :parameters, :sorting
|
11
11
|
|
12
|
+
def self.add_scope(name, obj)
|
13
|
+
define_method(name) do |*args|
|
14
|
+
res = obj.respond_to?(:call) ? obj.call(*args) : obj
|
15
|
+
self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
def initialize(model, table, options = {})
|
13
20
|
@model, @table, @options = model, table, options
|
14
21
|
@parameters = options.delete(:parameters) || []
|
@@ -16,16 +23,6 @@ module Groovy
|
|
16
23
|
@default_sort_key = table.is_a?(Groonga::Hash) ? '_key' : '_id'
|
17
24
|
end
|
18
25
|
|
19
|
-
def merge_with!(another)
|
20
|
-
# puts "merging #{parameters.inspect} with #{another.parameters.inspect}"
|
21
|
-
# parameters.merge!(another.parameters)
|
22
|
-
@parameters = another.parameters
|
23
|
-
@sorting = another.sorting
|
24
|
-
# parameters.concat(another.parameters)
|
25
|
-
# sorting.merge!(another.sorting)
|
26
|
-
self
|
27
|
-
end
|
28
|
-
|
29
26
|
def search(obj)
|
30
27
|
obj.each do |col, q|
|
31
28
|
unless model.index_columns.include?(col)
|
@@ -37,6 +34,7 @@ module Groovy
|
|
37
34
|
|
38
35
|
def inspect
|
39
36
|
clear_query
|
37
|
+
# "<#{self.class.name} #{parameters}>"
|
40
38
|
super
|
41
39
|
end
|
42
40
|
|
@@ -177,11 +175,11 @@ module Groovy
|
|
177
175
|
@sorting[:offset] = from || 0
|
178
176
|
|
179
177
|
while results.any?
|
180
|
-
yield
|
178
|
+
yield to_a
|
181
179
|
break if results.size < of
|
182
180
|
|
183
181
|
@sorting[:offset] += of
|
184
|
-
@
|
182
|
+
@records = @results = nil # reset
|
185
183
|
end
|
186
184
|
end
|
187
185
|
|
@@ -201,7 +199,10 @@ module Groovy
|
|
201
199
|
end
|
202
200
|
|
203
201
|
def add_param(param)
|
204
|
-
|
202
|
+
if parameters.include?(param)
|
203
|
+
# clear_query
|
204
|
+
raise "Duplicate param: #{param}"
|
205
|
+
end
|
205
206
|
parameters.push(param)
|
206
207
|
end
|
207
208
|
|
@@ -253,15 +254,6 @@ module Groovy
|
|
253
254
|
part.gsub(/\s(\w)/, '\ \1')
|
254
255
|
end.join(' OR ').sub(/^-/, '_id:>0 -') #.gsub(' OR -', ' -')
|
255
256
|
end
|
256
|
-
|
257
|
-
def method_missing(name, *args)
|
258
|
-
if model.respond_to?(name)
|
259
|
-
other = model.public_send(name, *args)
|
260
|
-
merge_with!(other.query)
|
261
|
-
else
|
262
|
-
super
|
263
|
-
end
|
264
|
-
end
|
265
257
|
end
|
266
258
|
|
267
259
|
end
|
data/lib/groovy/version.rb
CHANGED
data/spec/query_spec.rb
CHANGED
@@ -193,6 +193,17 @@ describe Groovy::Query do
|
|
193
193
|
end
|
194
194
|
|
195
195
|
describe '#in_batches' do
|
196
|
+
it 'returns batches as expected' do
|
197
|
+
groups = []
|
198
|
+
res = TestProduct.in_batches(of: 2) do |group|
|
199
|
+
groups.push(group)
|
200
|
+
end
|
201
|
+
|
202
|
+
expect(groups.size).to eq(3)
|
203
|
+
expect(groups[0].map(&:id)).to eql([1,2])
|
204
|
+
expect(groups[1].map(&:id)).to eql([3,4])
|
205
|
+
expect(groups[2].map(&:id)).to eql([5])
|
206
|
+
end
|
196
207
|
end
|
197
208
|
|
198
209
|
describe '#paginate' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groovy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rroonga
|