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

- old
+ new

@@ -17,79 +17,62 @@ module Hanami # Hanami application configuration # # @since 2.0.0 - # - # rubocop:disable Metrics/ClassLength class Configuration include Dry::Configurable DEFAULT_ENVIRONMENTS = Concurrent::Hash.new { |h, k| h[k] = Concurrent::Array.new } private_constant :DEFAULT_ENVIRONMENTS + attr_reader :application_name attr_reader :env attr_reader :actions attr_reader :middleware attr_reader :router attr_reader :views, :assets attr_reader :environments private :environments + # rubocop:disable Metrics/AbcSize def initialize(application_name:, env:) - @namespace = application_name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER) + @application_name = application_name @environments = DEFAULT_ENVIRONMENTS.clone @env = env # Some default setting values must be assigned at initialize-time to ensure they # have appropriate values for the current application self.root = Dir.pwd self.settings_store = Application::Settings::DotenvStore.new.with_dotenv_loaded - config.logger = Configuration::Logger.new(env: env, application_name: method(:application_name)) + config.logger = Configuration::Logger.new(env: env, application_name: application_name) - @assets = begin - require_path = "hanami/assets/application_configuration" - require require_path + @assets = load_dependent_config("hanami/assets/application_configuration") { Hanami::Assets::ApplicationConfiguration.new - rescue LoadError => e - raise e unless e.path == require_path - require_relative "configuration/null_configuration" - NullConfiguration.new - end + } - # Config for actions (same for views, below) may not be available if the gem isn't - # loaded; fall back to a null config object if it's missing - @actions = begin - require_path = "hanami/action/application_configuration" - require require_path - Hanami::Action::ApplicationConfiguration.new(assets_server_url: assets.server_url) - rescue LoadError => e - raise e unless e.path == require_path - require_relative "configuration/null_configuration" - NullConfiguration.new - end + @actions = load_dependent_config("hanami/action") { + require_relative "configuration/actions" + Actions.new + } @middleware = Middleware.new @router = Router.new(self) - @views = begin - require_path = "hanami/view/application_configuration" - require require_path - Hanami::View::ApplicationConfiguration.new - rescue LoadError => e - raise e unless e.path == require_path - require_relative "configuration/null_configuration" - NullConfiguration.new - end + @views = load_dependent_config("hanami/view") { + require_relative "configuration/views" + Views.new + } yield self if block_given? end + # rubocop:enable Metrics/AbcSize def environment(env_name, &block) environments[env_name] << block apply_env_config @@ -107,18 +90,10 @@ router.finalize! super end - def namespace - inflector.constantize(@namespace) - end - - def application_name - inflector.underscore(@namespace).to_sym - end - setting :root, constructor: -> path { Pathname(path) } setting :inflector, default: Dry::Inflector.new def inflections(&block) @@ -158,9 +133,19 @@ def apply_env_config(env = self.env) environments[env].each do |block| instance_eval(&block) end + end + + def load_dependent_config(require_path, &block) + require require_path + yield + rescue LoadError => e + raise e unless e.path == require_path + + require_relative "configuration/null_configuration" + NullConfiguration.new end def method_missing(name, *args, &block) if config.respond_to?(name) config.public_send(name, *args, &block)