Module: ScanDB::Database
- Defined in:
- lib/scandb/database.rb
Constant Summary collapse
- CONFIG_FILE =
Database configuration file
File.join(Config::PATH,'database.yml')
- DEFAULT_LOG_PATH =
Default log path
File.join(Config::PATH,'database.log')
- DEFAULT_LOG_LEVEL =
Default log level
:error
- DEFAULT_CONFIG =
Default database configuration
{ :adapter => :sqlite3, :database => File.join(Config::PATH,'scandb.db') }
Class Method Summary collapse
-
.config ⇒ Object
Returns the Database configuration that is stored in the
CONFIG_FILE
. -
.config=(configuration) ⇒ Object
Sets the Database configuration to the specified configuration.
-
.log ⇒ Object
Returns the current Database log.
-
.setup(configuration = Database.config, &block) ⇒ Object
Sets up the Database with the given configuration.
-
.setup_log(options = {}) ⇒ Object
Setup the Database log with the given options.
Class Method Details
.config ⇒ Object
Returns the Database configuration that is stored in the CONFIG_FILE
. Defaults to DEFAULT_CONFIG
if CONFIG_FILE
does not exist.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scandb/database.rb', line 59 def Database.config unless class_variable_defined?('@@scandb_database_config') @@scandb_database_config = DEFAULT_CONFIG if File.file?(CONFIG_FILE) conf = YAML.load(CONFIG_FILE) unless (conf.kind_of?(Hash) || conf.kind_of?(String)) raise(InvalidDatabaseConfig,"#{CONFIG_FILE} must contain either a Hash or a String",caller) end @@scandb_database_config = conf end end return @@scandb_database_config ||= DEFAULT_CONFIG end |
.config=(configuration) ⇒ Object
Sets the Database configuration to the specified configuration.
80 81 82 |
# File 'lib/scandb/database.rb', line 80 def Database.config=(configuration) @@scandb_database_config = configuration end |
.log ⇒ Object
Returns the current Database log.
87 88 89 |
# File 'lib/scandb/database.rb', line 87 def Database.log @@scandb_database_log ||= nil end |
.setup(configuration = Database.config, &block) ⇒ Object
Sets up the Database with the given configuration. If _configuration is not given, DEFAULT_CONFIG
will be used to setup the Database.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/scandb/database.rb', line 113 def Database.setup(configuration=Database.config,&block) # setup the logging Database.setup_log unless Database.log # setup the repository DataMapper.setup(Model::REPOSITORY_NAME, configuration) block.call if block # sourced from http://gist.github.com/3010 # in order to fix a has-many lazy-loading bug # in dm-core <= 0.9.4 descendants = DataMapper::Resource.descendants.dup descendants.each do |model| descendants.merge(model.descendants) if model.respond_to?(:descendants) end descendants.each do |model| model.relationships.each_value { |r| r.child_key if r.child_model == model } end DataMapper.auto_upgrade!(Model::REPOSITORY_NAME) return nil end |
.setup_log(options = {}) ⇒ Object
Setup the Database log with the given options.
options may contain the following keys:
:path
-
The path of the log file. Defaults to
DEFAULT_LOG_PATH
. :stream
-
The stream to use for the log.
:level
-
The level of messages to log.
100 101 102 103 104 105 106 |
# File 'lib/scandb/database.rb', line 100 def Database.setup_log(={}) path = ([:path] || DEFAULT_LOG_PATH) stream = ([:stream] || File.new(path,'w+')) level = ([:level] || DEFAULT_LOG_LEVEL) return @@scandb_database_log = DataMapper::Logger.new(stream,level) end |