# frozen_string_literal: true require "hanami/view" module Hanami module Extensions module View # Provides slice-specific configuration and behavior for any view context class # defined within a slice's module namespace. # # @api private # @since 2.0.0 class SliceConfiguredContext < Module attr_reader :slice def initialize(slice) super() @slice = slice end def extended(_context_class) define_new end def inspect "#<#{self.class.name}[#{slice.name}]>" end private # Defines a {.new} method on the context class that resolves key components from # the app container and provides them to {#initialize} as injected # dependencies. # # This includes the following app components: # # - the configured inflector as `inflector` # - "settings" from the app container as `settings` # - "routes" from the app container as `routes` # - "assets" from the app container as `assets` def define_new inflector = slice.inflector resolve_settings = method(:resolve_settings) resolve_routes = method(:resolve_routes) resolve_assets = method(:resolve_assets) define_method :new do |**kwargs| kwargs[:inflector] ||= inflector kwargs[:settings] ||= resolve_settings.() kwargs[:routes] ||= resolve_routes.() kwargs[:assets] ||= resolve_assets.() super(**kwargs) end end def resolve_settings slice.app[:settings] if slice.app.key?(:settings) end def resolve_routes slice.app["routes"] if slice.app.key?("routes") end def resolve_assets slice.app[:assets] if slice.app.key?(:assets) end end end end end