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



142
143
144
145
146
# File 'lib/hanami/configuration.rb', line 142

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



132
133
134
135
136
137
138
# File 'lib/hanami/configuration.rb', line 132

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



206
207
208
# File 'lib/hanami/configuration.rb', line 206

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



178
179
180
181
182
183
184
# File 'lib/hanami/configuration.rb', line 178

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


87
88
89
# File 'lib/hanami/configuration.rb', line 87

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



93
94
95
# File 'lib/hanami/configuration.rb', line 93

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



111
112
113
# File 'lib/hanami/configuration.rb', line 111

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


60
61
62
63
64
65
66
# File 'lib/hanami/configuration.rb', line 60

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 Beta::Application, at: '/', host: 'beta.bookshelf.com'
  mount Admin::Application, at: '/api'
  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

  • :host (Hash)

    a customizable set of options

Since:

  • 0.9.0



37
38
39
# File 'lib/hanami/configuration.rb', line 37

def mount(app, options)
  mounted[app] = App.new(app, options)
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



99
100
101
# File 'lib/hanami/configuration.rb', line 99

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