sewell 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,3 +1,3 @@
1
1
  module Sewell
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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
- "#{k}:@#{sanitize x, /#{k}\:/}"
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
- }.join(' ') + ' )'
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
- q << "( #{table}:@#{sanitize word, /#{table}\:/} )"
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
- q << '( ' + tables.map{|t| "#{t}:@#{sanitize(x)}"}.join(' OR ') + ' )'
71
+ if x != 'AND' and x != 'OR'
72
+ query += x
73
+ end
43
74
  end
44
75
  }
45
- q.join ' '
76
+ query
46
77
  end
47
78
 
48
79
  def self.sanitize query, *ex
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sewell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: