Class: Lotus::Configuration

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

Overview

Configuration for a Lotus application

Since:

  • 0.1.0

Constant Summary collapse

SSL_SCHEME =

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.

See Also:

Since:

  • 0.2.0

'https'.freeze

Instance Method Summary collapse

Constructor Details

#initializeLotus::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.

Initialize a new configuration instance

Since:

  • 0.1.0



30
31
32
33
34
# File 'lib/lotus/configuration.rb', line 30

def initialize
  @blk = Proc.new{}
  @env = Environment.new
  @configurations = Hash.new { |k, v| k[v] = [] }
end

Instance Method Details

#adapter(options) ⇒ Object #adapterHash

Adapter configuration. The application will instantiate adapter instance based on this configuration.

The given options must have key pairs :type and :uri If it isn’t, at the runtime the framework will raise a ‘ArgumentError`.

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:

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      adapter type: :sql, uri: 'sqlite3://uri'
    end
  end
end

Bookshelf::Application.configuration.adapter
  # => {type: :sql, uri: 'sqlite3://uri'}

Overloads:

  • #adapter(options) ⇒ Object

    Sets the given type and uri

    Parameters:

    • options (Hash)

      a set of options for adapter

  • #adapterHash

    Gets the value

    Returns:

    • (Hash)

      adapter options

See Also:

Since:

  • 0.2.0



891
892
893
894
895
896
897
# File 'lib/lotus/configuration.rb', line 891

def adapter(options = {})
  if !options.empty?
    @adapter = options
  else
    @adapter
  end
end

#assetsLotus::Config::Assets

The application will serve the static assets under these directories.

By default it’s equal to the ‘public/` directory under the application `root`.

Otherwise, you can add differents relatives paths under ‘root`.

Examples:

Getting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.assets
  # => #<Pathname:/root/path/public>

Adding new assets paths

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      assets do
        sources << [
          'vendor/assets'
        ]
      end
    end
  end
end

Bookshelf::Application.configuration.assets
  # => #<Lotus::Config::Assets @root=#<Pathname:/root/path/assets>, @paths=["public"]>

Gets the value

Returns:

  • (Lotus::Config::Assets)

    assets root

Since:

  • 0.1.0



419
420
421
422
423
424
425
# File 'lib/lotus/configuration.rb', line 419

def assets(&blk)
  if @assets
    @assets.__add(&blk)
  else
    @assets ||= Config::FrameworkConfiguration.new(&blk)
  end
end

#body_parsers(parsers) ⇒ Object #body_parsersArray

Body parsing configuration.

Specify a set of parsers for specific mime types that your application will use. This method will return the application’s parsers which you can use to add existing and new custom parsers for your application to use.

By default it’s an empty ‘Array`

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.body_parsers
  # => []

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      body_parsers :json, XmlParser.new
    end
  end
end

Bookshelf::Application.configuration.body_parsers
  # => [:json, XmlParser.new]

Setting a new value after one is set.

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      body_parsers :json
      body_parsers XmlParser.new
    end
  end
end

Bookshelf::Application.configuration.body_parsers
  # => [XmlParser.new]

Overloads:

  • #body_parsers(parsers) ⇒ Object

    Specify a set of body parsers.

    Parameters:

    • parsers (Array)

      the body parser definitions

  • #body_parsersArray

    Gets the value

    Returns:

    • (Array)

      the set of parsers

Since:

  • 0.2.0



737
738
739
740
741
742
743
# File 'lib/lotus/configuration.rb', line 737

def body_parsers(*parsers)
  if parsers.empty?
    @body_parsers ||= []
  else
    @body_parsers = parsers
  end
end

#configure(environment = nil, path = nil, &blk) ⇒ self

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.

Set a block yield when the configuration will be loaded or set a path for the specific environment.

Parameters:

  • environment (Symbol, nil) (defaults to: nil)

    the configuration environment name

  • blk (Proc)

    the configuration block

Returns:

  • (self)

Since:

  • 0.1.0



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lotus/configuration.rb', line 46

def configure(environment = nil, path = nil, &blk)
  if environment && path
    @configurations[environment.to_s] << Config::Configure.new(root, path, &blk)
  elsif environment
    @configurations[environment.to_s] << blk
  else
    @blk = blk
  end

  self
end

#controllerLotus::Config::FrameworkConfiguration

It lazily collects all the low level settings for Lotus::Controller’s configuration and applies them when the application is loaded.

NOTE: This forwards all the configurations to Lotus::Controller, without checking them. Before to use this feature, please have a look at the current Lotus::Controller version installed.

NOTE: This may override some configurations of your application.

Examples:

Define a setting

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      controller.default_request_format :json
    end
  end
end

Override a setting

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      handle_exceptions            false
      controller.handle_exceptions true
    end
  end
end

# Exceptions will be handled

Returns:

See Also:

Since:

  • 0.2.0



1613
1614
1615
# File 'lib/lotus/configuration.rb', line 1613

def controller
  @controller ||= Config::FrameworkConfiguration.new
end

#controller_pattern(value) ⇒ Object #controller_patternString

Defines a relative pattern to find controllers.

Lotus supports multiple architectures (aka application structures), this setting helps to understand the namespace where to find applications’ controllers and actions.

By default this equals to "Controllers::%{controller}::%{action}" That means controllers must be structured like this: Bookshelf::Controllers::Dashboard::Index, where Bookshelf is the application module, Controllers is the first value specified in the pattern, Dashboard the controller and Index the action.

This pattern MUST always contain "%{controller}" and %{action}. This pattern SHOULD be used accordingly to #view_pattern value.

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'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

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

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

Bookshelf::Application.configuration.controller_pattern
  # => "Controllers::%{controller}::%{action}"

# All the controllers MUST live under Bookshelf::Controllers

# GET '/' # => Bookshelf::Controllers::Dashboard::Index

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      controller_pattern "%{controller}Controller::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

  module DashboardController
    class Index
      include Bookshelf::Action

      def call(params)
      end
    end
  end
end

Bookshelf::Application.configuration.controller_pattern
  # => "%{controller}Controller::%{action}"

# All the controllers are directly under the Bookshelf module

# GET '/' # => Bookshelf::DashboardController::Index

Setting the value for a top level name structure

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      namespace Object
      controller_pattern "%{controller}Controller::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end
end

module DashboardController
  class Index
    incude Bookshelf::Action

    def call(params)
    end
  end
end

Bookshelf::Application.configuration.controller_pattern
  # => "%{controller}Controller::%{action}"

# All the controllers are at the top level namespace

# GET '/' # => DashboardController::Index

Overloads:

  • #controller_pattern(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the controller pattern

  • #controller_patternString

    Gets the value

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0



1310
1311
1312
1313
1314
1315
1316
# File 'lib/lotus/configuration.rb', line 1310

def controller_pattern(value = nil)
  if value
    @controller_pattern = value
  else
    @controller_pattern ||= 'Controllers::%{controller}::%{action}'
  end
end

#cookies(options) ⇒ Object #cookiesLotus::Config::Cookies

Configure cookies Enable cookies (disabled by default).

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.cookies
  # => #<Lotus::Config::Cookies:0x0000000329f880 @options={}, @default_options={:httponly=>true, :secure=>false}>

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      cookies domain: 'lotusrb.org'
    end
  end
end

Bookshelf::Application.configuration.cookies
  # => #<Lotus::Config::Cookies:0x0000000329f880 @options={:domain=>'lotusrb.org'}, @default_options={:domain=>'lotusrb.org', :httponly=>true, :secure=>false}>

Overloads:

Since:

  • 0.1.0



466
467
468
469
470
471
472
# File 'lib/lotus/configuration.rb', line 466

def cookies(options = nil)
  if options.nil?
    @cookies ||= Config::Cookies.new(self, options)
  else
    @cookies = Config::Cookies.new(self, options)
  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.1.0



1016
1017
1018
1019
# File 'lib/lotus/configuration.rb', line 1016

def default_format(format = nil)
  Lotus::Utils::Deprecation.new('default_format is deprecated, please use default_request_format')
  default_request_format(format)
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 ‘:html`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.default_request_format # => :html

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      default_request_format :json
    end
  end
end

Bookshelf::Application.configuration.default_request_format # => :json

Overloads:

  • #default_request_format(format) ⇒ Object

    Sets the given value

    Parameters:

    • format (#to_sym)

      the symbol format

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_request_formatSymbol

    Gets the value

    Returns:

    • (Symbol)

See Also:

Since:

  • 0.5.0



947
948
949
950
951
952
953
# File 'lib/lotus/configuration.rb', line 947

def default_request_format(format = nil)
  if format
    @default_request_format = Utils::Kernel.Symbol(format)
  else
    @default_request_format || :html
  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 ‘:html`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.default_response_format # => :html

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      default_response_format :json
    end
  end
end

Bookshelf::Application.configuration.default_response_format # => :json

Overloads:

  • #default_response_format(format) ⇒ Object

    Sets the given value

    Parameters:

    • format (#to_sym)

      the symbol format

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_response_formatSymbol?

    Gets the value

    Returns:

    • (Symbol, nil)

See Also:

Since:

  • 0.5.0



1002
1003
1004
1005
1006
1007
1008
# File 'lib/lotus/configuration.rb', line 1002

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

#force_ssl(value = nil) ⇒ Boolean

Force ssl redirection if http scheme is set

Returns:

  • (Boolean)

See Also:

  • Routing::ForceSsl

Since:

  • 0.4.0



118
119
120
121
122
123
124
# File 'lib/lotus/configuration.rb', line 118

def force_ssl(value = nil)
  if value
    @force_ssl = value
  else
    @force_ssl || false
  end
end

#handle_exceptions(value) ⇒ Object #handle_exceptionsTrueClass, FalseClass

Decide if 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:

Enabled (default)

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/error', to: 'error#index'
      end
    end

    load!
  end

  module Controllers::Error
    class Index
      include Bookshelf::Action

      def call(params)
        raise ArgumentError
      end
    end
  end
end

# GET '/error' # => 500 - Internal Server Error

Disabled

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      handle_exceptions false

      routes do
        get '/error', to: 'error#index'
      end
    end

    load!
  end

  module Controllers::Error
    class Index
      include Bookshelf::Action

      def call(params)
        raise ArgumentError
      end
    end
  end
end

# GET '/error' # => raises ArgumentError

Overloads:

  • #handle_exceptions(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (TrueClass, FalseClass)

      true or false, default to true

  • #handle_exceptionsTrueClass, FalseClass

    Gets the value

    Returns:

    • (TrueClass, FalseClass)

See Also:

Since:

  • 0.2.0



1521
1522
1523
1524
1525
1526
1527
# File 'lib/lotus/configuration.rb', line 1521

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

#host(value) ⇒ Object #schemeString

The URI host for this application. This is used by the router helpers to generate absolute URLs.

By default this value is ‘“localhost”`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.host # => "localhost"

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      host 'bookshelf.org'
    end
  end
end

Bookshelf::Application.configuration.host # => "bookshelf.org"

Overloads:

  • #host(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the URI host

  • #schemeString

    Gets the value

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0



1126
1127
1128
1129
1130
1131
1132
# File 'lib/lotus/configuration.rb', line 1126

def host(value = nil)
  if value
    @host = value
  else
    @host ||= @env.host
  end
end

#layout(value) ⇒ Object #layoutSymbol?

A Lotus::Layout for this application

By default it’s ‘nil`.

It accepts a Symbol as layout name. When the application is loaded, it will lookup for the corresponding class.

All the views will use this layout, unless otherwise specified.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.layout # => nil

# All the views will render without a layout

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      layout :application
    end
  end

  module Views
    module Dashboard
      class Index
        include Bookshelf::Views
      end

      class JsonIndex < Index
        layout nil
      end
    end
  end
end

Bookshelf::Application.configuration.namespace layout => :application

# All the views will use Bookshelf::Views::ApplicationLayout, unless
# they set a different value.

Bookshelf::Views::Dashboard::Index.layout
  # => Bookshelf::Views::ApplicationLayout

Bookshelf::Views::Dashboard::JsonIndex.layout
  # => Lotus::View::Rendering::NullLayout

Overloads:

  • #layout(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Symbol)

      the layout name

  • #layoutSymbol?

    Gets the value

    Returns:

    • (Symbol, nil)

      the layout name

See Also:

Since:

  • 0.1.0



314
315
316
317
318
319
320
# File 'lib/lotus/configuration.rb', line 314

def layout(value = nil)
  if value
    @layout = value
  else
    @layout
  end
end

#load!(namespace = nil) ⇒ self

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 configuration

Parameters:

  • namespace (String, nil) (defaults to: nil)

    the application namespace

Returns:

  • (self)

Since:

  • 0.1.0



66
67
68
69
70
71
# File 'lib/lotus/configuration.rb', line 66

def load!(namespace = nil)
  @namespace = namespace
  evaluate_configurations!

  self
end

#load_pathsLotus::Config::LoadPaths

Application load paths The application will recursively load all the Ruby files under these paths.

By default it’s empty in order to allow developers to decide their own app structure.

Examples:

Getting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.load_paths
  # => #<Lotus::Config::LoadPaths:0x007ff4fa212310 @paths=[]>

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      load_paths << [
        'app/controllers',
        'app/views
      ]
    end
  end
end

Bookshelf::Application.configuration.assets
  # => #<Lotus::Config::LoadPaths:0x007fe3a20b18e0 @paths=[["app/controllers", "app/views"]]>

Returns:

See Also:

Since:

  • 0.1.0



595
596
597
# File 'lib/lotus/configuration.rb', line 595

def load_paths
  @load_paths ||= Config::LoadPaths.new
end

#logger(value = nil) ⇒ Logger, NilClass

Defines a logger instance to the configuration

This logger instance will be used to set the logger available on application module

If no logger instance is defined, a Lotus::Logger will be set by default

Examples:

Define a logger

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      logger Logger.new(STDOUT)
    end
    load!
  end

  module Controllers::Error
    class Index
      include Bookshelf::Action

      def call(params)
        Bookshelf::Logger.info "Logging to STDOUT"
      end
    end
  end
end

Returns:

  • (Logger, NilClass)

    logger instance

Since:

  • 0.5.0



1692
1693
1694
1695
1696
1697
1698
# File 'lib/lotus/configuration.rb', line 1692

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

#mapping(blk) ⇒ Object #mapping(path) ⇒ Object #mappingLotus::Config::Mapping

Application collection mapping.

Specify a set of collections for the application, by passing a block, or a relative path where to find the file that describes them.

By default it’s ‘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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.mapping
  # => nil

Setting the value, by passing a block

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      mapping do
        collection :users do
          entity User

          attribute :id,   Integer
          attribute :name, String
        end
      end
    end
  end
end

Bookshelf::Application.configuration.mapping
  # => #<Lotus::Config::Mapping:0x007ff50a991388 @blk=#<Proc:0x007ff123991338@(irb):4>, @path=#<Pathname:.>>

Setting the value, by passing a relative path

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      mapping 'config/mapping'
    end
  end
end

Bookshelf::Application.configuration.mapping
  # => #<Lotus::Config::Routes:0x007ff50a991388 @blk=nil, @path=#<Pathname:config/mapping.rb>>

Overloads:

  • #mapping(blk) ⇒ Object

    Specify a set of mapping in the given block

    Parameters:

    • blk (Proc)

      the mapping definitions

  • #mapping(path) ⇒ Object

    Specify a relative path where to find the mapping file

    Parameters:

    • path (String)

      the relative path

  • #mappingLotus::Config::Mapping

    Gets the value

    Returns:

See Also:

Since:

  • 0.2.0



846
847
848
849
850
851
852
# File 'lib/lotus/configuration.rb', line 846

def mapping(path = nil, &blk)
  if path or block_given?
    @mapping = Config::Mapping.new(root, path, &blk)
  else
    @mapping
  end
end

#middlewareObject

Application middleware.

Specify middleware that your application will use. This method will return the application’s underlying Middleware stack which you can use to add new middleware for your application to use. By default, the middleware stack will contain only ‘Rack::Static` and `Rack::MethodOverride`. However, if `assets false` was specified # in the configuration block, the default `Rack::Static` will be removed.

Examples:

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      middleware.use Rack::MethodOverride, nil, 'max-age=0, private, must-revalidate'
      middleware.use Rack::ETag
    end
  end
end

See Also:

Since:

  • 0.2.0



770
771
772
# File 'lib/lotus/configuration.rb', line 770

def middleware
  @middleware ||= Lotus::Middleware.new(self)
end

#modelLotus::Config::FrameworkConfiguration

It lazily collects all the low level settings for Lotus::Model’s configuration and applies them when the application is loaded.

NOTE: This forwards all the configurations to Lotus::Model, without checking them. Before to use this feature, please have a look at the current Lotus::Model version installed.

NOTE: This may override some configurations of your application.

Examples:

Define a setting

require 'lotus'
require 'lotus/model'

module Bookshelf
  class Application < Lotus::Application
    configure do
      model.adapter type: :memory, uri: 'memory://localhost/database'
    end
  end
end

Override a setting

require 'lotus'
require 'lotus/model'

module Bookshelf
  class Application < Lotus::Application
    configure do
      adapter       type: :sql,    uri: 'postgres://localhost/database'
      model.adapter type: :memory, uri: 'memory://localhost/database'
    end
  end
end

# The memory adapter will override the SQL one

Returns:

See Also:

Since:

  • 0.2.0



1570
1571
1572
# File 'lib/lotus/configuration.rb', line 1570

def model
  @model ||= Config::FrameworkConfiguration.new
end

#namespace(value) ⇒ Object #namespaceClass, Module

The application namespace

By default it returns the Ruby namespace of the application. For instance for an application ‘Bookshelf::Application`, it returns `Bookshelf`.

This value isn’t set at the init time, but when the configuration is loaded with ‘#load!`.

Lotus applications are namespaced: all the controllers and views live under the application module, without polluting the global namespace. However, if for some reason, you want top level classes, set this value to ‘Object` (which is the top level namespace for Ruby).

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.namespace # => Bookshelf

# It will lookup namespaced controllers under Bookshelf
# eg. Bookshelf::Controllers::Dashboard

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      namespace Object
    end
  end
end

Bookshelf::Application.configuration.namespace # => Object

# It will lookup top level controllers under Object
# eg. DashboardController

Overloads:

  • #namespace(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Class, Module)

      A valid Ruby namespace

  • #namespaceClass, Module

    Gets the value

    Returns:

    • (Class, Module)

      a Ruby namespace

Since:

  • 0.1.0



235
236
237
238
239
240
241
# File 'lib/lotus/configuration.rb', line 235

def namespace(value = nil)
  if value
    @namespace = value
  else
    @namespace
  end
end

#path_prefix(value = nil) ⇒ String, NilClass

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.

This options is used as a bridge between container and router application.

Returns:

  • (String, NilClass)

    path prefix for routes

Since:

  • 0.4.0



1706
1707
1708
1709
1710
1711
1712
# File 'lib/lotus/configuration.rb', line 1706

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

#port(value) ⇒ Object #schemeString

The URI port for this application. This is used by the router helpers to generate absolute URLs.

By default this value is ‘2300`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.port # => 2300

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      port 8080
    end
  end
end

Bookshelf::Application.configuration.port # => 8080

Overloads:

  • #port(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (#to_int)

      the URI port

    Raises:

    • (TypeError)

      if the given value cannot be coerced to Integer

  • #schemeString

    Gets the value

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0



1178
1179
1180
1181
1182
1183
1184
# File 'lib/lotus/configuration.rb', line 1178

def port(value = nil)
  if value
    @port = Integer(value)
  else
    @port || @env.port
  end
end

#root(value) ⇒ Object #rootPathname

The root of the application

By default it returns the current directory, for this reason, **all the commands must be executed from the top level directory of the project**.

If for some reason, that constraint above cannot be satisfied, please configure the root directory, so that commands can be executed from everywhere.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.root # => #<Pathname:/path/to/root>

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      root '/path/to/another/root'
    end
  end
end

Overloads:

  • #root(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String, Pathname, #to_pathname)

      The root directory of the app

  • #rootPathname

    Gets the value

    Returns:

    • (Pathname)

    Raises:

    • (Errno::ENOENT)

      if the path cannot be found

See Also:

Since:

  • 0.1.0



172
173
174
175
176
177
178
# File 'lib/lotus/configuration.rb', line 172

def root(value = nil)
  if value
    @root = value
  else
    Utils::Kernel.Pathname(@root || Dir.pwd).realpath
  end
end

#routes(blk) ⇒ Object #routes(path) ⇒ Object #routesLotus::Config::Routes

Application routes.

Specify a set of routes for the application, by passing a block, or a relative path where to find the file that describes them.

By default it’s ‘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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.routes
  # => nil

Setting the value, by passing a block

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/', to: 'dashboard#index'
        resources :books
      end
    end
  end
end

Bookshelf::Application.configuration.routes
  # => #<Lotus::Config::Routes:0x007ff50a991388 @blk=#<Proc:0x007ff50a991338@(irb):4>, @path=#<Pathname:.>>

Setting the value, by passing a relative path

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes 'config/routes'
    end
  end
end

Bookshelf::Application.configuration.routes
  # => #<Lotus::Config::Routes:0x007ff50a991388 @blk=nil, @path=#<Pathname:config/routes.rb>>

Overloads:

  • #routes(blk) ⇒ Object

    Specify a set of routes in the given block

    Parameters:

    • blk (Proc)

      the routes definitions

  • #routes(path) ⇒ Object

    Specify a relative path where to find the routes file

    Parameters:

    • path (String)

      the relative path

  • #routesLotus::Config::Routes

    Gets the value

    Returns:

See Also:

Since:

  • 0.1.0



667
668
669
670
671
672
673
# File 'lib/lotus/configuration.rb', line 667

def routes(path = nil, &blk)
  if path or block_given?
    @routes = Config::Routes.new(root, path, &blk)
  else
    @routes
  end
end

#scheme(value) ⇒ Object #schemeString

The URI scheme for this application. This is used by the router helpers to generate absolute URLs.

By default this value is ‘“http”`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.scheme # => "http"

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      scheme 'https'
    end
  end
end

Bookshelf::Application.configuration.scheme # => "https"

Overloads:

  • #scheme(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the URI scheme

  • #schemeString

    Gets the value

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0



1064
1065
1066
1067
1068
1069
1070
# File 'lib/lotus/configuration.rb', line 1064

def scheme(value = nil)
  if value
    @scheme = value
  else
    @scheme ||= 'http'
  end
end

#securityLotus::Config::Security

Returns the security policy

Examples:

Getting values

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      security.x_frame_options         "ALLOW ALL"
      security.content_security_policy "script-src 'self' https://apis.example.com"
    end
  end
end

Bookshelf::Application.configuration.security.x_frame_options         # => "ALLOW ALL"
Bookshelf::Application.configuration.security.content_security_policy # => "script-src 'self' https://apis.example.com"

Setting values

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      security.x_frame_options         "ALLOW ALL"
      security.content_security_policy "script-src 'self' https://apis.example.com"
    end
  end
end

Returns:

See Also:

Since:

  • 0.3.0



107
108
109
# File 'lib/lotus/configuration.rb', line 107

def security
  @security ||= Config::Security.new
end

#sessions(adapter, options) ⇒ Object #sessions(false) ⇒ Object #sessionsLotus::Config::Sessions

Configure sessions Enable sessions (disabled by default).

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.

Given Class as adapter it will be used as sessions middleware. Given String as adapter it will be resolved as class name and used as sessions middleware. Given Symbol as adapter it is assumed it’s name of the class under Rack::Session namespace that will be used as sessions middleware (e.g. :cookie for Rack::Session::Cookie).

By default options include domain inferred from host configuration, and secure flag inferred from scheme configuration.

Examples:

Getting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.sessions
  # => #<Lotus::Config::Sessions:0x00000001ca0c28 @enabled=false>

Setting the value with symbol

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      sessions :cookie, secret: 'abc123'
    end
  end
end

Bookshelf::Application.configuration.sessions
  # => #<Lotus::Config::Sessions:0x00000001589458 @enabled=true, @adapter=:cookie, @options={:domain=>"localhost", :secure=>false}>

Disabling previusly enabled sessions

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      sessions :cookie
      sessions false
    end
  end
end

Bookshelf::Application.configuration.sessions
  # => #<Lotus::Config::Sessions:0x00000002460d78 @enabled=false>

Overloads:

  • #sessions(adapter, options) ⇒ Object

    Sets the given value.

    Parameters:

    • adapter (Class, String, Symbol)

      Rack middleware for sessions management

    • options (Hash)

      options to pass to sessions middleware

  • #sessions(false) ⇒ Object

    Disables sessions

  • #sessionsLotus::Config::Sessions

    Gets the value.

    Returns:

See Also:

Since:

  • 0.2.0



548
549
550
551
552
553
554
# File 'lib/lotus/configuration.rb', line 548

def sessions(adapter = nil, options = {})
  if adapter.nil?
    @sessions ||= Config::Sessions.new
  else
    @sessions = Config::Sessions.new(adapter, options, self)
  end
end

#ssl?FalseClass, TrueClass

Check if the application uses SSL

Returns:

  • (FalseClass, TrueClass)

    the result of the check

See Also:

Since:

  • 0.2.0



1079
1080
1081
# File 'lib/lotus/configuration.rb', line 1079

def ssl?
  scheme == SSL_SCHEME
end

#templates(value) ⇒ Object #templatesPathname

Templates root. The application will recursively look for templates under this path.

By default it’s equal to the application ‘root`.

Otherwise, you can specify a different relative path under ‘root`.

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'

module Bookshelf
  class Application < Lotus::Application
  end
end

Bookshelf::Application.configuration.templates
  # => #<Pathname:/root/path>

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      templates 'app/templates'
    end
  end
end

Bookshelf::Application.configuration.templates
  # => #<Pathname:/root/path/app/templates>

Overloads:

  • #templates(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the relative path to the templates root.

  • #templatesPathname

    Gets the value

    Returns:

    • (Pathname)

      templates root

See Also:

Since:

  • 0.1.0



370
371
372
373
374
375
376
# File 'lib/lotus/configuration.rb', line 370

def templates(value = nil)
  if value
    @templates = value
  else
    root.join @templates.to_s
  end
end

#viewLotus::Config::FrameworkConfiguration

It lazily collects all the low level settings for Lotus::View’s configuration and applies them when the application is loaded.

NOTE: This forwards all the configurations to Lotus::View, without checking them. Before to use this feature, please have a look at the current Lotus::View version installed.

NOTE: This may override some configurations of your application.

Examples:

Define a setting

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      view.layout :application
    end
  end
end

Override a setting

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      layout      :application
      view.layout :backend
    end
  end
end

# It will use `:backend` layout

Returns:

See Also:

Since:

  • 0.2.0



1656
1657
1658
# File 'lib/lotus/configuration.rb', line 1656

def view
  @view ||= Config::FrameworkConfiguration.new
end

#view_pattern(value) ⇒ Object #controller_patternString

Defines a relative pattern to find views:.

Lotus supports multiple architectures (aka application structures), this setting helps to understand the namespace where to find applications’ views:.

By default this equals to "Views::%{controller}::%{action}" That means views must be structured like this: Bookshelf::Views::Dashboard::Index, where Bookshelf is the application module, Views is the first value specified in the pattern, Dashboard a module corresponding to the controller name and Index the view, corresponding to the action name.

This pattern MUST always contain "%{controller}" and %{action}. This pattern SHOULD be used accordingly to #controller_pattern value.

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'

module Bookshelf
  class Application < Lotus::Application
    configure do
      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

  module Views
    module Dashboard
      class Index
        include Bookshelf::View
      end
    end
  end
end

Bookshelf::Application.configuration.view_pattern
  # => "Views::%{controller}::%{action}"

# All the views MUST live under Bookshelf::Views

# GET '/' # => Bookshelf::Views::Dashboard::Index

Setting the value

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      view_pattern "%{controller}::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end

  module Dashboard
    class Index
      include Bookshelf::View
    end
  end
end

Bookshelf::Application.configuration.view_pattern
  # => "%{controller}::%{action}"

# All the views are directly under the Bookshelf module

# GET '/' # => Bookshelf::Dashboard::Index

Setting the value for a top level name structure

require 'lotus'

module Bookshelf
  class Application < Lotus::Application
    configure do
      namespace Object
      view_pattern "%{controller}::%{action}"

      routes do
        get '/', to: 'dashboard#index'
      end
    end
  end
end

module Dashboard
  class Index
    include Bookshelf::View
  end
end

Bookshelf::Application.configuration.view_pattern
  # => "%{controller}::%{action}"

# All the views: are at the top level namespace

# GET '/' # => Dashboard::Index

Overloads:

  • #view_pattern(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String)

      the view pattern

  • #controller_patternString

    Gets the value

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0



1434
1435
1436
1437
1438
1439
1440
# File 'lib/lotus/configuration.rb', line 1434

def view_pattern(value = nil)
  if value
    @view_pattern = value
  else
    @view_pattern ||= 'Views::%{controller}::%{action}'
  end
end