module Hanami::Controller
A set of logically grouped actions
@since 0.1.0
@see Hanami::Action
@example
require 'hanami/controller' module Articles class Index include Hanami::Action # ... end class Show include Hanami::Action # ... end end
Constants
- VERSION
Defines the version
@since 0.1.0
Public Class Methods
Configure the framework. It yields the given block in the context of the configuration
@param blk [Proc] the configuration block
@since 0.2.0
@see Hanami::Controller::Configuration
@example
require 'hanami/controller' Hanami::Controller.configure do handle_exceptions false end
# File lib/hanami/controller.rb, line 90 def self.configure(&blk) configuration.instance_eval(&blk) end
Duplicate Hanami::Controller
in order to create a new separated instance of the framework.
The new instance of the framework will be completely decoupled from the original. It will inherit the configuration, but all the changes that happen after the duplication, won't be reflected on the other copies.
@return [Module] a copy of Hanami::Controller
@since 0.2.0 @api private
@example Basic usage
require 'hanami/controller' module MyApp Controller = Hanami::Controller.dupe end MyApp::Controller == Hanami::Controller # => false MyApp::Controller.configuration == Hanami::Controller.configuration # => false
@example Inheriting configuration
require 'hanami/controller' Hanami::Controller.configure do handle_exceptions false end module MyApp Controller = Hanami::Controller.dupe end module MyApi Controller = Hanami::Controller.dupe Controller.configure do handle_exceptions true end end Hanami::Controller.configuration.handle_exceptions # => false MyApp::Controller.configuration.handle_exceptions # => false MyApi::Controller.configuration.handle_exceptions # => true
# File lib/hanami/controller.rb, line 139 def self.dupe dup.tap do |duplicated| duplicated.configuration = configuration.duplicate end end
Duplicate the framework and generate modules for the target application
@param mod [Module] the Ruby namespace of the application @param controllers [String] the optional namespace where the application's
controllers will live
@param blk [Proc] an optional block to configure the framework
@return [Module] a copy of Hanami::Controller
@since 0.2.0
@see Hanami::Controller#dupe @see Hanami::Controller::Configuration
@see Hanami::Controller::Configuration#action_module
@example Basic usage
require 'hanami/controller' module MyApp Controller = Hanami::Controller.duplicate(self) end # It will: # # 1. Generate MyApp::Controller # 2. Generate MyApp::Action # 3. Generate MyApp::Controllers # 4. Configure MyApp::Action as the default module for actions module MyApp::Controllers::Dashboard include MyApp::Controller action 'Index' do # this will inject MyApp::Action def call(params) # ... end end end
@example Compare code
require 'hanami/controller' module MyApp Controller = Hanami::Controller.duplicate(self) do # ... end end # it's equivalent to: module MyApp Controller = Hanami::Controller.dupe Action = Hanami::Action.dup module Controllers end Controller.configure do action_module MyApp::Action end Controller.configure do # ... end end
@example Custom controllers module
require 'hanami/controller' module MyApp Controller = Hanami::Controller.duplicate(self, 'Ctrls') end defined?(MyApp::Controllers) # => nil defined?(MyApp::Ctrls) # => "constant" # Developers can namespace controllers under Ctrls module MyApp::Ctrls::Dashboard # ... end
@example Nil controllers module
require 'hanami/controller' module MyApp Controller = Hanami::Controller.duplicate(self, nil) end defined?(MyApp::Controllers) # => nil # Developers can namespace controllers under MyApp module MyApp::DashboardController # ... end
@example Block usage
require 'hanami/controller' module MyApp Controller = Hanami::Controller.duplicate(self) do handle_exceptions false end end Hanami::Controller.configuration.handle_exceptions # => true MyApp::Controller.configuration.handle_exceptions # => false
# File lib/hanami/controller.rb, line 251 def self.duplicate(mod, controllers = 'Controllers', &blk) dupe.tap do |duplicated| mod.module_eval %{ module #{ controllers }; end } if controllers mod.module_eval %{ Action = Hanami::Action.dup } duplicated.module_eval %{ configure do action_module #{ mod }::Action end } duplicated.configure(&blk) if block_given? end end
Framework loading entry point
@return [void]
@since 0.3.0
# File lib/hanami/controller.rb, line 271 def self.load! configuration.load! end