Module: Hanami::Controller

Includes:
Utils::ClassAttribute
Defined in:
gems/gems/hanami-controller-1.3.0/lib/hanami/controller.rb,
gems/gems/hanami-controller-1.3.0/lib/hanami/action/exposable/guard.rb,
gems/gems/hanami-controller-1.3.0/lib/hanami/controller/error.rb,
gems/gems/hanami-controller-1.3.0/lib/hanami/controller/version.rb,
gems/gems/hanami-controller-1.3.0/lib/hanami/controller/configuration.rb

Overview

A set of logically grouped actions

Examples:

require 'hanami/controller'

module Articles
  class Index
    include Hanami::Action

    # ...
  end

  class Show
    include Hanami::Action

    # ...
  end
end

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Classes: Configuration, Error, IllegalExposureError, MissingSessionError, UnknownFormatError

Constant Summary

VERSION =

Defines the version

Since:

  • 0.1.0

'1.3.0'.freeze

Class Method Summary collapse

Class Method Details

.configure(&blk) ⇒ Object

Configure the framework. It yields the given block in the context of the configuration

Examples:

require 'hanami/controller'

Hanami::Controller.configure do
  handle_exceptions false
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.2.0

def self.configure(&blk)
  configuration.instance_eval(&blk)
end

.duplicate(mod, controllers = 'Controllers', &blk) ⇒ Module

Duplicate the framework and generate modules for the target application

module MyApp::Controllers::Dashboard include MyApp::Controller

action 'Index' do # this will inject MyApp::Action def call(params) # … end end end

Examples:

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

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

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

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

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

Parameters:

  • mod (Module)

    the Ruby namespace of the application

  • controllers (String) (defaults to: 'Controllers')

    the optional namespace where the application's controllers will live

  • blk (Proc)

    an optional block to configure the framework

Returns:

  • (Module)

    a copy of Hanami::Controller

See Also:

Since:

  • 0.2.0

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

.load!void

This method returns an undefined value.

Framework loading entry point

Since:

  • 0.3.0

def self.load!
  configuration.load!
end