sewell 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -19,14 +19,14 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  ``` ruby
22
- query = Sewell.generate 'sena:airi OR mashiro AND nuko:buta', %w{sena uryu nuko} #=> ( sena:@airi ) OR ( sena:@mashiro OR uryu:@mashiro OR nuko:@mashiro ) AND ( nuko:@buta )
22
+ query = Sewell.generate 'sena:airi OR mashiro AND nuko:buta', %w{sena uryu nuko} #=> ( sena:@airi ) OR ( sena:@mashiro OR uryu:@mashiro OR nuko:@mashiro ) + ( nuko:@buta )
23
23
  Groonga['SenaAiri'].select(query)
24
24
 
25
- Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND') #=> ( sena:@airi OR sena:@huro ) AND ( nuko:@trape )
25
+ Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND') #=> ( sena:@airi OR sena:@huro ) + ( 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
+ Sewell.generate({mashiro: '-inui airi'}) #=> ( mashiro:!inui + mashiro:@airi )
29
+ Sewell.generate('-inui airi', ['mashiro']) #=> ( mashiro:!inui + mashiro:@airi )
30
30
  ```
31
31
 
32
32
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module Sewell
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/sewell.rb CHANGED
@@ -17,7 +17,12 @@ module Sewell
17
17
 
18
18
  private
19
19
 
20
- def self.from_hash hash, sep
20
+ def self.from_hash hash, s
21
+ if s == 'OR'
22
+ sep = 'OR'
23
+ else
24
+ sep = '+'
25
+ end
21
26
  hash.map{|k,v|
22
27
  '( ' + build(v.split(' ').map{|x|
23
28
  if x == 'OR' or x == 'AND'
@@ -59,14 +64,21 @@ module Sewell
59
64
  build q
60
65
  end
61
66
 
62
- def self.build q
67
+ def self.build q, *s
68
+ if s.first == 'OR'
69
+ sep = 'OR'
70
+ else
71
+ sep = '+'
72
+ end
63
73
  query = ''
64
74
  q.each_with_index{|x,i|
65
75
  next if x == 'OR' or x == 'AND'
66
- if q[i+1] == 'OR' or q[i+1] == 'AND'
67
- query += "#{x} #{q[i+1]} "
76
+ if q[i+1] == 'OR'
77
+ query += "#{x} OR "
78
+ elsif q[i+1] == 'AND'
79
+ query += "#{x} + "
68
80
  elsif q[i+1] != nil
69
- query += "#{x} AND "
81
+ query += "#{x} #{sep} "
70
82
  else
71
83
  if x != 'AND' and x != 'OR'
72
84
  query += x
@@ -7,12 +7,12 @@ describe Sewell do
7
7
  end
8
8
 
9
9
  it 'can generate from string' do
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 )'
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 ) + ( nuko:@buta )'
11
+ Sewell.generate('-inui airi', ['mashiro']).should == '( mashiro:!inui ) + ( mashiro:@airi )'
12
12
  end
13
13
 
14
14
  it 'can generate from hash' do
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
+ Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND').should == '( sena:@airi OR sena:@huro ) + ( nuko:@trape )'
16
+ Sewell.generate({mashiro: '-inui airi'}, 'AND').should == '( mashiro:!inui + mashiro:@airi )'
17
17
  end
18
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.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: