class Mongrel2::Config

The base Mongrel2 database-backed configuration class. It's a subclass of Sequel::Model, so you'll first need to be familiar with Sequel (sequel.rubyforge.org/) and especially its Sequel::Model ORM.

You will also probably want to refer to the Sequel::Plugins documentation for the validation_helpers and subclasses plugins.

References

Constants

CONFIG_DEFAULTS

Configuration defaults

CONFIG_SQL

The Pathname of the SQL file used to create the config database

DEFAULTS
MIMETYPES_SQL

The Pathname of the SQL file used to add default mimetypes mappings to the config database

Public Class Methods

configure( config=nil ) click to toggle source

Configurability API – called when the configuration is loaded with the 'mongrel2' section of the config file if there is one. This method can also be used without Configurability by passing an object that can be merged with Mongrel2::Config::CONFIG_DEFAULTS.

# File lib/mongrel2/config.rb, line 118
def self::configure( config=nil )
        return unless config

        config = CONFIG_DEFAULTS.merge( config )

        if dbspec = config[ :configdb ]
                # Assume it's a path to a sqlite database if it doesn't have a schema
                dbspec = "%s://%s" % [ self.sqlite_adapter, dbspec ] unless
                        dbspec.include?( ':' )
                self.db = Sequel.connect( dbspec )
        end
end
database_initialized?() click to toggle source

Returns true if the config database has been installed. This currently only checks to see if the 'server' table exists for the sake of speed.

# File lib/mongrel2/config.rb, line 194
def self::database_initialized?
        return self.without_sql_logging do
                self.db.table_exists?( :server )
        end
end
db=( newdb ) click to toggle source

Reset the database connection that all model objects will use to newdb, which should be a Sequel::Database.

# File lib/mongrel2/config.rb, line 134
def self::db=( newdb )
        @db = newdb
        if @dataset
                Loggability.for_logger( self ).with_level( :fatal ) do
                        set_dataset( newdb.dataset.clone(@dataset.opts) )
                end
        end

        if self == Mongrel2::Config
                newdb.logger = Loggability[ Mongrel2 ].proxy_for( newdb )
                newdb.sql_log_level = :debug

                self.descendents.each do |subclass|
                        self.log.debug "Resetting database connection for %p to: %p (%#16x)" %
                                [ subclass, newdb, newdb.object_id * 2 ]
                        subclass.db = newdb
                end
        end
end
dbname() click to toggle source

Return the name of the current config database, or nil if the current database is an in-memory one.

# File lib/mongrel2/config.rb, line 226
def self::dbname
        if self.db.opts[:database]
                return self.db.opts[:database]
        elsif self.db.uri
                return URI( self.db.uri )
        else
                return nil
        end
end
in_memory_db() click to toggle source

Return a Sequel::Database for an in-memory database via the available SQLite library

# File lib/mongrel2/config.rb, line 109
def self::in_memory_db
        return Sequel.connect( adapter: self.sqlite_adapter )
end
init_database() click to toggle source

Initialize the currently-configured database (if it hasn't been already)

# File lib/mongrel2/config.rb, line 202
def self::init_database
        return if self.database_initialized?
        return self.init_database!
end
init_database!() click to toggle source

Initialize the currently-configured database, dropping any existing tables.

# File lib/mongrel2/config.rb, line 209
def self::init_database!
        sql = self.load_config_schema
        mimetypes_sql = self.load_mimetypes_sql

        self.log.warn "Installing config schema."

        self.db.execute_ddl( sql )
        self.db.run( mimetypes_sql )

        # Force the associations to reset
        self.db = db
        self.log_action( "Initialized the config database." )
end
load_config_schema() click to toggle source

Return the contents of the configuration schema SQL file.

# File lib/mongrel2/config.rb, line 181
def self::load_config_schema
        return CONFIG_SQL.read
end
load_mimetypes_sql() click to toggle source

Return the contents of the mimetypes SQL file.

# File lib/mongrel2/config.rb, line 187
def self::load_mimetypes_sql
        return MIMETYPES_SQL.read
end
log_action( what, why=nil, where=nil, how=nil ) click to toggle source

Log an entry to the commit log with the given what, why, where, and how values and return it after it's saved.

# File lib/mongrel2/config.rb, line 239
def self::log_action( what, why=nil, where=nil, how=nil )
        Mongrel2::Config::Log.log_action( what, why, where, how )
end
mimetypes() click to toggle source

Return a Hash of current mimetypes from the config database keyed by extension.

# File lib/mongrel2/config.rb, line 171
def self::mimetypes
        unless @mimetypes
                @mimetypes = Mongrel2::Config::Mimetype.to_hash( :extension, :mimetype )
                @mimetypes.freeze
        end
        return @mimetypes
end
servers() click to toggle source

Return the Array of currently-configured servers in the config database as Mongrel2::Config::Server objects.

# File lib/mongrel2/config.rb, line 157
def self::servers
        return Mongrel2::Config::Server.all
end
settings() click to toggle source

Return a Hash of current settings from the config database. The keys are converted to Symbols.

# File lib/mongrel2/config.rb, line 164
def self::settings
        setting_hash = Mongrel2::Config::Setting.to_hash( :key, :value )
        return Mongrel2::Table.new( setting_hash )
end
sqlite_adapter() click to toggle source

Return the name of the Sequel SQLite adapter to use. If the amalgalite library is available, this will return 'amalgalite', else it returns 'sqlite'.

# File lib/mongrel2/config.rb, line 99
def self::sqlite_adapter
        if defined?( ::Amalgalite )
                return 'amalgalite'
        else
                return 'sqlite'
        end
end

Protected Class Methods

without_sql_logging( logged_db=nil ) { || ... } click to toggle source

Execute a block after removing all loggers from the current database handle, then restore them before returning.

# File lib/mongrel2/config.rb, line 251
def self::without_sql_logging( logged_db=nil )
        logged_db ||= self.db

        loggers_to_restore = logged_db.loggers.dup
        logged_db.loggers.clear
        yield
ensure
        logged_db.loggers.replace( loggers_to_restore )
end