Sha256: 8ad0583b9844bddf312c5107a8dbbd105a39a787ffffe44482b1d0344013e454

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module Alf
  module Rack
    # 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
    # ```
    class Connect

      CONFIG_KEY = "ALF_RACK_CONFIG".freeze

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

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

    end # class Connect
  end # module Rack
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-rack-0.16.3 lib/alf/rack/connect.rb
alf-rack-0.16.2 lib/alf/rack/connect.rb
alf-rack-0.16.1 lib/alf/rack/connect.rb
alf-rack-0.16.0 lib/alf/rack/connect.rb
alf-rack-0.15.0 lib/alf/rack/connect.rb