Class: Hanami::Configuration Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/configuration.rb,
lib/hanami/configuration/app.rb,
lib/hanami/configuration/middleware.rb

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

Defined Under Namespace

Classes: App, Middleware

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ 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.

Returns a new instance of Configuration


13
14
15
16
# File 'lib/hanami/configuration.rb', line 13

def initialize(&blk)
  @settings = Concurrent::Map.new
  instance_eval(&blk)
end

Instance Method Details

#appsObject

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.

Since:

  • 0.9.0


139
140
141
142
143
# File 'lib/hanami/configuration.rb', line 139

def apps
  mounted.each_pair do |klass, app|
    yield(app) if klass <= Hanami::Application
  end
end

#early_hints(value = nil) ⇒ 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.

Setup Early Hints feature

Examples:

Enable for all the environments

# config/environment.rb
Hanami.configure do
  early_hints true
end

Enable only for production

# config/environment.rb
Hanami.configure do
  environment :production do
    early_hints true
  end
end

Since:

  • 1.2.0


129
130
131
132
133
134
135
# File 'lib/hanami/configuration.rb', line 129

def early_hints(value = nil)
  if value.nil?
    settings.fetch(:early_hints, false)
  else
    settings[:early_hints] = value
  end
end

#environment(name) ⇒ 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.

Configure settings for the current environment

Examples:

Configure Logging for Different Environments

# config/environment.rb
# ...
Hanami.configure do
  # ...
  environment :development do
    logger level: :debug
  end

  environment :production do
    logger level: :info, formatter: :json
  end
end

Parameters:

  • name (Symbol)

    the name of the Hanami environment

See Also:

Since:

  • 1.0.0


203
204
205
# File 'lib/hanami/configuration.rb', line 203

def environment(name)
  yield if ENV['HANAMI_ENV'] == name.to_s
end

#logger(*options) ⇒ 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.

Configure logger

Examples:

Basic Usage

# config/environment.rb
# ...
Hanami.configure do
  # ...
  environment :development do
    logger level: :debug
  end
end

Daily Rotation

# config/environment.rb
# ...
Hanami.configure do
  # ...
  environment :development do
    logger 'daily', level: :debug
  end
end

Parameters:

  • options (Array)

    a set of options

See Also:

Since:

  • 1.0.0


175
176
177
178
179
180
181
# File 'lib/hanami/configuration.rb', line 175

def logger(*options)
  if options.empty?
    settings.fetch(:logger, nil)
  else
    settings[:logger] = options
  end
end

#mailer(&blk) ⇒ 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.

Configure mailer

Examples:

# config/environment.rb
# ...
Hanami.configure do
  mailer do
    root 'lib/bookshelf/mailers'

    # See https://guides.hanamirb.org/mailers/delivery
    delivery :test
  end

  # ...
end

Parameters:

  • blk (Proc)

    the mailer configuration

See Also:

  • Mailer.configure

84
85
86
# File 'lib/hanami/configuration.rb', line 84

def mailer(&blk)
  mailer_settings.push(blk) if block_given?
end

#mailer_settingsObject

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.

Since:

  • next


90
91
92
# File 'lib/hanami/configuration.rb', line 90

def mailer_settings
  settings.fetch_or_store(:mailers, [])
end

#middlewareObject

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.

Examples:

# config/environment.rb
# ...
Hanami.configure do
  middleware.use MyRackMiddleware
end

Since:

  • 1.2.0


108
109
110
# File 'lib/hanami/configuration.rb', line 108

def middleware
  settings.fetch_or_store(:middleware, Configuration::Middleware.new)
end

#model(&blk) ⇒ 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.

Configure database

Examples:

# config/environment.rb
# ...
Hanami.configure do
  model do
    adapter :sql, ENV['DATABASE_URL']

    migrations 'db/migrations'
    schema     'db/schema.sql'
  end

  # ...
end

Parameters:

  • blk (Proc)

    the database configuration

See Also:

  • Model.configure

57
58
59
60
61
62
63
# File 'lib/hanami/configuration.rb', line 57

def model(&blk)
  if block_given?
    settings.put_if_absent(:model, blk)
  else
    settings.fetch(:model)
  end
end

#mount(app, options) ⇒ 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.

Mount a Hanami::Application or a Rack app

Examples:

# config/environment.rb
# ...
Hanami.configure do
  mount Web::Application, at: '/'

  # ...
end

Parameters:

  • app (#call)

    an application compatible with Rack SPEC

  • options (Hash)

    a set of options

  • :at (Hash)

    a customizable set of options

Since:

  • 0.9.0


34
35
36
# File 'lib/hanami/configuration.rb', line 34

def mount(app, options)
  mounted[app] = App.new(app, options.fetch(:at))
end

#mountedObject

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.

Since:

  • 0.9.0


96
97
98
# File 'lib/hanami/configuration.rb', line 96

def mounted
  settings.fetch_or_store(:mounted, {})
end