|
1 | 1 | require 'database_cleaner/null_strategy' |
2 | 2 | require 'database_cleaner/strategy' |
| 3 | +require 'database_cleaner/string_helper' |
3 | 4 | require 'forwardable' |
4 | 5 |
|
5 | 6 | module DatabaseCleaner |
6 | 7 | class UnknownStrategySpecified < ArgumentError; end |
7 | 8 |
|
8 | 9 | class Cleaner |
9 | 10 | 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 |
12 | 13 | # 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 | + |
14 | 17 | 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 |
19 | 22 | end |
20 | 23 |
|
21 | 24 | include Comparable |
@@ -86,37 +89,16 @@ def create_strategy(*args) |
86 | 89 | end |
87 | 90 |
|
88 | 91 | def orm_strategy(strategy) |
89 | | - strategy_module_name = camelize(strategy) |
| 92 | + strategy_module_name = StringHelper.camelize(strategy) |
90 | 93 | orm_module.const_get(strategy_module_name) |
91 | 94 | rescue NameError |
92 | 95 | available_strategies = self.class.available_strategies(orm_module) |
93 | 96 | raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{available_strategies.join(', ')}" |
94 | 97 | end |
95 | 98 |
|
96 | 99 | def orm_module |
97 | | - orm_module_name = camelize(orm) |
| 100 | + orm_module_name = StringHelper.camelize(orm) |
98 | 101 | DatabaseCleaner.const_get(orm_module_name) |
99 | 102 | 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 |
121 | 103 | end |
122 | 104 | end |
0 commit comments