sewell 0.0.1 → 0.0.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Sewell
2
2
 
3
- TODO: Write a gem description
3
+ Generator for Groonga's query.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,14 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
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 )
23
+ Groonga['SenaAiri'].select(query)
24
+
25
+ Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND') #=> ( sena:@airi OR sena:@huro ) AND ( nuko:@trape )
26
+ Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'OR') #=> ( sena:@airi OR sena:@huro ) OR ( nuko:@trape )
27
+
28
+ ```
22
29
 
23
30
  ## Contributing
24
31
 
@@ -1,3 +1,3 @@
1
1
  module Sewell
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/sewell.rb CHANGED
@@ -3,11 +3,13 @@ require "sewell/version"
3
3
 
4
4
  module Sewell
5
5
  # Your code goes here...
6
- def self.generate args, tables
7
- raise TypeError unless tables.class == Array
6
+ def self.generate args, args2
8
7
  case args.class.to_s
9
8
  when 'String'
10
- from_str args, tables
9
+ raise TypeError unless args2.class == Array
10
+ from_str args, args2
11
+ when 'Hash'
12
+ from_hash args, args2
11
13
  else
12
14
  raise TypeError
13
15
  end
@@ -15,6 +17,18 @@ module Sewell
15
17
 
16
18
  private
17
19
 
20
+ def self.from_hash hash, sep
21
+ hash.map{|k,v|
22
+ '( ' + v.split(' ').map{|x|
23
+ if x == 'OR' or x == 'AND'
24
+ x
25
+ else
26
+ "#{k}:@#{sanitize x, /#{k}\:/}"
27
+ end
28
+ }.join(' ') + ' )'
29
+ }.join " #{sep} "
30
+ end
31
+
18
32
  def self.from_str str, tables
19
33
  str.gsub!(/ /, ' ')
20
34
  q = []
@@ -9,4 +9,8 @@ describe Sewell do
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
11
  end
12
+
13
+ it 'can generate from hash' do
14
+ Sewell.generate({sena: 'airi OR huro', nuko: 'trape'}, 'AND').should == '( sena:@airi OR sena:@huro ) AND ( nuko:@trape )'
15
+ end
12
16
  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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: