Module: Wallaby::Classifier
- Extended by:
- Classifier
- Included in:
- ClassArray, Classifier, Configuration, Wallaby::Configuration::Mapping, Map, Map::ModeMapper
- Defined in:
- lib/wallaby/classifier.rb
Overview
Concern to handle the conversion between Class and String
Instance Method Summary collapse
-
#class_name_of(klass) ⇒ String?
Convert Class to String.
-
#to_class(name, raising: Wallaby.configuration.raise_on_name_error) ⇒ Class, ...
Convert String to Class.
Instance Method Details
#class_name_of(klass) ⇒ String?
Convert Class to String. If not Class, unchanged.
12 13 14 |
# File 'lib/wallaby/classifier.rb', line 12 def class_name_of(klass) klass.is_a?(Class) ? klass.try(:name) : klass.try(:to_s) end |
#to_class(name, raising: Wallaby.configuration.raise_on_name_error) ⇒ Class, ...
Convert String to Class. If not String, unchanged.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wallaby/classifier.rb', line 21 def to_class(name, raising: Wallaby.configuration.raise_on_name_error) return name unless name.is_a?(String) # blank string will lead to NameError `wrong constant name` return if name.blank? # [Performance] Caching the result for uninitialized constant return (yield(name) if block_given?) if raising && Map.class_name_error_map[name] == :uninitialized # NOTE: DO NOT try to use `const_defined?` and `const_get` EVER. # Rails does all the class loading magics using `constantize` name.constantize rescue NameError => e if raising Map.class_name_error_map[name] ||= e..start_with?('uninitialized constant') ? :uninitialized : :name_error raise e if Map.class_name_error_map[name] == :name_error end # block to handle this missing constant, e.g. use a default class or log useful instruction yield(name) if block_given? end |