Class: Alf::Rack::Connect

Inherits:
Object
  • Object
show all
Defined in:
lib/alf/rack/connect.rb

Overview

Connect to a database and make the connection available in the Rack environment.

Example:

“‘ require ’sinatra’

use Alf::Rack::Connect do |cfg| # see Alf::Rack::Config

cfg.database = ... # (required) a Alf::Database or Alf::Adapter

end

get ‘/’ do

# the configuration object (a dup of what has been seen above)
config = env[Alf::Rack::Connect::CONFIG_KEY]

# the config object is connected
connection = config.connection
# => Alf::Database::Connection

# ...

end “‘

Constant Summary collapse

CONFIG_KEY =
"ALF_RACK_CONFIG".freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, config = Config.new) {|config| ... } ⇒ Connect

Returns a new instance of Connect.

Yields:

  • (config)


30
31
32
33
34
# File 'lib/alf/rack/connect.rb', line 30

def initialize(app, config = Config.new)
  @app    = app
  @config = config
  yield(config) if block_given?
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
# File 'lib/alf/rack/connect.rb', line 36

def call(env)
  env[CONFIG_KEY] = cfg = @config.dup
  cfg.connect do
    @app.call(env)
  end
end