Class: Lotus::Controller::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/controller/configuration.rb

Overview

Configuration for the framework, controllers and actions.

Lotus::Controller has its own global configuration that can be manipulated via Lotus::Controller.configure.

Every time that Lotus::Controller and Lotus::Action are included, that global configuration is being copied to the recipient. The copy will inherit all the settings from the original, but all the subsequent changes aren’t reflected from the parent to the children, and viceversa.

This architecture allows to have a global configuration that capture the most common cases for an application, and let controllers and single actions to specify exceptions.

Since:

  • 0.2.0

Constant Summary collapse

DEFAULT_ERROR_CODE =

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

Default HTTP code for server side errors

Since:

  • 0.2.0

500
DEFAULT_FORMATS =

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

Default Mime type to format mapping

Since:

  • 0.2.0

{
  'application/octet-stream' => :all,
  '*/*'                      => :all,
  'text/html'                => :html
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLotus::Controller::Configuration

Initialize a configuration instance

Since:

  • 0.2.0



102
103
104
# File 'lib/lotus/controller/configuration.rb', line 102

def initialize
  reset!
end

Instance Attribute Details

#action_module(value) ⇒ Object #action_moduleModule

Specify which is the default action module to be included when we use the Lotus::Controller.action method.

This setting is useful when we use multiple instances of the framework in the same process, so we want to ensure that the actions will include MyApp::Action, rather than AnotherApp::Action.

If not set, the default value is Lotus::Action

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.action_module # => Lotus::Action

Setting the value

require 'lotus/controller'

module MyAction
end

Lotus::Controller.configure do
  action_module MyAction
end

module Dashboard
  # It includes MyAction, instead of Lotus::Action
  class Index
    include MyAction

    def call(params)
      # ...
    end
  end
end

Duplicated framework

require 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.duplicate(self)

  module Controllers::Dashboard
    include MyApp::Controller

    # It includes MyApp::Action, instead of Lotus::Action
    class Index
      include MyApp::Action

      def call(params)
        # ...
      end
    end
  end
end

Overloads:

  • #action_module(value) ⇒ Object

    Sets the given value

  • #action_moduleModule

    Gets the value

See Also:

  • Dsl#action
  • Lotus::Controller#duplicate

Since:

  • 0.2.0



290
291
292
293
294
295
296
# File 'lib/lotus/controller/configuration.rb', line 290

def action_module(value = nil)
  if value.nil?
    @action_module
  else
    @action_module = value
  end
end

#cookies(options = nil) ⇒ Object

Set default cookies options for all responses

By default this value is an empty hash.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.cookies # => {}

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  cookies({
    domain: 'lotusrb.org',
    path: '/controller',
    secure: true,
    httponly: true
  })
end

Since:

  • 0.4.0



587
588
589
590
591
592
593
594
595
# File 'lib/lotus/controller/configuration.rb', line 587

def cookies(options = nil)
  if options
    @cookies.merge!(
      options.reject { |_, v| v.nil? }
    )
  else
    @cookies
  end
end

#default_charset(charset = nil) ⇒ Object

Set a charset as default fallback for all the requests without a strict requirement for the charset.

By default this value is nil.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.default_charset # => nil

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  default_charset 'koi8-r'
end

See Also:

Since:

  • 0.3.0



528
529
530
531
532
533
534
# File 'lib/lotus/controller/configuration.rb', line 528

def default_charset(charset = nil)
  if charset
    @default_charset = charset
  else
    @default_charset
  end
end

#default_headers(headers = nil) ⇒ Object

Set default headers for all responses

By default this value is an empty hash.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.default_headers # => {}

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  default_headers({
    'X-Frame-Options' => 'DENY'
  })
end

Since:

  • 0.4.0



555
556
557
558
559
560
561
562
563
# File 'lib/lotus/controller/configuration.rb', line 555

def default_headers(headers = nil)
  if headers
    @default_headers.merge!(
      headers.reject {|_,v| v.nil? }
    )
  else
    @default_headers
  end
end

#default_request_format(format) ⇒ Object #default_request_formatSymbol?

Set a format as default fallback for all the requests without a strict requirement for the mime type.

The given format must be coercible to a symbol, and be a valid mime type alias. If it isn’t, at the runtime the framework will raise a Lotus::Controller::UnknownFormatError.

By default this value is nil.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.default_request_format # => nil

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  default_request_format :html
end

Overloads:

  • #default_request_format(format) ⇒ Object

    Sets the given value

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_request_formatSymbol?

    Gets the value

See Also:

Since:

  • 0.5.0



445
446
447
448
449
450
451
# File 'lib/lotus/controller/configuration.rb', line 445

def default_request_format(format = nil)
  if format
    @default_request_format = Utils::Kernel.Symbol(format)
  else
    @default_request_format
  end
end

#default_response_format(format) ⇒ Object #default_response_formatSymbol?

Set a format to be used for all responses regardless of the request type.

The given format must be coercible to a symbol, and be a valid mime type alias. If it isn’t, at the runtime the framework will raise a Lotus::Controller::UnknownFormatError.

By default this value is nil.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.default_response_format # => nil

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  default_response_format :json
end

Overloads:

  • #default_response_format(format) ⇒ Object

    Sets the given value

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_response_formatSymbol?

    Gets the value

See Also:

Since:

  • 0.5.0



500
501
502
503
504
505
506
# File 'lib/lotus/controller/configuration.rb', line 500

def default_response_format(format = nil)
  if format
    @default_response_format = Utils::Kernel.Symbol(format)
  else
    @default_response_format
  end
end

#handle_exceptions(value) ⇒ Object #handle_exceptionsTrueClass, FalseClass

Handle exceptions with an HTTP status or let them uncaught

If this value is set to true, the configured exceptions will return the specified HTTP status, the rest of them with 500.

If this value is set to false, the exceptions won’t be caught.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'lotus/controller'

Lotus::Controller.configuration.handle_exceptions # => true

Setting the value

require 'lotus/controller'

Lotus::Controller.configure do
  handle_exceptions false
end

Overloads:

  • #handle_exceptions(value) ⇒ Object

    Sets the given value

  • #handle_exceptionsTrueClass, FalseClass

    Gets the value

See Also:

Since:

  • 0.2.0



153
154
155
156
157
158
159
# File 'lib/lotus/controller/configuration.rb', line 153

def handle_exceptions(value = nil)
  if value.nil?
    @handle_exceptions
  else
    @handle_exceptions = value
  end
end

#modulesArray<Proc>

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.

Return included modules

See Also:

Since:

  • 0.2.0



652
653
654
# File 'lib/lotus/controller/configuration.rb', line 652

def modules
  @modules
end

Class Method Details

.for(base) ⇒ Lotus::Controller::Configuration

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.

Return a copy of the configuration of the framework instance associated with the given class.

When multiple instances of Lotus::Controller are used in the same application, we want to make sure that a controller or an action will receive the expected configuration.

Examples:

Direct usage of the framework

require 'lotus/controller'

class Show
  include Lotus::Action
end

Lotus::Controller::Configuration.for(Show)
  # => will duplicate from Lotus::Controller

Multiple instances of the framework

require 'lotus/controller'

module MyApp
  Controller = Lotus::Controller.duplicate(self)

  module Controllers::Dashboard
    class Index
      include MyApp::Action

      def call(params)
        # ...
      end
    end
  end
end

class Show
  include Lotus::Action
end

Lotus::Controller::Configuration.for(Show)
  # => will duplicate from Lotus::Controller

Lotus::Controller::Configuration.for(MyApp::Controllers::Dashboard)
  # => will duplicate from MyApp::Controller

Since:

  • 0.2.0



90
91
92
93
94
# File 'lib/lotus/controller/configuration.rb', line 90

def self.for(base)
  namespace = Utils::String.new(base).namespace
  framework = Utils::Class.load_from_pattern!("(#{namespace}|Lotus)::Controller")
  framework.configuration.duplicate
end

Instance Method Details

#copy!(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.

Copy the configuration for the given action

See Also:

  • Lotus::Controller::Configurable.included

Since:

  • 0.3.0



681
682
683
684
685
# File 'lib/lotus/controller/configuration.rb', line 681

def copy!(base)
  modules.each do |mod|
    base.class_eval(&mod)
  end
end

#default_format(format = nil) ⇒ Object

Deprecated.

Set a format as default fallback for all the requests without a strict requirement for the mime type.

Since:

  • 0.2.0



459
460
461
462
# File 'lib/lotus/controller/configuration.rb', line 459

def default_format(format = nil)
  Lotus::Utils::Deprecation.new('default_format is deprecated, please use default_request_format')
  default_request_format(format)
end

#duplicateLotus::Controller::Configuration

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 by copying the settings in a new instance.

Since:

  • 0.2.0



629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/lotus/controller/configuration.rb', line 629

def duplicate
  Configuration.new.tap do |c|
    c.handle_exceptions       = handle_exceptions
    c.handled_exceptions      = handled_exceptions.dup
    c.action_module           = action_module
    c.modules                 = modules.dup
    c.formats                 = formats.dup
    c.default_request_format  = default_request_format
    c.default_response_format = default_response_format
    c.default_charset         = default_charset
    c.default_headers         = default_headers.dup
    c.cookies = cookies.dup
  end
end

#exception_handler(exception) ⇒ 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.

Return a callable handler for the given exception

See Also:

Since:

  • 0.3.0



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/lotus/controller/configuration.rb', line 193

def exception_handler(exception)
  handler = nil

  @handled_exceptions.each do |exception_class, h|
    if exception.kind_of?(exception_class)
      handler = h
      break
    end
  end

  handler || DEFAULT_ERROR_CODE
end

#format(hash) ⇒ Object

Register a format

Examples:

require 'lotus/controller'

Lotus::Controller.configure do
  format custom: 'application/custom'
end

module Articles
  class Index
    include Lotus::Action

    def call(params)
      # ...
    end
  end

  class Show
    include Lotus::Action

    def call(params)
      # ...
      self.format = :custom
    end
  end
end

action = Articles::Index.new

action.call({ 'HTTP_ACCEPT' => 'text/html' })
  # => Content-Type "text/html"
action.format # => :html

action.call({ 'HTTP_ACCEPT' => 'application/custom' })
  # => Content-Type "application/custom"
action.format # => :custom

action = Articles::Show.new

action.call({ 'HTTP_ACCEPT' => 'text/html' })
  # => Content-Type "application/custom"
action.format # => :custom

See Also:

Since:

  • 0.2.0



401
402
403
404
405
406
# File 'lib/lotus/controller/configuration.rb', line 401

def format(hash)
  symbol, mime_type = *Utils::Kernel.Array(hash)

  @formats.merge! Utils::Kernel.String(mime_type) =>
    Utils::Kernel.Symbol(symbol)
end

#format_for(mime_type) ⇒ Symbol?

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.

Returns a format for the given mime type

See Also:

Since:

  • 0.2.0



607
608
609
# File 'lib/lotus/controller/configuration.rb', line 607

def format_for(mime_type)
  @formats[mime_type]
end

#handle_exception(exception) ⇒ Object

Specify how to handle an exception with an HTTP status

Raised exceptions will return the configured HTTP status, only if

`handled_exceptions` is set on `true`.

Examples:

require 'lotus/controller'

Lotus::Controller.configure do
  handle_exception ArgumentError => 400
end

See Also:

Since:

  • 0.2.0



181
182
183
# File 'lib/lotus/controller/configuration.rb', line 181

def handle_exception(exception)
  @handled_exceptions.merge!(exception)
end

#handled_exception?(exception) ⇒ Boolean

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.

Check if the given exception is handled.

See Also:

Since:

  • 0.3.2



214
215
216
217
# File 'lib/lotus/controller/configuration.rb', line 214

def handled_exception?(exception)
  handled_exceptions &&
    !!@handled_exceptions.fetch(exception.class) { false }
end

#load!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.

Load the framework

Since:

  • 0.3.0



691
692
693
# File 'lib/lotus/controller/configuration.rb', line 691

def load!
  freeze
end

#mime_type_for(format) ⇒ String?

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.

Returns a mime type for the given format

Since:

  • 0.2.0



619
620
621
# File 'lib/lotus/controller/configuration.rb', line 619

def mime_type_for(format)
  @formats.key(format)
end

#prepare(&blk) ⇒ void

This method returns an undefined value.

Configure the logic to be executed when Lotus::Action is included This is useful to DRY code by having a single place where to configure shared behaviors like authentication, sessions, cookies etc.

This method can be called multiple times.

Examples:

Configure shared logic.

require 'lotus/controller'

Lotus::Controller.configure do
  prepare do
    include Lotus::Action::Sessions
    include MyAuthentication
    use SomeMiddleWare

    before { authenticate! }
  end
end

module Dashboard
  class Index
    # When Lotus::Action is included, it will:
    #   * Include `Lotus::Action::Session` and `MyAuthentication`
    #   * Configure to use `SomeMiddleWare`
    #   * Configure a `before` callback that triggers `#authenticate!`
    include Lotus::Action

    def call(params)
      # ...
    end
  end
end

Raises:

  • (ArgumentError)

    if called without passing a block

See Also:

Since:

  • 0.3.0



341
342
343
344
345
346
347
# File 'lib/lotus/controller/configuration.rb', line 341

def prepare(&blk)
  if block_given?
    @modules.push(blk)
  else
    raise ArgumentError.new('Please provide a block')
  end
end

#reset!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.

Reset all the values to the defaults

Since:

  • 0.2.0



658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/lotus/controller/configuration.rb', line 658

def reset!
  @handle_exceptions       = true
  @handled_exceptions      = {}
  @modules                 = []
  @formats                 = DEFAULT_FORMATS.dup
  @default_request_format  = nil
  @default_response_format = nil
  @default_charset         = nil
  @default_headers         = {}
  @cookies                 = {}
  @action_module           = ::Lotus::Action
end