Class: Mongrel2::Config

Inherits:
Object
  • Object
show all
Extended by:
Configurability, Loggability
Includes:
Constants
Defined in:
lib/mongrel2/config.rb

Overview

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

Defined Under Namespace

Modules: DSL Classes: Directory, Filter, Handler, Host, Log, Mimetype, Proxy, Route, Server, Setting, Statistic, XRequest

Constant Summary collapse

CONFIG_DEFAULTS =

Configuration defaults

{
	:configdb => Mongrel2::DEFAULT_CONFIG_URI,
}
DEFAULTS =
CONFIG_DEFAULTS
CONFIG_SQL =

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

DATA_DIR + 'config.sql'
MIMETYPES_SQL =

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

DATA_DIR + 'mimetypes.sql'

Constants included from Constants

Mongrel2::Constants::DATA_DIR, Mongrel2::Constants::DEFAULT_CONFIG_SCRIPT, Mongrel2::Constants::DEFAULT_CONFIG_URI, Mongrel2::Constants::DEFAULT_CONTROL_SOCKET, Mongrel2::Constants::MAX_BROADCAST_IDENTS

Class Method Summary collapse

Class Method Details

.configure(config = nil) ⇒ Object

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.


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mongrel2/config.rb', line 119

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?Boolean

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.

Returns:

  • (Boolean)

195
196
197
198
199
# File 'lib/mongrel2/config.rb', line 195

def self::database_initialized?
	return self.without_sql_logging do
		self.db.table_exists?( :server )
	end
end

.db=(newdb) ⇒ Object

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


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mongrel2/config.rb', line 135

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

.dbnameObject

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


227
228
229
230
231
232
233
234
235
# File 'lib/mongrel2/config.rb', line 227

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_dbObject

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


110
111
112
# File 'lib/mongrel2/config.rb', line 110

def self::in_memory_db
	return Sequel.connect( adapter: self.sqlite_adapter )
end

.init_databaseObject

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


203
204
205
206
# File 'lib/mongrel2/config.rb', line 203

def self::init_database
	return if self.database_initialized?
	return self.init_database!
end

.init_database!Object

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


210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mongrel2/config.rb', line 210

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_schemaObject

Return the contents of the configuration schema SQL file.


182
183
184
# File 'lib/mongrel2/config.rb', line 182

def self::load_config_schema
	return CONFIG_SQL.read
end

.load_mimetypes_sqlObject

Return the contents of the mimetypes SQL file.


188
189
190
# File 'lib/mongrel2/config.rb', line 188

def self::load_mimetypes_sql
	return MIMETYPES_SQL.read
end

.log_action(what, why = nil, where = nil, how = nil) ⇒ Object

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


240
241
242
# File 'lib/mongrel2/config.rb', line 240

def self::log_action( what, why=nil, where=nil, how=nil )
	Mongrel2::Config::Log.log_action( what, why, where, how )
end

.mimetypesObject

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


172
173
174
175
176
177
178
# File 'lib/mongrel2/config.rb', line 172

def self::mimetypes
	unless @mimetypes
		@mimetypes = Mongrel2::Config::Mimetype.to_hash( :extension, :mimetype )
		@mimetypes.freeze
	end
	return @mimetypes
end

.serversObject

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


158
159
160
# File 'lib/mongrel2/config.rb', line 158

def self::servers
	return Mongrel2::Config::Server.all
end

.settingsObject

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


165
166
167
168
# File 'lib/mongrel2/config.rb', line 165

def self::settings
	setting_hash = Mongrel2::Config::Setting.to_hash( :key, :value )
	return Mongrel2::Table.new( setting_hash )
end

.sqlite_adapterObject

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


100
101
102
103
104
105
106
# File 'lib/mongrel2/config.rb', line 100

def self::sqlite_adapter
	if defined?( ::Amalgalite )
		return 'amalgalite'
	else
		return 'sqlite'
	end
end