Module: Lotus::Controller

Includes:
Utils::ClassAttribute
Defined in:
lib/lotus/controller.rb,
lib/lotus/controller/dsl.rb,
lib/lotus/controller/version.rb,
lib/lotus/controller/configuration.rb

Overview

A set of logically grouped actions

Examples:

require 'lotus/controller'

class ArticlesController
  include Lotus::Controller

  action 'Index' do
    # ...
  end

  action 'Show' do
    # ...
  end
end

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: Dsl Classes: Configuration, UnknownFormatError

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

'0.2.0'

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 'lotus/controller'

Lotus::Controller.configure do
  handle_exceptions false
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.2.0



68
69
70
# File 'lib/lotus/controller.rb', line 68

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

.dupeModule

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Duplicate Lotus::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.

Examples:

Basic usage

require 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.dupe
end

MyApp::Controller == Lotus::Controller # => false

MyApp::Controller.configuration ==
  Lotus::Controller.configuration # => false

Inheriting configuration

require 'lotus/controller'

Lotus::Controller.configure do
  handle_exceptions false
end

module MyApp
  Controller = Lotus::Controller.dupe
end

module MyApi
  Controller = Lotus::Controller.dupe
  Controller.configure do
    handle_exceptions true
  end
end

Lotus::Controller.configuration.handle_exceptions # => false
MyApp::Controller.configuration.handle_exceptions # => false
MyApi::Controller.configuration.handle_exceptions # => true

Returns:

  • (Module)

    a copy of Lotus::Controller

Since:

  • 0.2.0



117
118
119
120
121
# File 'lib/lotus/controller.rb', line 117

def self.dupe
  dup.tap do |duplicated|
    duplicated.configuration = configuration.duplicate
  end
end

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

Duplicate the framework and generate modules for the target application

 @since 0.2.0

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 'lotus/controller'

module MyApp
  Controller = Lotus::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 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.duplicate(self) do
    # ...
  end
end

# it's equivalent to:

module MyApp
  Controller = Lotus::Controller.dupe
  Action     = Lotus::Action.dup

  module Controllers
  end

  Controller.configure do
    action_module MyApp::Action
  end

  Controller.configure do
    # ...
  end
end

Custom controllers module

require 'lotus/controller'

module MyApp
  Controller = Lotus::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 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.duplicate(self, nil)
end

defined?(MyApp::Controllers) # => nil

# Developers can namespace controllers under MyApp
module MyApp::DashboardController
  # ...
end

Block usage

require 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.duplicate(self) do
    handle_exceptions false
  end
end

Lotus::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 Lotus::Controller

See Also:

Since:

  • 0.1.0



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/lotus/controller.rb', line 229

def self.duplicate(mod, controllers = 'Controllers', &blk)
  dupe.tap do |duplicated|
    mod.module_eval %{ module #{ controllers }; end } if controllers
    mod.module_eval %{ Action = Lotus::Action.dup }

    duplicated.module_eval %{
      configure do
        action_module #{ mod }::Action
      end
    }

    duplicated.configure(&blk) if block_given?
  end
end

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby’s hook for modules. It includes basic Lotus::Controller modules to the given Class (or Module). It sets a copy of the framework configuration

Parameters:

  • base (Class, Module)

    the target controller

See Also:

Since:

  • 0.1.0



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/lotus/controller.rb', line 256

def self.included(base)
  conf = self.configuration.duplicate

  base.class_eval do
    include Dsl
    include Utils::ClassAttribute

    class_attribute :configuration
    self.configuration = conf
  end
end