activerecord-refinements 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,48 +3,59 @@ require 'active_record'
3
3
 
4
4
  module ActiveRecord
5
5
  module Refinements
6
- refine Symbol do
7
- def ==(val)
8
- {self => val}
9
- end
10
-
11
- def !=(val)
12
- ["#{self} <> ?", val]
13
- end
14
-
15
- def =~(val)
16
- ["#{self} like ?", val]
17
- end
18
-
19
- def >(val)
20
- ["#{self} > ?", val]
21
- end
22
-
23
- def >=(val)
24
- ["#{self} >= ?", val]
6
+ module WhereBlockSyntax
7
+ refine Symbol do
8
+ %i[== != =~ > >= < <=].each do |op|
9
+ define_method(op) {|val| [self, op, val] }
10
+ end
25
11
  end
12
+ end
26
13
 
27
- def <(val)
28
- ["#{self} < ?", val]
14
+ class WhereBlockEvaluator
15
+ using ActiveRecord::Refinements::WhereBlockSyntax
16
+
17
+ def initialize(table)
18
+ @table = table
19
+ end
20
+
21
+ def evaluate(&block)
22
+ col, op, val = instance_eval &block
23
+ case op
24
+ when :==
25
+ @table[col].eq val
26
+ when :!=
27
+ @table[col].not_eq val
28
+ when :=~
29
+ @table[col].matches val
30
+ when :>
31
+ @table[col].gt val
32
+ when :>=
33
+ @table[col].gteq val
34
+ when :<
35
+ @table[col].lt val
36
+ when :<=
37
+ @table[col].lteq val
38
+ else
39
+ raise "unexpected op: #{op}"
40
+ end
29
41
  end
42
+ end
30
43
 
31
- def <=(val)
32
- ["#{self} <= ?", val]
44
+ module QueryMethods
45
+ def where(opts = nil, *rest, &block)
46
+ if block
47
+ evaluator = ActiveRecord::Refinements::WhereBlockEvaluator.new(table)
48
+ clone.tap do |relation|
49
+ relation.where_values += build_where(evaluator.evaluate(&block))
50
+ end
51
+ else
52
+ super
53
+ end
33
54
  end
34
55
  end
35
56
  end
36
57
 
37
58
  module QueryMethods
38
- def where(opts = nil, *rest, &block)
39
- return self if opts.blank? && block.nil?
40
-
41
- relation = clone
42
- if block
43
- relation.where_values += build_where(Module.new { using ActiveRecord::Refinements }.module_eval(&block))
44
- else
45
- relation.where_values += build_where(opts, rest)
46
- end
47
- relation
48
- end
59
+ prepend ActiveRecord::Refinements::QueryMethods
49
60
  end
50
61
  end
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module Refinements
3
- VERSION = "0.0.1"
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
data/spec/where_spec.rb CHANGED
@@ -8,31 +8,43 @@ describe 'Symbol enhancements' do
8
8
 
9
9
  describe '#=~' do
10
10
  subject { User.where { :name != 'nobu' }.to_sql }
11
- it { should =~ /WHERE \(name <> 'nobu'\)/ }
11
+ it { should =~ /WHERE \("users"."name" != 'nobu'\)/ }
12
12
  end
13
13
 
14
14
  describe '#>' do
15
15
  subject { User.where { :age > 3 }.to_sql }
16
- it { should =~ /WHERE \(age > 3\)/ }
16
+ it { should =~ /WHERE \("users"."age" > 3\)/ }
17
17
  end
18
18
 
19
19
  describe '#>=' do
20
20
  subject { User.where { :age >= 18 }.to_sql }
21
- it { should =~ /WHERE \(age >= 18\)/ }
21
+ it { should =~ /WHERE \("users"."age" >= 18\)/ }
22
22
  end
23
23
 
24
24
  describe '#<' do
25
25
  subject { User.where { :age < 60 }.to_sql }
26
- it { should =~ /WHERE \(age < 60\)/ }
26
+ it { should =~ /WHERE \("users"."age" < 60\)/ }
27
27
  end
28
28
 
29
29
  describe '#<=' do
30
30
  subject { User.where { :age <= 35 }.to_sql }
31
- it { should =~ /WHERE \(age <= 35\)/ }
31
+ it { should =~ /WHERE \("users"."age" <= 35\)/ }
32
32
  end
33
33
 
34
34
  describe '#=~' do
35
35
  subject { User.where { :name =~ 'tender%' }.to_sql }
36
- it { should =~ /WHERE \(name like 'tender%'\)/ }
36
+ it { should =~ /WHERE \("users"."name" LIKE 'tender%'\)/ }
37
37
  end
38
+
39
+ context 'outside of where block' do
40
+ specify {
41
+ expect { :omg > 1 }.to raise_error ArgumentError
42
+ }
43
+ end
44
+ end
45
+
46
+ # just to make sure that this plugin does not break AR
47
+ describe 'AR default where syntax' do
48
+ subject { User.where(name: 'Ruby', age: 19).to_sql }
49
+ it { should =~ /WHERE "users"."name" = 'Ruby' AND "users"."age" = 19/ }
38
50
  end
metadata CHANGED
@@ -1,74 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activerecord-refinements
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Akira Matsuda
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-09-27 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activerecord
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: sqlite3
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :development
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: rspec
50
39
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
52
41
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
60
54
  type: :development
61
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
62
  description: Adding clean and powerful query syntax on AR using refinements
63
- email:
63
+ email:
64
64
  - ronnie@dio.jp
65
65
  executables: []
66
-
67
66
  extensions: []
68
-
69
67
  extra_rdoc_files: []
70
-
71
- files:
68
+ files:
72
69
  - .gitignore
73
70
  - Gemfile
74
71
  - LICENSE.txt
@@ -79,39 +76,30 @@ files:
79
76
  - lib/activerecord-refinements/version.rb
80
77
  - spec/spec_helper.rb
81
78
  - spec/where_spec.rb
82
- homepage: ""
79
+ homepage: ''
83
80
  licenses: []
84
-
85
81
  post_install_message:
86
82
  rdoc_options: []
87
-
88
- require_paths:
83
+ require_paths:
89
84
  - lib
90
- required_ruby_version: !ruby/object:Gem::Requirement
85
+ required_ruby_version: !ruby/object:Gem::Requirement
91
86
  none: false
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- hash: 3
96
- segments:
97
- - 0
98
- version: "0"
99
- required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
92
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- hash: 3
105
- segments:
106
- - 0
107
- version: "0"
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
108
97
  requirements: []
109
-
110
98
  rubyforge_project:
111
99
  rubygems_version: 1.8.24
112
100
  signing_key:
113
101
  specification_version: 3
114
102
  summary: ActiveRecord + Ruby 2.0 refinements
115
- test_files:
103
+ test_files:
116
104
  - spec/spec_helper.rb
117
105
  - spec/where_spec.rb