module Hanami module Boot module SourceDirs def self.setup_component_dir!(component_dir, slice, container) # TODO: this `== "lib"` check should be codified into a method somewhere if component_dir.path == "lib" # Expect component files in the root of the lib # component dir to define classes inside the slice's namespace. # # e.g. "lib/foo.rb" should define SliceNamespace::Foo, and will be # registered as "foo" component_dir.namespaces.root(key: nil, const: slice.namespace_path) slice.application.autoloader.push_dir(slice.root.join("lib"), namespace: slice.namespace) container.config.component_dirs.add(component_dir) else # Expect component files in the root of these component dirs to define # classes inside a namespace matching the dir. # # e.g. "actions/foo.rb" should define SliceNamespace::Actions::Foo, and # will be registered as "actions.foo" dir_namespace_path = File.join(slice.namespace_path, component_dir.path) autoloader_namespace = begin slice.inflector.constantize(slice.inflector.camelize(dir_namespace_path)) rescue NameError slice.namespace.const_set(slice.inflector.camelize(component_dir.path), Module.new) end # TODO: do we need to do something special to clear out any previously configured root namespace here? component_dir.namespaces.root(const: dir_namespace_path, key: component_dir.path) # TODO: do we need to swap path delimiters for key delimiters here? container.config.component_dirs.add(component_dir) slice.application.autoloader.push_dir( slice.root.join(component_dir.path), namespace: autoloader_namespace ) end end end end end