Skip to content

Commit 685c779

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 f50ccb2 commit 685c779

3 files changed

Lines changed: 35 additions & 31 deletions

File tree

lib/database_cleaner/cleaner.rb

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
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+
# introspect publicly available constants for descendents of Strategy to get list of strategies
12+
# 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
1213
# 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|
14+
orm_module.constants.filter_map do |constant_name|
15+
next if constant_name == :Base
16+
1417
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]
18+
next unless ancestors.include?(DatabaseCleaner::Strategy)
19+
20+
StringHelper.underscore(constant_name).to_sym
21+
end
1922
end
2023

2124
include Comparable
@@ -86,37 +89,16 @@ def create_strategy(*args)
8689
end
8790

8891
def orm_strategy(strategy)
89-
strategy_module_name = camelize(strategy)
92+
strategy_module_name = StringHelper.camelize(strategy)
9093
orm_module.const_get(strategy_module_name)
9194
rescue NameError
9295
available_strategies = self.class.available_strategies(orm_module)
9396
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{available_strategies.join(', ')}"
9497
end
9598

9699
def orm_module
97-
orm_module_name = camelize(orm)
100+
orm_module_name = StringHelper.camelize(orm)
98101
DatabaseCleaner.const_get(orm_module_name)
99102
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
121103
end
122104
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)