ilike 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/README.md +16 -3
- data/ilike.gemspec +1 -0
- data/lib/ilike.rb +7 -4
- data/lib/ilike/arel_template.rb +21 -0
- data/lib/ilike/version.rb +1 -1
- data/spec/lib/ilike/arel_template_spec.rb +23 -0
- data/spec/lib/ilike_spec.rb +24 -5
- metadata +56 -15
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
CHANGED
@@ -8,9 +8,21 @@ write code that doesn't depend explicitly on your db adapter:
|
|
8
8
|
```ruby
|
9
9
|
class Product < ActiveRecord::Base; end
|
10
10
|
|
11
|
-
Product.create(:name => '
|
12
|
-
Product.
|
13
|
-
|
11
|
+
Product.create(:name => 'Pinot wine', :origin => 'France', :price => 13.50)
|
12
|
+
Product.create(:name => 'Barolo wine', :origin => 'Italy', :price => 12.50)
|
13
|
+
|
14
|
+
# basic search:
|
15
|
+
Product.ilike(:name => '%wine')
|
16
|
+
# => [#<Product id: 2, name: "Pinot wine", origin: "France", price: #<BigDecimal:105b70880,'0.135E2',18(18)>>,
|
17
|
+
#<Product id: 3, name: "Barolo wine", origin: "Italy", price: #<BigDecimal:105b705d8,'0.125E2',18(18)>>]
|
18
|
+
|
19
|
+
# search on multiple fields:
|
20
|
+
Product.ilike(:name => '%wine%', :origin => '%Ita%')
|
21
|
+
# => [#<Product id: 3, name: "Barolo wine", origin: "Italy", price: #<BigDecimal:1059677a0,'0.125E2',18(18)>>]
|
22
|
+
|
23
|
+
# search with multiple keywords on the same field:
|
24
|
+
Product.ilike(:name => ['%wine', '%barolo%')
|
25
|
+
# => [#<Product id: 3, name: "Barolo wine", origin: "Italy", price: #<BigDecimal:1059fe998,'0.125E2',18(18)>>]
|
14
26
|
```
|
15
27
|
|
16
28
|
|
@@ -28,6 +40,7 @@ Or install it yourself as:
|
|
28
40
|
|
29
41
|
$ gem install liker
|
30
42
|
|
43
|
+
|
31
44
|
## Contributing
|
32
45
|
|
33
46
|
1. Fork it
|
data/ilike.gemspec
CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Ilike::VERSION
|
17
17
|
|
18
|
+
gem.add_dependency 'rake'
|
18
19
|
gem.add_dependency 'activerecord', '>= 3.0'
|
19
20
|
gem.add_development_dependency 'rspec'
|
20
21
|
gem.add_development_dependency 'guard-rspec'
|
data/lib/ilike.rb
CHANGED
@@ -2,10 +2,13 @@ require 'ilike/version'
|
|
2
2
|
require 'active_record'
|
3
3
|
|
4
4
|
module Ilike
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
autoload :ArelTemplate, 'ilike/arel_template'
|
6
|
+
|
7
|
+
def ilike(conditions)
|
8
|
+
code = conditions.inject [] do |parts, arr|
|
9
|
+
parts << ArelTemplate.new(arr).build
|
10
|
+
end.flatten.join('.and')
|
11
|
+
class_eval "where(#{code})"
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Ilike
|
2
|
+
class ArelTemplate
|
3
|
+
attr_reader :field, :values
|
4
|
+
|
5
|
+
def initialize(arr)
|
6
|
+
@field, @values = arr
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
if Array === values
|
11
|
+
values.map {|value| template(value)}
|
12
|
+
else
|
13
|
+
template(values)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def template(value)
|
18
|
+
"(arel_table[:#{field}].matches('#{value}'))"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/ilike/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Ilike
|
4
|
+
describe ArelTemplate do
|
5
|
+
let(:template) { ArelTemplate.new([:name, 'andrea%'])}
|
6
|
+
let(:multi_template) { ArelTemplate.new([:name, ['%andrea%', '%longhi%']])}
|
7
|
+
|
8
|
+
it 'template builds the basic code for arel' do
|
9
|
+
template.template('andrea%').should == "(arel_table[:name].matches('andrea%'))"
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when passing multiple keywords for field' do
|
13
|
+
it 'builds an array of templates' do
|
14
|
+
multi_template.build.should be_an(Array)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'builds expected templates' do
|
18
|
+
expected = ["(arel_table[:name].matches('%andrea%'))", "(arel_table[:name].matches('%longhi%'))"]
|
19
|
+
multi_template.build.should == expected
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/lib/ilike_spec.rb
CHANGED
@@ -10,16 +10,22 @@ describe Ilike do
|
|
10
10
|
)
|
11
11
|
ActiveRecord::Schema.define do
|
12
12
|
create_table :products do |t|
|
13
|
-
t.string :name,
|
14
|
-
t.
|
13
|
+
t.string :name, :null => false
|
14
|
+
t.string :origin, :null => false
|
15
|
+
t.decimal :price, :null => false
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
20
|
before do
|
20
21
|
Product.delete_all
|
21
|
-
@pasta = Product.create(:name => 'Spaghetti Pasta', :price => 1.00)
|
22
|
-
@
|
22
|
+
@pasta = Product.create(:name => 'Spaghetti Pasta', :origin => 'Italy', :price => 1.00)
|
23
|
+
@pinot = Product.create(:name => 'Pinot wine', :origin => 'France', :price => 13.50)
|
24
|
+
@barolo = Product.create(:name => 'Barolo wine', :origin => 'Italy', :price => 12.50)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'finds records that end with the requested string' do
|
28
|
+
Product.ilike(:name => '%pasta').should == [@pasta]
|
23
29
|
end
|
24
30
|
|
25
31
|
it 'finds records that end with the requested string' do
|
@@ -27,6 +33,19 @@ describe Ilike do
|
|
27
33
|
end
|
28
34
|
|
29
35
|
it 'finds records that contain the requested string' do
|
30
|
-
Product.ilike(:name => '%
|
36
|
+
Product.ilike(:name => '%olo%').should == [@barolo]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'finds records that match all matchers' do
|
40
|
+
Product.ilike(:name => '%wine', :origin => '%fra%').should == [@pinot]
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'searches with all options' do
|
44
|
+
Product.ilike(:name => ['%wine%', '%barolo%']).should == [@barolo]
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'builds the expected sql' do
|
48
|
+
sql = 'SELECT "products".* FROM "products" WHERE ("products"."name" LIKE \'%wine%\' AND "products"."name" LIKE \'%barolo%\')'
|
49
|
+
Product.ilike(:name => ['%wine%', '%barolo%']).to_sql.should == sql
|
31
50
|
end
|
32
51
|
end
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ilike
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- andrea longhi
|
@@ -10,52 +15,79 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2012-12-
|
18
|
+
date: 2012-12-12 00:00:00 Z
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
21
|
+
name: rake
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
requirement: *id001
|
17
33
|
prerelease: false
|
18
|
-
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activerecord
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
19
37
|
none: false
|
20
38
|
requirements:
|
21
39
|
- - ">="
|
22
40
|
- !ruby/object:Gem::Version
|
41
|
+
hash: 7
|
42
|
+
segments:
|
43
|
+
- 3
|
44
|
+
- 0
|
23
45
|
version: "3.0"
|
24
46
|
type: :runtime
|
25
|
-
|
47
|
+
requirement: *id002
|
48
|
+
prerelease: false
|
26
49
|
- !ruby/object:Gem::Dependency
|
27
50
|
name: rspec
|
28
|
-
|
29
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
30
52
|
none: false
|
31
53
|
requirements:
|
32
54
|
- - ">="
|
33
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
34
59
|
version: "0"
|
35
60
|
type: :development
|
36
|
-
|
61
|
+
requirement: *id003
|
62
|
+
prerelease: false
|
37
63
|
- !ruby/object:Gem::Dependency
|
38
64
|
name: guard-rspec
|
39
|
-
|
40
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
65
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
41
66
|
none: false
|
42
67
|
requirements:
|
43
68
|
- - ">="
|
44
69
|
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
45
73
|
version: "0"
|
46
74
|
type: :development
|
47
|
-
|
75
|
+
requirement: *id004
|
76
|
+
prerelease: false
|
48
77
|
- !ruby/object:Gem::Dependency
|
49
78
|
name: sqlite3
|
50
|
-
|
51
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
79
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
52
80
|
none: false
|
53
81
|
requirements:
|
54
82
|
- - ">="
|
55
83
|
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
56
87
|
version: "0"
|
57
88
|
type: :development
|
58
|
-
|
89
|
+
requirement: *id005
|
90
|
+
prerelease: false
|
59
91
|
description: easy way to match strings with active record
|
60
92
|
email:
|
61
93
|
- andrea@spaghetticode.it
|
@@ -67,6 +99,7 @@ extra_rdoc_files: []
|
|
67
99
|
|
68
100
|
files:
|
69
101
|
- .gitignore
|
102
|
+
- .rspec
|
70
103
|
- Gemfile
|
71
104
|
- Guardfile
|
72
105
|
- LICENSE
|
@@ -74,7 +107,9 @@ files:
|
|
74
107
|
- Rakefile
|
75
108
|
- ilike.gemspec
|
76
109
|
- lib/ilike.rb
|
110
|
+
- lib/ilike/arel_template.rb
|
77
111
|
- lib/ilike/version.rb
|
112
|
+
- spec/lib/ilike/arel_template_spec.rb
|
78
113
|
- spec/lib/ilike_spec.rb
|
79
114
|
- spec/spec_helper.rb
|
80
115
|
homepage: https://github.com/spaghetticode/ilike
|
@@ -90,12 +125,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
125
|
requirements:
|
91
126
|
- - ">="
|
92
127
|
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
93
131
|
version: "0"
|
94
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
133
|
none: false
|
96
134
|
requirements:
|
97
135
|
- - ">="
|
98
136
|
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
99
140
|
version: "0"
|
100
141
|
requirements: []
|
101
142
|
|
@@ -105,6 +146,6 @@ signing_key:
|
|
105
146
|
specification_version: 3
|
106
147
|
summary: easy way to match strings with active record
|
107
148
|
test_files:
|
149
|
+
- spec/lib/ilike/arel_template_spec.rb
|
108
150
|
- spec/lib/ilike_spec.rb
|
109
151
|
- spec/spec_helper.rb
|
110
|
-
has_rdoc:
|