Module: ScanDB::Database

Defined in:
lib/scandb/database.rb

Constant Summary collapse

CONFIG_FILE =

Database configuration file

File.join(Config::PATH,'database.yml')
DEFAULT_CONFIG =

Default database configuration

"sqlite3://" + File.join(Config::PATH,'scandb.db')
DEFAULT_LOG_PATH =

Default log path

File.join(Config::PATH,'scandb.log')
DEFAULT_LOG_LEVEL =

Default log level

:info

Class Method Summary collapse

Class Method Details

.configObject

Returns the Database configuration that is stored in the CONFIG_FILE. Defaults to DEFAULT_CONFIG if CONFIG_FILE does not exist.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/scandb/database.rb', line 54

def Database.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

    return conf
  end

  return DEFAULT_CONFIG
end

.setup(configuration = DEFAULT_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.



91
92
93
94
95
96
97
98
99
# File 'lib/scandb/database.rb', line 91

def Database.setup(configuration=DEFAULT_CONFIG,&block)
  Database.setup_log
  DataMapper.setup(:default, configuration)

  block.call if block

  DataMapper.auto_upgrade!
  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.



77
78
79
80
81
82
83
84
# File 'lib/scandb/database.rb', line 77

def Database.setup_log(options={})
  path = (options[:path] || DEFAULT_LOG_PATH)
  stream = (options[:stream] || File.new(path,'w+'))
  level = (options[:level] || DEFAULT_LOG_LEVEL)

  DataMapper::Logger.new(stream,level)
  return nil
end