everywhere 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -6
- data/everywhere.gemspec +1 -1
- data/lib/everywhere/hash_key.rb +20 -14
- data/lib/everywhere/hash_value.rb +16 -10
- data/lib/everywhere/method.rb +9 -1
- data/lib/everywhere/symbol.rb +7 -5
- data/lib/everywhere/version.rb +1 -1
- data/spec/hash_key_spec.rb +12 -0
- data/spec/hash_value_spec.rb +12 -0
- data/spec/method_spec.rb +12 -0
- data/spec/symbol_spec.rb +12 -0
- metadata +8 -8
data/README.rdoc
CHANGED
@@ -19,10 +19,17 @@ Same for where + like.
|
|
19
19
|
|
20
20
|
SELECT "users".* FROM "users" WHERE ("users"."name" LIKE 'Akira%')
|
21
21
|
|
22
|
+
=== where + not like
|
23
|
+
|
24
|
+
And where + "not like" as well.
|
25
|
+
|
26
|
+
SELECT "users".* FROM "users" WHERE ("users"."name" NOT LIKE 'Matz%')
|
27
|
+
|
22
28
|
|
23
29
|
== Syntaxes
|
24
30
|
|
25
|
-
"+Everywhere+" supports 4 syntaxes.
|
31
|
+
"+Everywhere+" supports 4 syntaxes. Note that you can use only one syntax at a time, and others will be disabled.
|
32
|
+
The +hash_value+ syntax will be enabled by default.
|
26
33
|
|
27
34
|
* hash_value
|
28
35
|
Push the value into a Hash indexed by +:not+.
|
@@ -72,11 +79,6 @@ For example, if you prefer the symbol syntax, put the following line in your con
|
|
72
79
|
The default value is +:hash_value+.
|
73
80
|
|
74
81
|
|
75
|
-
== Todo
|
76
|
-
|
77
|
-
+not like+ (if needed)
|
78
|
-
|
79
|
-
|
80
82
|
== Contributing to Everywhere
|
81
83
|
|
82
84
|
* Fork, fix, then send me a pull request.
|
data/everywhere.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Everywhere::VERSION
|
8
8
|
s.authors = ["Akira Matsuda"]
|
9
9
|
s.email = ["ronnie@dio.jp"]
|
10
|
-
s.homepage =
|
10
|
+
s.homepage = 'https://github.com/amatsuda/everywhere'
|
11
11
|
s.summary = %q{Hash condition syntax for AR query everywhere!}
|
12
12
|
s.description = %q{Hash condition syntax for AR query everywhere!}
|
13
13
|
|
data/lib/everywhere/hash_key.rb
CHANGED
@@ -6,60 +6,66 @@ module ActiveRecord
|
|
6
6
|
class << self
|
7
7
|
include Everywhere::Util
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def build_from_hash_with_not_and_like_and_not_like(engine, attributes, default_table)
|
10
|
+
attributes_with_not_and_like_and_not_like = {}
|
11
11
|
attributes.each do |column, value|
|
12
12
|
# {not: {key: value}}
|
13
|
-
if column.in?([:not, :like])
|
13
|
+
if column.in?([:not, :like, :not_like])
|
14
14
|
value.each do |k, v|
|
15
|
-
|
15
|
+
attributes_with_not_and_like_and_not_like["#{k}__#{column}__"] = v
|
16
16
|
end
|
17
17
|
else
|
18
|
-
|
18
|
+
attributes_with_not_and_like_and_not_like[column] = value
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
build_from_hash_without_not_and_like_and_not_like(engine, attributes_with_not_and_like_and_not_like, default_table).map do |rel|
|
22
22
|
if rel.left.name.to_s.ends_with? '__not__'
|
23
23
|
rel.left.name = rel.left.name.to_s.sub(/__not__$/, '').to_sym
|
24
24
|
negate rel
|
25
25
|
elsif rel.left.name.to_s.ends_with? '__like__'
|
26
26
|
rel.left.name = rel.left.name.to_s.sub(/__like__$/, '').to_sym
|
27
27
|
Arel::Nodes::Matches.new rel.left, rel.right
|
28
|
+
elsif rel.left.name.to_s.ends_with? '__not_like__'
|
29
|
+
rel.left.name = rel.left.name.to_s.sub(/__not_like__$/, '').to_sym
|
30
|
+
Arel::Nodes::DoesNotMatch.new rel.left, rel.right
|
28
31
|
else
|
29
32
|
rel
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
33
|
-
alias_method_chain :build_from_hash, :
|
36
|
+
alias_method_chain :build_from_hash, :not_and_like_and_not_like
|
34
37
|
end
|
35
38
|
else
|
36
39
|
include Everywhere::Util
|
37
40
|
|
38
|
-
def
|
39
|
-
|
41
|
+
def build_from_hash_with_not_and_like_and_not_like(attributes, default_table)
|
42
|
+
attributes_with_not_and_like_and_not_like = {}
|
40
43
|
attributes.each do |column, value|
|
41
44
|
# {not: {key: value}}
|
42
|
-
if (column == :not) || (column == :like)
|
45
|
+
if (column == :not) || (column == :like) || (column == :not_like)
|
43
46
|
value.each do |k, v|
|
44
|
-
|
47
|
+
attributes_with_not_and_like_and_not_like["#{k}__#{column}__"] = v
|
45
48
|
end
|
46
49
|
else
|
47
|
-
|
50
|
+
attributes_with_not_and_like_and_not_like[column] = value
|
48
51
|
end
|
49
52
|
end
|
50
|
-
|
53
|
+
build_from_hash_without_not_and_like_and_not_like(attributes_with_not_and_like_and_not_like, default_table).map do |rel|
|
51
54
|
if rel.left.name.to_s.ends_with? '__not__'
|
52
55
|
rel.left.name = rel.left.name.to_s.sub(/__not__$/, '').to_sym
|
53
56
|
negate rel
|
54
57
|
elsif rel.left.name.to_s.ends_with? '__like__'
|
55
58
|
rel.left.name = rel.left.name.to_s.sub(/__like__$/, '').to_sym
|
56
59
|
Arel::Nodes::Matches.new rel.left, rel.right
|
60
|
+
elsif rel.left.name.to_s.ends_with? '__not_like__'
|
61
|
+
rel.left.name = rel.left.name.to_s.sub(/__not_like__$/, '').to_sym
|
62
|
+
Arel::Nodes::DoesNotMatch.new rel.left, rel.right
|
57
63
|
else
|
58
64
|
rel
|
59
65
|
end
|
60
66
|
end
|
61
67
|
end
|
62
|
-
alias_method_chain :build_from_hash, :
|
68
|
+
alias_method_chain :build_from_hash, :not_and_like_and_not_like
|
63
69
|
end
|
64
70
|
end
|
65
71
|
end
|
@@ -6,54 +6,60 @@ module ActiveRecord
|
|
6
6
|
class << self
|
7
7
|
include Everywhere::Util
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def build_from_hash_with_not_and_like_and_not_like(engine, attributes, default_table)
|
10
|
+
attributes_with_not_and_like_and_not_like = attributes.map do |column, value|
|
11
11
|
# {key: {not: value}}
|
12
|
-
if value.is_a?(Hash) && (value.keys.size == 1) && value.keys.first.in?([:not, :like])
|
12
|
+
if value.is_a?(Hash) && (value.keys.size == 1) && value.keys.first.in?([:not, :like, :not_like])
|
13
13
|
["#{column}__#{value.keys.first}__", value.values.first]
|
14
14
|
else
|
15
15
|
[column, value]
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
build_from_hash_without_not_and_like_and_not_like(engine, attributes_with_not_and_like_and_not_like, default_table).map do |rel|
|
19
19
|
if rel.left.name.to_s.ends_with?('__not__')
|
20
20
|
rel.left.name = rel.left.name.to_s.sub(/__not__$/, '').to_sym
|
21
21
|
negate rel
|
22
22
|
elsif rel.left.name.to_s.ends_with?('__like__')
|
23
23
|
rel.left.name = rel.left.name.to_s.sub(/__like__$/, '').to_sym
|
24
24
|
Arel::Nodes::Matches.new rel.left, rel.right
|
25
|
+
elsif rel.left.name.to_s.ends_with?('__not_like__')
|
26
|
+
rel.left.name = rel.left.name.to_s.sub(/__not_like__$/, '').to_sym
|
27
|
+
Arel::Nodes::DoesNotMatch.new rel.left, rel.right
|
25
28
|
else
|
26
29
|
rel
|
27
30
|
end
|
28
31
|
end
|
29
32
|
end
|
30
|
-
alias_method_chain :build_from_hash, :
|
33
|
+
alias_method_chain :build_from_hash, :not_and_like_and_not_like
|
31
34
|
end
|
32
35
|
else
|
33
36
|
include Everywhere::Util
|
34
37
|
|
35
|
-
def
|
36
|
-
|
38
|
+
def build_from_hash_with_not_and_like_and_not_like(attributes, default_table)
|
39
|
+
attributes_with_not_and_like_and_not_like = attributes.map do |column, value|
|
37
40
|
# {key: {not: value}}
|
38
|
-
if value.is_a?(Hash) && (value.keys.size == 1) && ((value.keys.first == :not) || (value.keys.first == :like))
|
41
|
+
if value.is_a?(Hash) && (value.keys.size == 1) && ((value.keys.first == :not) || (value.keys.first == :like) || (value.keys.first == :not_like))
|
39
42
|
["#{column}__#{value.keys.first}__", value.values.first]
|
40
43
|
else
|
41
44
|
[column, value]
|
42
45
|
end
|
43
46
|
end
|
44
|
-
|
47
|
+
build_from_hash_without_not_and_like_and_not_like(attributes_with_not_and_like_and_not_like, default_table).map do |rel|
|
45
48
|
if rel.left.name.to_s.ends_with?('__not__')
|
46
49
|
rel.left.name = rel.left.name.to_s.sub(/__not__$/, '').to_sym
|
47
50
|
negate rel
|
48
51
|
elsif rel.left.name.to_s.ends_with?('__like__')
|
49
52
|
rel.left.name = rel.left.name.to_s.sub(/__like__$/, '').to_sym
|
50
53
|
Arel::Nodes::Matches.new rel.left, rel.right
|
54
|
+
elsif rel.left.name.to_s.ends_with?('__not_like__')
|
55
|
+
rel.left.name = rel.left.name.to_s.sub(/__not_like__$/, '').to_sym
|
56
|
+
Arel::Nodes::DoesNotMatch.new rel.left, rel.right
|
51
57
|
else
|
52
58
|
rel
|
53
59
|
end
|
54
60
|
end
|
55
61
|
end
|
56
|
-
alias_method_chain :build_from_hash, :
|
62
|
+
alias_method_chain :build_from_hash, :not_and_like_and_not_like
|
57
63
|
end
|
58
64
|
end
|
59
65
|
end
|
data/lib/everywhere/method.rb
CHANGED
@@ -3,7 +3,7 @@ require 'everywhere/util'
|
|
3
3
|
module ActiveRecord
|
4
4
|
class Base
|
5
5
|
class << self
|
6
|
-
delegate :where_not, :where_like, :to => :scoped
|
6
|
+
delegate :where_not, :where_like, :where_not_like, :to => :scoped
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -25,5 +25,13 @@ module ActiveRecord
|
|
25
25
|
relation.where_values += build_where(opts, rest).map {|r| Arel::Nodes::Matches.new r.left, r.right}
|
26
26
|
relation
|
27
27
|
end
|
28
|
+
|
29
|
+
def where_not_like(opts, *rest)
|
30
|
+
return self if opts.blank?
|
31
|
+
|
32
|
+
relation = clone
|
33
|
+
relation.where_values += build_where(opts, rest).map {|r| Arel::Nodes::DoesNotMatch.new r.left, r.right}
|
34
|
+
relation
|
35
|
+
end
|
28
36
|
end
|
29
37
|
end
|
data/lib/everywhere/symbol.rb
CHANGED
@@ -4,16 +4,18 @@ module ActiveRecord
|
|
4
4
|
module QueryMethods
|
5
5
|
include Everywhere::Util
|
6
6
|
|
7
|
-
def
|
7
|
+
def build_where_with_not_and_like_and_not_like(opts, other = [])
|
8
8
|
case opts
|
9
9
|
when :not
|
10
|
-
|
10
|
+
build_where_without_not_and_like_and_not_like(*other).map {|r| negate r}
|
11
11
|
when :like
|
12
|
-
|
12
|
+
build_where_without_not_and_like_and_not_like(*other).map {|r| Arel::Nodes::Matches.new r.left, r.right}
|
13
|
+
when :not_like
|
14
|
+
build_where_without_not_and_like_and_not_like(*other).map {|r| Arel::Nodes::DoesNotMatch.new r.left, r.right}
|
13
15
|
else
|
14
|
-
|
16
|
+
build_where_without_not_and_like_and_not_like(opts, other)
|
15
17
|
end
|
16
18
|
end
|
17
|
-
alias_method_chain :build_where, :
|
19
|
+
alias_method_chain :build_where, :not_and_like_and_not_like
|
18
20
|
end
|
19
21
|
end
|
data/lib/everywhere/version.rb
CHANGED
data/spec/hash_key_spec.rb
CHANGED
@@ -64,3 +64,15 @@ describe 'like' do
|
|
64
64
|
its(:to_sql) { should == %q["posts"."title" LIKE 'he%'] }
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe 'not like' do
|
69
|
+
describe 'not like match' do
|
70
|
+
before do
|
71
|
+
@where = Post.where(:not_like => {:title => 'he%'}).where_values
|
72
|
+
end
|
73
|
+
subject { @where }
|
74
|
+
it { @where.should have(1).item }
|
75
|
+
subject { @where.first }
|
76
|
+
its(:to_sql) { should == %q["posts"."title" NOT LIKE 'he%'] }
|
77
|
+
end
|
78
|
+
end
|
data/spec/hash_value_spec.rb
CHANGED
@@ -64,3 +64,15 @@ describe 'like' do
|
|
64
64
|
its(:to_sql) { should == %q["posts"."title" LIKE 'he%'] }
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe 'not like' do
|
69
|
+
describe 'not like match' do
|
70
|
+
before do
|
71
|
+
@where = Post.where(:title => {:not_like => 'he%'}).where_values
|
72
|
+
end
|
73
|
+
subject { @where }
|
74
|
+
it { @where.should have(1).item }
|
75
|
+
subject { @where.first }
|
76
|
+
its(:to_sql) { should == %q["posts"."title" NOT LIKE 'he%'] }
|
77
|
+
end
|
78
|
+
end
|
data/spec/method_spec.rb
CHANGED
@@ -64,3 +64,15 @@ describe 'like' do
|
|
64
64
|
its(:to_sql) { should == %q["posts"."title" LIKE 'he%'] }
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe 'not like' do
|
69
|
+
describe 'not like match' do
|
70
|
+
before do
|
71
|
+
@where = Post.where_not_like(:title => 'he%').where_values
|
72
|
+
end
|
73
|
+
subject { @where }
|
74
|
+
it { @where.should have(1).item }
|
75
|
+
subject { @where.first }
|
76
|
+
its(:to_sql) { should == %q["posts"."title" NOT LIKE 'he%'] }
|
77
|
+
end
|
78
|
+
end
|
data/spec/symbol_spec.rb
CHANGED
@@ -64,3 +64,15 @@ describe 'like' do
|
|
64
64
|
its(:to_sql) { should == %q["posts"."title" LIKE 'he%'] }
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe 'not like' do
|
69
|
+
describe 'not like match' do
|
70
|
+
before do
|
71
|
+
@where = Post.where(:not_like, :title => 'he%').where_values
|
72
|
+
end
|
73
|
+
subject { @where }
|
74
|
+
it { @where.should have(1).item }
|
75
|
+
subject { @where.first }
|
76
|
+
its(:to_sql) { should == %q["posts"."title" NOT LIKE 'he%'] }
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: everywhere
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-25 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70208330286520 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70208330286520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3
|
27
|
-
requirement: &
|
27
|
+
requirement: &70208330286100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70208330286100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activerecord
|
38
|
-
requirement: &
|
38
|
+
requirement: &70208330285680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70208330285680
|
47
47
|
description: Hash condition syntax for AR query everywhere!
|
48
48
|
email:
|
49
49
|
- ronnie@dio.jp
|
@@ -71,7 +71,7 @@ files:
|
|
71
71
|
- spec/method_spec.rb
|
72
72
|
- spec/spec_helper.rb
|
73
73
|
- spec/symbol_spec.rb
|
74
|
-
homepage:
|
74
|
+
homepage: https://github.com/amatsuda/everywhere
|
75
75
|
licenses: []
|
76
76
|
post_install_message:
|
77
77
|
rdoc_options: []
|