activerecord-refinements 0.1.0 → 0.1.1
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.
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Refinements
|
3
|
+
module WhereBlockSyntax
|
4
|
+
refine Symbol do
|
5
|
+
%i[== != =~ > >= < <=].each do |op|
|
6
|
+
define_method(op) {|val| [self, op, val] }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class WhereBlockEvaluator
|
12
|
+
using ActiveRecord::Refinements::WhereBlockSyntax
|
13
|
+
|
14
|
+
def initialize(table)
|
15
|
+
@table = table
|
16
|
+
end
|
17
|
+
|
18
|
+
def evaluate(&block)
|
19
|
+
col, op, val = instance_eval &block
|
20
|
+
case op
|
21
|
+
when :==
|
22
|
+
@table[col].eq val
|
23
|
+
when :!=
|
24
|
+
@table[col].not_eq val
|
25
|
+
when :=~
|
26
|
+
@table[col].matches val
|
27
|
+
when :>
|
28
|
+
@table[col].gt val
|
29
|
+
when :>=
|
30
|
+
@table[col].gteq val
|
31
|
+
when :<
|
32
|
+
@table[col].lt val
|
33
|
+
when :<=
|
34
|
+
@table[col].lteq val
|
35
|
+
else
|
36
|
+
raise "unexpected op: #{op}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module QueryMethods
|
42
|
+
def where(opts = nil, *rest, &block)
|
43
|
+
if block
|
44
|
+
evaluator = ActiveRecord::Refinements::WhereBlockEvaluator.new(table)
|
45
|
+
clone.tap do |relation|
|
46
|
+
relation.where_values += build_where(evaluator.evaluate(&block))
|
47
|
+
end
|
48
|
+
else
|
49
|
+
super
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,60 +1,8 @@
|
|
1
1
|
require 'activerecord-refinements/version'
|
2
2
|
require 'active_record'
|
3
|
+
require 'active_record/refinements'
|
3
4
|
|
4
5
|
module ActiveRecord
|
5
|
-
module Refinements
|
6
|
-
module WhereBlockSyntax
|
7
|
-
refine Symbol do
|
8
|
-
%i[== != =~ > >= < <=].each do |op|
|
9
|
-
define_method(op) {|val| [self, op, val] }
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
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
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
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
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
6
|
module QueryMethods
|
59
7
|
prepend ActiveRecord::Refinements::QueryMethods
|
60
8
|
end
|
data/spec/where_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-refinements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- README.md
|
73
73
|
- Rakefile
|
74
74
|
- activerecord-refinements.gemspec
|
75
|
+
- lib/active_record/refinements.rb
|
75
76
|
- lib/activerecord-refinements.rb
|
76
77
|
- lib/activerecord-refinements/version.rb
|
77
78
|
- spec/spec_helper.rb
|