searrrch 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -4
- data/lib/searrrch.rb +15 -3
- data/spec/operators_spec.rb +20 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e77f9cbe9ea3dcc47a1e5f2131d7b14c23941a2
|
4
|
+
data.tar.gz: 2d6c952923e46581935cb4198dfb95fc9e286814
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff903fa346b90435014938dca517748ebe8b53ad127bb8758bac65af8e1df5aa1c950103a4d88639aae85857c86f9ce636ee7be021849a0319d2fe5f534f78e0
|
7
|
+
data.tar.gz: ba11675321951b27fe94ad6091b5df4220db246bb1594f4488d5b2cb401d7ec585f36c40b17bf69ff70c9e977db2dec607ee7ac4fe4f62d44374936569b9933f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -16,17 +16,29 @@ The user can also put the value between quotes, in which case nested escaped quo
|
|
16
16
|
Usage:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
|
20
|
-
search
|
21
|
-
search.each_value('user_id', :integer) do |val|
|
19
|
+
search = Searrrch.new 'user_id: 123 user_id: 124 free text here'
|
20
|
+
search.each_value(:user_id, :integer) do |val|
|
22
21
|
# this block will be called twice with val being set to '123' and '124'
|
23
22
|
end
|
24
23
|
|
25
24
|
# this return all values from user_id in an array
|
26
|
-
search.to_array(
|
25
|
+
search.to_array(:user_id)
|
26
|
+
|
27
|
+
# or, if you preffer:
|
28
|
+
search.as_array(:user_id) do |user_ids|
|
29
|
+
# this is called once and only if user_id is set
|
30
|
+
end
|
27
31
|
|
28
32
|
# this returns the remaining text
|
29
33
|
search.freetext
|
34
|
+
|
35
|
+
|
36
|
+
# as a final note, this also works:
|
37
|
+
|
38
|
+
search = Searrrch.new 'user_id: 123,124 free text here', true
|
39
|
+
search.each_value(:user_id, :integer) do |val|
|
40
|
+
# the same as previous example; but now every value get cut on ','
|
41
|
+
end
|
30
42
|
```
|
31
43
|
|
32
44
|
This is particular useful for appending search criteria to your rails query ;)
|
data/lib/searrrch.rb
CHANGED
@@ -12,7 +12,7 @@ class Searrrch
|
|
12
12
|
# also support ',' for you cool kids that expect something like a "list of ids"
|
13
13
|
# 3. and also accept any char if quoted - in which case the same quotation should be quoted as well
|
14
14
|
|
15
|
-
VERSION = '0.0.
|
15
|
+
VERSION = '0.0.4'
|
16
16
|
|
17
17
|
# iterates over the entire string identifying each of the elements
|
18
18
|
# this code only checks for:
|
@@ -25,7 +25,7 @@ class Searrrch
|
|
25
25
|
# Both key and value must have only the other regular chars.
|
26
26
|
#
|
27
27
|
# Everything after the last option will be considered free text search
|
28
|
-
def initialize(query)
|
28
|
+
def initialize(query, explode_comma=false)
|
29
29
|
query = query.to_s
|
30
30
|
@operators = {}
|
31
31
|
|
@@ -36,7 +36,12 @@ class Searrrch
|
|
36
36
|
value = value[1, value.length - 2] if ["'", '"'].include?(value[0])
|
37
37
|
offset = m.end(2)
|
38
38
|
@operators[key] ||= []
|
39
|
-
|
39
|
+
|
40
|
+
if explode_comma
|
41
|
+
value.split(',').each{ |v| @operators[key] << v }
|
42
|
+
else
|
43
|
+
@operators[key] << value
|
44
|
+
end
|
40
45
|
end
|
41
46
|
@freetext = query[offset, query.length].strip
|
42
47
|
end
|
@@ -56,6 +61,13 @@ class Searrrch
|
|
56
61
|
res
|
57
62
|
end
|
58
63
|
|
64
|
+
# Same as to_array, but yield the value of the array to a block if a value is
|
65
|
+
# found
|
66
|
+
def as_array(key, expects = :string)
|
67
|
+
arr =to_array(key, expects)
|
68
|
+
yield arr if arr.length > 0
|
69
|
+
end
|
70
|
+
|
59
71
|
def freetext(expects = :string)
|
60
72
|
convert(@freetext, expects)
|
61
73
|
end
|
data/spec/operators_spec.rb
CHANGED
@@ -87,4 +87,24 @@ RSpec.describe 'operators' do
|
|
87
87
|
search = Searrrch.new query
|
88
88
|
expect(search.to_array(:operator)).to eq values
|
89
89
|
end
|
90
|
+
|
91
|
+
it 'explodes the comma' do
|
92
|
+
query = 'user_id: 1,2,3,4'
|
93
|
+
search = Searrrch.new query, true
|
94
|
+
expect(search.to_array(:user_id, :integer)).to eq [1,2,3,4]
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'uses as_array instead of to_array' do
|
98
|
+
query = 'user_id: 1,2,3,4'
|
99
|
+
search = Searrrch.new query, true
|
100
|
+
search.as_array(:user_id, :integer) do |value|
|
101
|
+
expect(value).to eq [1,2,3,4]
|
102
|
+
end
|
103
|
+
|
104
|
+
search.as_array(:unset_value, :integer) do |value|
|
105
|
+
# this is an error .. meaning if this guy is called, fails!
|
106
|
+
# idk how to do this yet using rspec, so I am using this ugly but effective shortcut
|
107
|
+
expect(0).to eq [1,2,3,4]
|
108
|
+
end
|
109
|
+
end
|
90
110
|
end
|