Skip to content

Commit 4ee52aa

Browse files
author
Leo Arnold
committed
Camelize strategy name in shared examples
Following up on #681, we also need to camelize strategy names in the RSpec shared examples for cleaning strategies. For developer convenience, the methods for string manipulation were moved from the Cleaner class to a dedicated StringHelper module.
1 parent 09b47d0 commit 4ee52aa

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

lib/database_cleaner/cleaner.rb

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
require 'database_cleaner/null_strategy'
22
require 'database_cleaner/strategy'
3+
require 'database_cleaner/string_helper'
34
require 'forwardable'
45

56
module DatabaseCleaner
67
class UnknownStrategySpecified < ArgumentError; end
78

89
class Cleaner
910
def self.available_strategies(orm_module)
10-
# introspect publically available constants for descendents of Strategy to get list of strategies
11-
# ignore classes named Base, because its a common name for a shared base class that adds ORM access stuff to Strategy before being inherited by final concrete class
11+
strategies = []
12+
13+
# introspect publicly available constants for descendents of Strategy to get list of strategies
14+
# ignore classes named Base, because it's a common name for a shared base class that adds ORM access stuff to Strategy before being inherited by final concrete class
1215
# if you're writing an adapter and this method is falsely returning an internal constant that isn't a valid strategy, consider making it private with Module#private_constant.
13-
orm_module.constants.select do |constant_name|
16+
orm_module.constants.each do |constant_name|
17+
next if constant_name == :Base
18+
1419
ancestors = orm_module.const_get(constant_name).ancestors rescue []
15-
ancestors.include?(DatabaseCleaner::Strategy)
16-
end.map do |constant_name|
17-
underscore(constant_name).to_sym
18-
end - [:base]
20+
next unless ancestors.include?(DatabaseCleaner::Strategy)
21+
22+
strategies << StringHelper.underscore(constant_name).to_sym
23+
end
24+
25+
strategies
1926
end
2027

2128
include Comparable
@@ -86,37 +93,16 @@ def create_strategy(*args)
8693
end
8794

8895
def orm_strategy(strategy)
89-
strategy_module_name = camelize(strategy)
96+
strategy_module_name = StringHelper.camelize(strategy)
9097
orm_module.const_get(strategy_module_name)
9198
rescue NameError
9299
available_strategies = self.class.available_strategies(orm_module)
93100
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{available_strategies.join(', ')}"
94101
end
95102

96103
def orm_module
97-
orm_module_name = camelize(orm)
104+
orm_module_name = StringHelper.camelize(orm)
98105
DatabaseCleaner.const_get(orm_module_name)
99106
end
100-
101-
# copied from ActiveSupport to avoid adding it as a dependency
102-
103-
def camelize(term)
104-
string = term.to_s
105-
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
106-
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
107-
string.gsub!("/", "::")
108-
string
109-
end
110-
111-
def self.underscore(camel_cased_word)
112-
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
113-
word = camel_cased_word.to_s.gsub("::", "/")
114-
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
115-
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
116-
word.tr!("-", "_")
117-
word.downcase!
118-
word
119-
end
120-
private_class_method :underscore
121107
end
122108
end

lib/database_cleaner/spec/shared_examples.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RSpec.shared_examples_for "a database_cleaner adapter" do
1212
describe 'all strategies should adhere to a database_cleaner strategy interface' do
1313
DatabaseCleaner::Cleaner.available_strategies(described_class).each do |strategy|
14-
subject { described_class.const_get(strategy.to_s.capitalize).new }
14+
subject { described_class.const_get(DatabaseCleaner::StringHelper.camelize(strategy.to_s)).new }
1515

1616
it_behaves_like 'a database_cleaner strategy'
1717
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module DatabaseCleaner
2+
# Methods copied from ActiveSupport to avoid adding it as a dependency
3+
module StringHelper
4+
def self.camelize(term)
5+
string = term.to_s
6+
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
7+
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
8+
string.gsub!("/", "::")
9+
string
10+
end
11+
12+
def self.underscore(camel_cased_word)
13+
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
14+
word = camel_cased_word.to_s.gsub("::", "/")
15+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
16+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
17+
word.tr!("-", "_")
18+
word.downcase!
19+
word
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)