Skip to content

Commit 787fe89

Browse files
committed
Add clear and restore method
1 parent 4f350dc commit 787fe89

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

lib/sakuramochi.rb

-30
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,6 @@ def self.config
1818
@config
1919
end
2020

21-
configure do |config|
22-
predicates = {
23-
:contains => [:contains],
24-
:starts_with => [:starts_with, :start_with],
25-
:ends_with => [:ends_with, :end_with],
26-
:in => [:in],
27-
:eq => [:eq, :equal, :equals],
28-
:gt => [:gt],
29-
:gte => [:gte, :gteq],
30-
:lt => [:lt],
31-
:lte => [:lte, :lteq],
32-
}
33-
negative = proc { |p| "not_#{p}" }
34-
35-
config.add predicates[:contains], :arel_predicate => :matches, :converter => proc { |v| "%#{v}%" }
36-
config.add predicates[:contains].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}%" }
37-
config.add predicates[:starts_with], :arel_predicate => :matches, :converter => proc { |v| "#{v}%" }
38-
config.add predicates[:starts_with].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "#{v}%" }
39-
config.add predicates[:ends_with], :arel_predicate => :matches, :converter => proc { |v| "%#{v}" }
40-
config.add predicates[:ends_with].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}" }
41-
config.add predicates[:in], :arel_predicate => :in
42-
config.add predicates[:in].map(&negative), :arel_predicate => :not_in
43-
config.add predicates[:eq], :arel_predicate => :eq
44-
config.add predicates[:eq].map(&negative), :ne, :arel_predicate => :not_eq
45-
config.add predicates[:gt], :arel_predicate => :gt
46-
config.add predicates[:gte], :arel_predicate => :gteq
47-
config.add predicates[:lt], :arel_predicate => :lt
48-
config.add predicates[:lte], :arel_predicate => :lteq
49-
end
50-
5121
ActiveSupport.on_load(:active_record) do
5222
ActiveRecord::Relation.send(:include, Sakuramochi::Relation)
5323
ActiveRecord::PredicateBuilder.send(:include, Sakuramochi::PredicateBuilder)

lib/sakuramochi/configuration.rb

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Configuration
66

77
def initialize
88
@predicates = {}
9+
restore
910
end
1011

1112
def add(*args)
@@ -26,5 +27,41 @@ def add(*args)
2627
end
2728
end
2829
end
30+
31+
def clear
32+
@predicates.clear
33+
end
34+
35+
def restore
36+
clear
37+
38+
names = {
39+
:contains => [:contains],
40+
:starts_with => [:starts_with, :start_with],
41+
:ends_with => [:ends_with, :end_with],
42+
:in => [:in],
43+
:eq => [:eq, :equal, :equals],
44+
:gt => [:gt],
45+
:gte => [:gte, :gteq],
46+
:lt => [:lt],
47+
:lte => [:lte, :lteq],
48+
}
49+
negative = proc { |p| "not_#{p}" }
50+
51+
add names[:contains], :arel_predicate => :matches, :converter => proc { |v| "%#{v}%" }
52+
add names[:contains].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}%" }
53+
add names[:starts_with], :arel_predicate => :matches, :converter => proc { |v| "#{v}%" }
54+
add names[:starts_with].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "#{v}%" }
55+
add names[:ends_with], :arel_predicate => :matches, :converter => proc { |v| "%#{v}" }
56+
add names[:ends_with].map(&negative), :arel_predicate => :does_not_match, :converter => proc { |v| "%#{v}" }
57+
add names[:in], :arel_predicate => :in
58+
add names[:in].map(&negative), :arel_predicate => :not_in
59+
add names[:eq], :arel_predicate => :eq
60+
add names[:eq].map(&negative), :ne, :arel_predicate => :not_eq
61+
add names[:gt], :arel_predicate => :gt
62+
add names[:gte], :arel_predicate => :gteq
63+
add names[:lt], :arel_predicate => :lt
64+
add names[:lte], :arel_predicate => :lteq
65+
end
2966
end
3067
end

spec/sakuramochi/predicate_builder_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
describe 'matches' do
66
before do
77
Sakuramochi.configure do |config|
8+
config.clear
89
config.add :contains, :arel_predicate => :matches, :converter => proc { |v| "%#{v}%" }
910
end
1011
@statuses = Status.where(key => value)

spec/sakuramochi/predicate_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
context 'config.add :test' do
8787
before do
8888
Sakuramochi.configure do |config|
89+
config.clear
8990
config.add :test
9091
end
9192
end

spec/spec_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
CreateTestTables.up
1313
CreateTestTables.seed
1414
end
15+
config.after do
16+
Sakuramochi.config.restore
17+
end
1518
end

0 commit comments

Comments
 (0)