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.
41 42 43 44 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 41 def initialize(namespaces) @namespaces = namespaces.flatten @cache = {} end |
Class Method Details
.finder_method ⇒ Object
36 37 38 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 36 def self.finder_method @finder_method ||= use_const_defined? ? :find_with_const_defined : :find_by_trying end |
.use_const_defined? ⇒ Boolean
32 33 34 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 32 def self.use_const_defined? defined?(Rails) && ::Rails.application && ::Rails.application.config.respond_to?(:eager_load) && ::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
68 69 70 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 68 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.
53 54 55 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 53 def find(as) @cache[as] ||= resolve(as) end |
#resolve(as) ⇒ Object
57 58 59 60 61 |
# File 'lib/formtastic/namespaced_class_finder.rb', line 57 def resolve(as) class_name = class_name(as) finder(class_name) or raise NotFoundError, "class #{class_name}" end |