groovy 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/basic.rb +2 -2
- data/groovy.gemspec +1 -1
- data/lib/groovy/model.rb +2 -10
- data/lib/groovy/query.rb +18 -16
- data/lib/groovy/version.rb +1 -1
- data/spec/model_spec.rb +3 -7
- data/spec/query_spec.rb +21 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56df8a7df15cb6444023882779e0f99db75c5fd2a2843356b4aae4e18dd62659
|
4
|
+
data.tar.gz: f8515e325de94ae8966caa271659c010c9a4a4335a3270c10f3d5360528d2faf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6257e0e6c769952f74099b04fc41cf81116cb4f8ed7105030c694c5af8ab4d99424a851b9bbff332fe12424f6d578dcdcfc1db4396ce07c030df105fa3e9162
|
7
|
+
data.tar.gz: 02252aa72c91778f9d464e4c4341e3bafa55f0b4fc71687b8e4e2c7520e78a860bd465bfa971a5ec0d84a2f1e83ef3e2fa46d813436a8141fc6ce9f8e20a55f7
|
data/example/basic.rb
CHANGED
@@ -12,8 +12,8 @@ class Product
|
|
12
12
|
t.timestamps
|
13
13
|
end
|
14
14
|
|
15
|
-
scope :by_price, -> { sort_by(price: :asc) }
|
16
|
-
scope :named, -> (name) { where(name: name) if name }
|
15
|
+
scope :by_price, -> { puts self.inspect; sort_by(price: :asc) }
|
16
|
+
scope :named, -> (name) { puts self.inspect; where(name: name) if name }
|
17
17
|
end
|
18
18
|
|
19
19
|
def populate
|
data/groovy.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
# s.required_rubygems_version = ">= 1.3.6"
|
15
15
|
# s.rubyforge_project = "groovy"
|
16
16
|
|
17
|
-
s.add_runtime_dependency "rroonga", "~>
|
17
|
+
s.add_runtime_dependency "rroonga", "~> 9.0"
|
18
18
|
|
19
19
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
20
20
|
s.add_development_dependency "rspec", '~> 3.0', '>= 3.0.0'
|
data/lib/groovy/model.rb
CHANGED
@@ -143,16 +143,8 @@ module Groovy
|
|
143
143
|
num == 1 ? arr.first : arr
|
144
144
|
end
|
145
145
|
|
146
|
-
def find_by(params)
|
147
|
-
where(params).limit(1).first
|
148
|
-
end
|
149
|
-
|
150
|
-
def clear_query
|
151
|
-
@query = nil
|
152
|
-
end
|
153
|
-
|
154
146
|
def query
|
155
|
-
|
147
|
+
query_class.new(self, table)
|
156
148
|
end
|
157
149
|
|
158
150
|
def scope(name, obj)
|
@@ -162,7 +154,7 @@ module Groovy
|
|
162
154
|
end
|
163
155
|
end
|
164
156
|
|
165
|
-
[:search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
|
157
|
+
[:find_by, :search, :where, :not, :sort_by, :limit, :offset, :paginate, :in_batches].each do |scope_method|
|
166
158
|
define_method scope_method do |*args, &block|
|
167
159
|
query.public_send(scope_method, *args, &block)
|
168
160
|
end
|
data/lib/groovy/query.rb
CHANGED
@@ -6,12 +6,14 @@ module Groovy
|
|
6
6
|
AND = '+'.freeze
|
7
7
|
NOT = '-'.freeze
|
8
8
|
PER_PAGE = 50.freeze
|
9
|
+
VALID_QUERY_CHARS = 'a-zA-Z0-9_\.,&-'.freeze
|
10
|
+
REMOVE_INVALID_CHARS_REGEX = Regexp.new('[^\s' + VALID_QUERY_CHARS + ']').freeze
|
9
11
|
|
10
12
|
attr_reader :parameters, :sorting
|
11
13
|
|
12
14
|
def self.add_scope(name, obj)
|
13
15
|
define_method(name) do |*args|
|
14
|
-
res = obj.respond_to?(:call) ?
|
16
|
+
res = obj.respond_to?(:call) ? instance_exec(*args, &obj) : obj
|
15
17
|
self
|
16
18
|
end
|
17
19
|
end
|
@@ -32,10 +34,16 @@ module Groovy
|
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
def inspect
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
# def inspect
|
38
|
+
# "<#{self.class.name} #{parameters}>"
|
39
|
+
# end
|
40
|
+
|
41
|
+
def find(id)
|
42
|
+
find_by(_id: id)
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_by(conditions)
|
46
|
+
where(conditions).limit(1).first
|
39
47
|
end
|
40
48
|
|
41
49
|
# http://groonga.org/docs/reference/grn_expr/query_syntax.html
|
@@ -51,7 +59,7 @@ module Groovy
|
|
51
59
|
add_param(AND + [key, val.max].join(':<=')) if val.max # gte
|
52
60
|
|
53
61
|
elsif val.is_a?(Regexp)
|
54
|
-
str = val.source.gsub(
|
62
|
+
str = val.source.gsub(REMOVE_INVALID_CHARS_REGEX, '')
|
55
63
|
param = val.source[0] == '^' ? ':^' : val.source[-1] == '$' ? ':$' : ':~' # starts with or regexp
|
56
64
|
add_param(AND + [key, str.downcase].join(param)) # regex must be downcase
|
57
65
|
|
@@ -85,7 +93,7 @@ module Groovy
|
|
85
93
|
add_param(AND + [key, val.max].join(':>=')) if val.max # lte, nil if range.max is -1
|
86
94
|
|
87
95
|
elsif val.is_a?(Regexp)
|
88
|
-
str = val.source.gsub(
|
96
|
+
str = val.source.gsub(REMOVE_INVALID_CHARS_REGEX, '')
|
89
97
|
param = val.source[0] == '^' ? ':^' : val.source[-1] == '$' ? ':$' : ':~' # starts with or regexp
|
90
98
|
add_param(NOT + [key, str.downcase].join(param)) # regex must be downcase
|
91
99
|
|
@@ -194,13 +202,8 @@ module Groovy
|
|
194
202
|
private
|
195
203
|
attr_reader :model, :table, :options
|
196
204
|
|
197
|
-
def clear_query
|
198
|
-
model.clear_query # sets @query to nil
|
199
|
-
end
|
200
|
-
|
201
205
|
def add_param(param)
|
202
206
|
if parameters.include?(param)
|
203
|
-
# clear_query
|
204
207
|
raise "Duplicate param: #{param}"
|
205
208
|
end
|
206
209
|
|
@@ -213,8 +216,6 @@ module Groovy
|
|
213
216
|
rescue Groonga::TooLargeOffset
|
214
217
|
# puts "Offset is higher than table size!"
|
215
218
|
[]
|
216
|
-
ensure
|
217
|
-
clear_query
|
218
219
|
end
|
219
220
|
|
220
221
|
def execute
|
@@ -252,8 +253,9 @@ module Groovy
|
|
252
253
|
end
|
253
254
|
|
254
255
|
def prepare_query
|
255
|
-
|
256
|
-
|
256
|
+
space_regex = Regexp.new('\s([' + VALID_QUERY_CHARS + '])')
|
257
|
+
query = parameters.join(' ').split(/ or /i).map do |part|
|
258
|
+
part.gsub(' ', ' ').gsub(space_regex, '\ \1')
|
257
259
|
end.join(' OR ').sub(/^-/, '_id:>0 -') #.gsub(' OR -', ' -')
|
258
260
|
end
|
259
261
|
end
|
data/lib/groovy/version.rb
CHANGED
data/spec/model_spec.rb
CHANGED
@@ -11,10 +11,6 @@ describe Groovy::Model do
|
|
11
11
|
Groovy.close('model_spec')
|
12
12
|
end
|
13
13
|
|
14
|
-
after :each do
|
15
|
-
TestProduct.clear_query # otherwise next test will fail
|
16
|
-
end
|
17
|
-
|
18
14
|
describe '.schema' do
|
19
15
|
end
|
20
16
|
|
@@ -43,9 +39,9 @@ describe Groovy::Model do
|
|
43
39
|
|
44
40
|
it 'does not duplicate params' do
|
45
41
|
res = TestProduct.with_name('foo').by_price_asc.cheapest
|
46
|
-
res.
|
47
|
-
expect(res.
|
48
|
-
expect(res.
|
42
|
+
expect(res).to be_a(Groovy::Query)
|
43
|
+
expect(res.parameters).to eq(["+name:foo"])
|
44
|
+
expect(res.sorting).to eq(:by=>[{:key=>"price", :order=>:asc}], :limit=>-1, :offset=>0)
|
49
45
|
end
|
50
46
|
|
51
47
|
end
|
data/spec/query_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe Groovy::Query do
|
|
5
5
|
before :all do
|
6
6
|
Groovy.open('tmp/querying', 'query_spec')
|
7
7
|
load_schema! 'query_spec'
|
8
|
-
@p1 = TestProduct.create!(name: "Product 1", visible: true, price: 10, tag_list: 'one, number two
|
8
|
+
@p1 = TestProduct.create!(name: "Product 1", visible: true, price: 10, tag_list: 'one, number two & three')
|
9
9
|
@p2 = TestProduct.create!(name: "Product 2", visible: false, price: 20, tag_list: 'number two, three')
|
10
10
|
@p3 = TestProduct.create!(name: "Product 3", visible: true, price: 30, tag_list: nil)
|
11
11
|
@p4 = TestProduct.create!(name: "Product 4", visible: false, price: 40, tag_list: 'one, number two')
|
@@ -48,6 +48,11 @@ describe Groovy::Query do
|
|
48
48
|
res = TestProduct.where(name: 'Product 2')
|
49
49
|
expect(res.map(&:id)).to eq([@p2.id])
|
50
50
|
end
|
51
|
+
|
52
|
+
it 'works with symbols' do
|
53
|
+
res = TestProduct.where(tag_list: 'one, number two & three')
|
54
|
+
expect(res.map(&:id)).to eq([@p1.id])
|
55
|
+
end
|
51
56
|
end
|
52
57
|
|
53
58
|
context 'basic regex' do
|
@@ -60,6 +65,11 @@ describe Groovy::Query do
|
|
60
65
|
res = TestProduct.where(tag_list: /TWO/)
|
61
66
|
expect(res.map(&:id)).to eq([@p1.id, @p2.id, @p4.id])
|
62
67
|
end
|
68
|
+
|
69
|
+
it 'works with symbols' do
|
70
|
+
res = TestProduct.where(tag_list: /two & three/)
|
71
|
+
expect(res.map(&:id)).to eq([@p1.id])
|
72
|
+
end
|
63
73
|
end
|
64
74
|
|
65
75
|
describe 'starts with regex' do
|
@@ -138,6 +148,11 @@ describe Groovy::Query do
|
|
138
148
|
res = TestProduct.not(name: 'Product 2')
|
139
149
|
expect(res.map(&:id)).to eq([@p1.id, @p3.id, @p4.id, @p5.id])
|
140
150
|
end
|
151
|
+
|
152
|
+
it 'works with symbols' do
|
153
|
+
res = TestProduct.not(tag_list: 'one, number two & three')
|
154
|
+
expect(res.map(&:id)).to eq([@p2.id, @p3.id, @p4.id, @p5.id])
|
155
|
+
end
|
141
156
|
end
|
142
157
|
|
143
158
|
context 'basic regex' do
|
@@ -150,6 +165,11 @@ describe Groovy::Query do
|
|
150
165
|
res = TestProduct.not(tag_list: /TWO/)
|
151
166
|
expect(res.map(&:id)).to eq([@p3.id, @p5.id])
|
152
167
|
end
|
168
|
+
|
169
|
+
it 'works with symbols' do
|
170
|
+
res = TestProduct.not(tag_list: /two & three/)
|
171
|
+
expect(res.map(&:id)).to eq([@p2.id, @p3.id, @p4.id, @p5.id])
|
172
|
+
end
|
153
173
|
end
|
154
174
|
|
155
175
|
describe 'starts with regex' 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.2.
|
4
|
+
version: 0.2.2
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rroonga
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '9.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '9.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.7.
|
122
|
+
rubygems_version: 2.7.6
|
123
123
|
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: A wrapper around Groonga/Rroonga
|