lib/hanami/application.rb in hanami-2.0.0.alpha7.1 vs lib/hanami/application.rb in hanami-2.0.0.alpha8

- old
+ new

@@ -5,57 +5,58 @@ require "pathname" require "rack" require "zeitwerk" require_relative "constants" require_relative "slice" +require_relative "slice_name" require_relative "application/slice_registrar" module Hanami # Hanami application class # # @since 2.0.0 class Application @_mutex = Mutex.new class << self - def inherited(klass) + def inherited(subclass) super + @_mutex.synchronize do - klass.class_eval do + subclass.class_eval do @_mutex = Mutex.new - @_configuration = Hanami::Configuration.new(application_name: name, env: Hanami.env) + @application_name = SliceName.new(subclass, inflector: -> { subclass.inflector }) + @configuration = Hanami::Configuration.new(application_name: @application_name, env: Hanami.env) @autoloader = Zeitwerk::Loader.new @container = Class.new(Dry::System::Container) + @prepared = @booted = false + extend ClassMethods end - klass.send :prepare_base_load_path + subclass.send :prepare_base_load_path - Hanami.application = klass + Hanami.application = subclass end end end # Application class interface # # rubocop:disable Metrics/ModuleLength module ClassMethods - attr_reader :autoloader, :container + attr_reader :application_name, :configuration, :autoloader, :container - def self.extended(klass) - klass.class_eval do - @prepared = @booted = false - end - end + alias_method :slice_name, :application_name - def configuration - @_configuration - end - alias_method :config, :configuration + def application + self + end + def prepare(provider_name = nil) container.prepare(provider_name) and return self if provider_name return self if prepared? @@ -145,48 +146,21 @@ def settings @_settings ||= load_settings end def namespace - configuration.namespace + application_name.namespace end - def namespace_name - namespace.name - end - - def namespace_path - inflector.underscore(namespace) - end - - def application_name - configuration.application_name - end - def root configuration.root end def inflector configuration.inflector end - # @api private - def component_provider(component) - raise "Hanami.application must be prepared before detecting providers" unless prepared? - - # e.g. [Admin, Main, MyApp] - providers = slices.to_a + [self] - - component_class = component.is_a?(Class) ? component : component.class - component_name = component_class.name - - return unless component_name - - providers.detect { |provider| component_name.include?(provider.namespace.to_s) } - end - private def prepare_base_load_path base_path = File.join(root, "lib") $LOAD_PATH.unshift base_path unless $LOAD_PATH.include?(base_path) @@ -220,12 +194,12 @@ ] end def prepare_autoload_paths # Autoload classes defined in lib/[app_namespace]/ - if root.join("lib", namespace_path).directory? - autoloader.push_dir(root.join("lib", namespace_path), namespace: namespace) + if root.join("lib", application_name.name).directory? + autoloader.push_dir(root.join("lib", application_name.name), namespace: namespace) end end def prepare_container_consts namespace.const_set :Container, container @@ -237,12 +211,12 @@ slices.freeze end def prepare_autoloader # Autoload classes defined in lib/[app_namespace]/ - if root.join("lib", namespace_path).directory? - autoloader.push_dir(root.join("lib", namespace_path), namespace: namespace) + if root.join("lib", application_name.name).directory? + autoloader.push_dir(root.join("lib", application_name.name), namespace: namespace) end autoloader.setup end @@ -256,10 +230,10 @@ rescue LoadError Settings.new end def autodiscover_application_constant(constants) - inflector.constantize([namespace_name, *constants].join(MODULE_DELIMITER)) + inflector.constantize([application_name.namespace_name, *constants].join(MODULE_DELIMITER)) end def load_router require_relative "application/router"