sewell 0.0.2 → 0.0.3
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.md +2 -0
- data/lib/sewell/version.rb +1 -1
- data/lib/sewell.rb +37 -6
- data/spec/sewell/sewell_spec.rb +2 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -25,6 +25,8 @@ Or install it yourself as:
|
|
25
25
|
Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND') #=> ( sena:@airi OR sena:@huro ) AND ( nuko:@trape )
|
26
26
|
Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'OR') #=> ( sena:@airi OR sena:@huro ) OR ( nuko:@trape )
|
27
27
|
|
28
|
+
Sewell.generate({mashiro: '-inui airi'}) #=> ( mashiro:!inui AND mashiro:@airi )
|
29
|
+
Sewell.generate('-inui airi', ['mashiro']) #=> ( mashiro:!inui AND mashiro:@airi )
|
28
30
|
```
|
29
31
|
|
30
32
|
## Contributing
|
data/lib/sewell/version.rb
CHANGED
data/lib/sewell.rb
CHANGED
@@ -19,13 +19,18 @@ module Sewell
|
|
19
19
|
|
20
20
|
def self.from_hash hash, sep
|
21
21
|
hash.map{|k,v|
|
22
|
-
'( ' + v.split(' ').map{|x|
|
22
|
+
'( ' + build(v.split(' ').map{|x|
|
23
23
|
if x == 'OR' or x == 'AND'
|
24
24
|
x
|
25
25
|
else
|
26
|
-
|
26
|
+
if x.split('').first == '-'
|
27
|
+
x.sub!(/^-/, '')
|
28
|
+
"#{k}:!#{sanitize x, /#{k}\:/}"
|
29
|
+
else
|
30
|
+
"#{k}:@#{sanitize x, /#{k}\:/}"
|
31
|
+
end
|
27
32
|
end
|
28
|
-
}
|
33
|
+
}) + ' )'
|
29
34
|
}.join " #{sep} "
|
30
35
|
end
|
31
36
|
|
@@ -37,12 +42,38 @@ module Sewell
|
|
37
42
|
if x.scan(/:/).count == 1
|
38
43
|
table = x.split(':').first
|
39
44
|
word = x.split(':').last
|
40
|
-
|
45
|
+
if word.split('').first == '-'
|
46
|
+
q << "( #{table}:!#{sanitize word, /#{table}\:/} )"
|
47
|
+
else
|
48
|
+
q << "( #{table}:@#{sanitize word, /#{table}\:/} )"
|
49
|
+
end
|
50
|
+
else
|
51
|
+
if x.split('').first == '-'
|
52
|
+
x.sub!(/^-/, '')
|
53
|
+
q << '( ' + tables.map{|t| "#{t}:!#{sanitize(x)}"}.join(' AND ') + ' )'
|
54
|
+
else
|
55
|
+
q << '( ' + tables.map{|t| "#{t}:@#{sanitize(x)}"}.join(' OR ') + ' )'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
}
|
59
|
+
build q
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.build q
|
63
|
+
query = ''
|
64
|
+
q.each_with_index{|x,i|
|
65
|
+
next if x == 'OR' or x == 'AND'
|
66
|
+
if q[i+1] == 'OR' or q[i+1] == 'AND'
|
67
|
+
query += "#{x} #{q[i+1]} "
|
68
|
+
elsif q[i+1] != nil
|
69
|
+
query += "#{x} AND "
|
41
70
|
else
|
42
|
-
|
71
|
+
if x != 'AND' and x != 'OR'
|
72
|
+
query += x
|
73
|
+
end
|
43
74
|
end
|
44
75
|
}
|
45
|
-
|
76
|
+
query
|
46
77
|
end
|
47
78
|
|
48
79
|
def self.sanitize query, *ex
|
data/spec/sewell/sewell_spec.rb
CHANGED
@@ -8,9 +8,11 @@ describe Sewell do
|
|
8
8
|
|
9
9
|
it 'can generate from string' do
|
10
10
|
Sewell.generate('sena:airi OR mashiro AND nuko:buta', %w{sena uryu nuko}).should == '( sena:@airi ) OR ( sena:@mashiro OR uryu:@mashiro OR nuko:@mashiro ) AND ( nuko:@buta )'
|
11
|
+
Sewell.generate('-inui airi', ['mashiro']).should == '( mashiro:!inui ) AND ( mashiro:@airi )'
|
11
12
|
end
|
12
13
|
|
13
14
|
it 'can generate from hash' do
|
14
15
|
Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND').should == '( sena:@airi OR sena:@huro ) AND ( nuko:@trape )'
|
16
|
+
Sewell.generate({mashiro: '-inui airi'}, 'AND').should == '( mashiro:!inui AND mashiro:@airi )'
|
15
17
|
end
|
16
18
|
end
|