Class: Wallaby::Guesser
- Inherits:
-
Object
- Object
- Wallaby::Guesser
- Defined in:
- lib/wallaby/guesser.rb
Overview
Guess the associated class for give class
Constant Summary collapse
- SUFFIX =
:no_doc:
/(Controller|Decorator|Servicer|Authorizer|Paginator)$/.freeze
Class Method Summary collapse
-
.class_for(class_name, options = {}, &block) ⇒ Class?
Find out the first demodulized class constant for the give class name.
- .possible_class_from(class_name, denamespace: false) ⇒ Object
Class Method Details
.class_for(class_name, options = {}, &block) ⇒ Class?
Find out the first demodulized class constant for the give class name. For example, if given class name is Admin::Order::ItemsController, then it will try to constantize the following demodulized class names and return the first one that is successfully constantized:
-
Admin::Order::Item
-
Order::Item
-
Item
21 22 23 24 25 26 27 |
# File 'lib/wallaby/guesser.rb', line 21 def class_for(class_name, = {}, &block) suffix = [:suffix] || EMPTY_STRING replacement = [:replacement] || SUFFIX denamespace = .key?(:denamespace) ? [:denamespace] : true base_name = class_name.gsub(replacement, EMPTY_STRING).singularize << suffix possible_class_from(base_name, denamespace: denamespace, &block) end |
.possible_class_from(class_name, denamespace: false) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wallaby/guesser.rb', line 29 def possible_class_from(class_name, denamespace: false) target = nil parts = denamespace ? class_name.split(COLONS) : [class_name] parts.each_with_index.find do |_, index| klass = Classifier.to_class(parts[index..].join(COLONS)) next unless klass # additional checking, the given block should return true to continue next if block_given? && !yield(klass) target = klass end target end |