Class: ClassFinder
- Inherits:
-
Object
- Object
- ClassFinder
- Defined in:
- lib/dayman/class_finder.rb
Overview
Finds class according to the resource’s context:
module Foo
class TestResource
end
module Bar
class TestResource
end
end
end
Using Dayman to fetch a Foo::Bar::TestResource containing a { type: ‘test_resources’ } object ClassFinder will look for the first TestResource class starting from Foo::Bar::TestResource’s nesting. If Foo::Bar::TestResource did not exist, it would find Foo::TestResource instead.
Constant Summary collapse
- MODULE_SEPARATOR =
'::'
Instance Method Summary collapse
- #find ⇒ Object
-
#initialize(resource:, type:) ⇒ ClassFinder
constructor
A new instance of ClassFinder.
Constructor Details
#initialize(resource:, type:) ⇒ ClassFinder
Returns a new instance of ClassFinder.
24 25 26 27 28 |
# File 'lib/dayman/class_finder.rb', line 24 def initialize(resource:, type:) @resource = resource @type = type @klass = nil end |
Instance Method Details
#find ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dayman/class_finder.rb', line 30 def find namespaces = @resource.name.split(MODULE_SEPARATOR) while @klass.blank? && namespaces.present? namespaces.pop @klass = (namespaces + [@type.classify]).join(MODULE_SEPARATOR).safe_constantize end @klass end |