Module: Hanami::Slice::ClassMethods
- Defined in:
- gems/gems/hanami-2.1.0/lib/hanami/slice.rb
Overview
rubocop:disable Metrics/ModuleLength
Constant Summary
- ROUTER_NOT_ALLOWED_HANDLER =
-> env, allowed_http_methods { raise Hanami::Router::NotAllowedError.new(env, allowed_http_methods) }.freeze
- ROUTER_NOT_FOUND_HANDLER =
-> env { raise Hanami::Router::NotFoundError.new(env) }.freeze
Instance Method Summary collapse
-
#[](key) ⇒ Object
Resolves the component with the given key from the container.
- #assets_dir? ⇒ Boolean private
-
#call(rack_env) ⇒ Array
Calls the slice’s [Rack] app and returns a Rack-compatible response object.
- #ensure_root ⇒ Object private
- #ensure_slice_consts ⇒ Object private
- #ensure_slice_name ⇒ Object private
-
#import(from: , as: nil, keys: nil) ⇒ Object
Specifies components to import from another slice.
-
#key?(key) ⇒ Boolean
Returns true if the component with the given key is registered in the container.
- #load_router(inspector:) ⇒ Object private
- #load_routes ⇒ Object private
- #prepare(provider_name = nil) ⇒ Object
- #prepare_all ⇒ Object private
- #prepare_autoloader ⇒ Object private
- #prepare_container_base_config ⇒ Object private
- #prepare_container_component_dirs ⇒ Object private
- #prepare_container_consts ⇒ Object private
- #prepare_container_imports ⇒ Object private
- #prepare_container_plugins ⇒ Object private
- #prepare_container_providers ⇒ Object private
- #prepare_settings ⇒ Object private
-
#prepare_slice ⇒ Object
private
rubocop:disable Metrics/AbcSize.
- #prepare_slices ⇒ Object private
-
#register_provider(name, namespace: nil, from: nil, source: nil) ⇒ container
Registers a provider and its lifecycle hooks.
- #render_detailed_errors? ⇒ Boolean private
- #render_errors? ⇒ Boolean private
-
#start(provider_name) ⇒ container
Starts a provider.
-
#stop(provider_name) ⇒ container
Stops a provider.
Instance Method Details
#assets_dir? ⇒ Boolean (private)
#ensure_root ⇒ Object (private)
Source: | on GitHub
def ensure_root unless config.root raise SliceLoadError, "Slice must have a `config.root` before it can be prepared" end end |
#ensure_slice_consts ⇒ Object (private)
Source: | on GitHub
def ensure_slice_consts if namespace.const_defined?(:Container) || namespace.const_defined?(:Deps) raise( SliceLoadError, "#{namespace}::Container and #{namespace}::Deps constants must not already be defined" ) end end |
#ensure_slice_name ⇒ Object (private)
Source: | on GitHub
def ensure_slice_name unless name raise SliceLoadError, "Slice must have a class name before it can be prepared" end end |
#import(from: , as: nil, keys: nil) ⇒ Object
#load_router(inspector:) ⇒ Object (private)
Source: | on GitHub
def load_router(inspector:) return unless routes require_relative "slice/router" slice = self config = self.config rack_monitor = self["rack.monitor"] show_welcome = Hanami.env?(:development) && routes.empty? render_errors = render_errors? render_detailed_errors = render_detailed_errors? error_handlers = {}.tap do |hsh| if render_errors || render_detailed_errors hsh[:not_allowed] = ROUTER_NOT_ALLOWED_HANDLER hsh[:not_found] = ROUTER_NOT_FOUND_HANDLER end end Slice::Router.new( inspector: inspector, routes: routes, resolver: config.router.resolver.new(slice: self), **error_handlers, **config.router. ) do use(rack_monitor) use(Hanami::Web::Welcome) if show_welcome use( Hanami::Middleware::RenderErrors, config, Hanami::Middleware::PublicErrorsApp.new(slice.root.join("public")) ) if render_detailed_errors require "hanami/webconsole" use(Hanami::Webconsole::Middleware, config) end if Hanami.bundled?("hanami-controller") if config.actions.method_override require "rack/method_override" use(Rack::MethodOverride) end if config.actions.sessions.enabled? use(*config.actions.sessions.middleware) end end if Hanami.bundled?("hanami-assets") && config.assets.serve use(Hanami::Middleware::Assets) end middleware_stack.update(config.middleware_stack) end end |
#load_routes ⇒ Object (private)
Source: | on GitHub
def load_routes return false unless Hanami.bundled?("hanami-router") if root.directory? routes_require_path = File.join(root, ROUTES_PATH) begin require_relative "./routes" require routes_require_path rescue LoadError => e raise e unless e.path == routes_require_path end end begin routes_class = namespace.const_get(ROUTES_CLASS_NAME) routes_class.routes rescue NameError => e raise e unless e.name == ROUTES_CLASS_NAME.to_sym end end |
#prepare ⇒ self #prepare(provider_name) ⇒ self
Source: | on GitHub
def prepare(provider_name = nil) if provider_name container.prepare(provider_name) else prepare_slice end self end |
#prepare_all ⇒ Object (private)
Source: | on GitHub
def prepare_all prepare_settings prepare_container_consts prepare_container_plugins prepare_container_base_config prepare_container_component_dirs prepare_container_imports prepare_container_providers end |
#prepare_autoloader ⇒ Object (private)
Source: | on GitHub
def prepare_autoloader # Component dirs are automatically pushed to the autoloader by dry-system's # zeitwerk plugin. This method adds other dirs that are not otherwise configured # as component dirs. # Everything in the slice root can be autoloaded except `config/` and `slices/`, # which are framework-managed directories if root.join(CONFIG_DIR)&.directory? autoloader.ignore(root.join(CONFIG_DIR)) end if root.join(SLICES_DIR)&.directory? autoloader.ignore(root.join(SLICES_DIR)) end autoloader.setup end |
#prepare_container_base_config ⇒ Object (private)
Source: | on GitHub
def prepare_container_base_config container.config.name = slice_name.to_sym container.config.root = root container.config.provider_dirs = [File.join("config", "providers")] container.config.registrations_dir = File.join("config", "registrations") container.config.env = config.env container.config.inflector = config.inflector end |
#prepare_container_component_dirs ⇒ Object (private)
Source: | on GitHub
def prepare_container_component_dirs return unless root.directory? # Component files in both the root and `lib/` define classes in the slice's # namespace if root.join(LIB_DIR)&.directory? container.config.component_dirs.add(LIB_DIR) do |dir| dir.namespaces.add_root(key: nil, const: slice_name.name) end end # When auto-registering components in the root, ignore files in `config/` (this is # for framework config only), `lib/` (these will be auto-registered as above), as # well as the configured no_auto_register_paths no_auto_register_paths = ([LIB_DIR, CONFIG_DIR] + config.no_auto_register_paths) .map { |path| path.end_with?(File::SEPARATOR) ? path : "#{path}#{File::SEPARATOR}" } # TODO: Change `""` (signifying the root) once dry-rb/dry-system#238 is resolved container.config.component_dirs.add("") do |dir| dir.namespaces.add_root(key: nil, const: slice_name.name) dir.auto_register = -> component { relative_path = component.file_path.relative_path_from(root).to_s !relative_path.start_with?(*no_auto_register_paths) } end end |
#prepare_container_consts ⇒ Object (private)
Source: | on GitHub
def prepare_container_consts namespace.const_set :Container, container namespace.const_set :Deps, container.injector end |
#prepare_container_imports ⇒ Object (private)
Source: | on GitHub
def prepare_container_imports import( keys: config.shared_app_component_keys, from: app.container, as: nil ) end |
#prepare_container_plugins ⇒ Object (private)
#prepare_container_providers ⇒ Object (private)
Source: | on GitHub
def prepare_container_providers # Check here for the `routes` definition only, not `router` itself, because the # `router` requires the slice to be prepared before it can be loaded, and at this # point we're still in the process of preparing. if routes require_relative "providers/routes" register_provider(:routes, source: Providers::Routes.for_slice(self)) end if assets_dir? && Hanami.bundled?("hanami-assets") require_relative "providers/assets" register_provider(:assets, source: Providers::Assets.for_slice(self)) end end |
#prepare_settings ⇒ Object (private)
Source: | on GitHub
def prepare_settings container.register(:settings, settings) if settings end |
#prepare_slice ⇒ Object (private)
rubocop:disable Metrics/AbcSize
Source: | on GitHub
def prepare_slice return self if prepared? config.finalize! ensure_slice_name ensure_slice_consts ensure_root prepare_all instance_exec(container, &@prepare_container_block) if @prepare_container_block container.configured! prepare_autoloader # Load child slices last, ensuring their parent is fully prepared beforehand # (useful e.g. for slices that may wish to access constants defined in the # parent's autoloaded directories) prepare_slices @prepared = true self end |
#prepare_slices ⇒ Object (private)
Source: | on GitHub
def prepare_slices slices.load_slices.each(&:prepare) slices.freeze end |
#register_provider(name, namespace: nil, from: nil, source: nil) ⇒ container
Source: | on GitHub
def register_provider(...) container.register_provider(...) end |