Class: Formtastic::NamespacedClassFinder
- Inherits:
-
Object
- Object
- Formtastic::NamespacedClassFinder
- Defined in:
- lib/formtastic/namespaced_class_finder.rb
Overview
This class implements class resolution in a namespace chain. It is used both by Formtastic::Helpers::InputHelper and Formtastic::Helpers::ActionHelper to look up action and input classes.
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#class_name(as) ⇒ Object
Converts symbol to class name Overridden in subclasses to create
StringInput
andButtonAction
. -
#find(as) ⇒ Object
Looks up the given reference in the configured namespaces.
-
#initialize(namespaces) ⇒ NamespacedClassFinder
constructor
A new instance of NamespacedClassFinder.
- #resolve(as) ⇒ Object
Constructor Details
#initialize(namespaces) ⇒ NamespacedClassFinder
Returns a new instance of NamespacedClassFinder.
36 37 38 39 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 36 def initialize(namespaces) @namespaces = namespaces.flatten @cache = {} end |
Class Method Details
.use_const_defined? ⇒ Boolean
31 32 33 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 31 def self.use_const_defined? defined?(Rails) && ::Rails.application && ::Rails.application.config.eager_load end |
Instance Method Details
#class_name(as) ⇒ Object
Converts symbol to class name
Overridden in subclasses to create StringInput
and ButtonAction
63 64 65 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 63 def class_name(as) as.to_s.camelize end |
#find(as) ⇒ Object
Looks up the given reference in the configured namespaces.
Two finder methods are provided, one for development tries to reference the constant directly, triggering Rails' autoloading const_missing machinery; the second one instead for production checks with .const_defined before referencing the constant.
48 49 50 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 48 def find(as) @cache[as] ||= resolve(as) end |
#resolve(as) ⇒ Object
52 53 54 55 56 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 52 def resolve(as) class_name = class_name(as) finder(class_name) or raise NotFoundError, "class #{class_name}" end |