Sha256: 241f7389343dada5bd839694f5cede969a5453938f41fe8c2eed2e9eb739d8fd

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module Alf
  module Rack
    class Config < Support::Config

      # The database instance to use for obtaining connections
      option :database, Database, nil

      # The connection options to use
      option :connection_options, Hash, {}

      # Enclose all requests in a single database transaction?
      option :transactional, Boolean, true

      # Transaction options to use
      option :transaction_options, Hash, {}

      # Sets the database, coercing it if required
      def database=(db)
        @database = db.is_a?(Database) ? db : Alf.database(db)
      end

      # Returns the default viewpoint to use
      def viewpoint
        connection_options[:viewpoint]
      end

      # Sets the default viewpoint on connection options
      def viewpoint=(vp)
        connection_options[:viewpoint] = vp
      end

    ### At runtime (requires having dup the config first)

      # The current database connection
      attr_reader :connection

      # Connects to the database, starts a transaction if required, then
      # yields the block with the connection object.
      #
      # The connection is kept under an instance variable and can be later
      # obtained through the `connection` accessor. Do NOT use this method
      # without having called `dup` on the config object as it relies on
      # shared mutable state.
      def connect(&bl)
        return yield unless database
        database.connect(connection_options) do |conn|
          @connection = conn
          if transactional?
            conn.in_transaction(transaction_options){ yield(conn) }
          else
            yield(conn)
          end
        end
      end

      # Reconnect with new options. This method is provided if you want to
      # use another viewpoint than the original one in the middle of the
      # request treatment.
      def reconnect(opts)
        connection.reconnect(opts)
      end

    end # class Config
  end # module Rack
end # module Alf

Version data entries

4 entries across 4 versions & 1 rubygems

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