Module: Ronin::Database

Constants

CONFIG_FILE
File.join(Config::PATH,'database.yml')
DEFAULT_CONFIG
"sqlite3://" + File.join(Config::PATH,'database.sqlite3')
DEFAULT_LOG_LEVEL
:info
DEFAULT_LOG_PATH
File.join(Config::PATH,'database.log')

Public Visibility

Public Class Method Summary

config

Returns the Database configuration that is stored in the.

config=(configuration)

Sets the Database configuration.

log

Returns: DataMapper::Logger

setup(configuration = Database.config, &block)

Sets up the Database.

setup?

Returns: Boolean

setup_log(options = {})

Setup the Database log.

Returns: DataMapper::Logger

update!(&block)

Updates the Database, by running auto-upgrades, but only if the.

Public Class Method Details

config

public config

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

Meta Tags

Raises:

[InvalidConfig]

The config file did not contain a YAML Hash or String.

[View source]


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ronin/database/database.rb', line 55

def Database.config
  unless (class_variable_defined?('@@ronin_database_config'))
    @@ronin_database_config = DEFAULT_CONFIG

    if File.file?(CONFIG_FILE)
      conf = YAML.load(CONFIG_FILE)

      unless (conf.kind_of?(Hash) || conf.kind_of?(String))
        raise(InvalidConfig,"#{CONFIG_FILE} must contain either a Hash or a String",caller)
      end

      @@ronin_database_config = conf
    end
  end

  return @@ronin_database_config ||= DEFAULT_CONFIG
end

config=

public config=(configuration)

Sets the Database configuration.

Meta Tags

Parameters:

[String, Hash] configuration

The DataMapper configuration to set the Ronin::Database with.

[View source]


79
80
81
# File 'lib/ronin/database/database.rb', line 79

def Database.config=(configuration)
  @@ronin_database_config = configuration
end

log

public DataMapper::Logger log

Meta Tags

Returns:

[DataMapper::Logger, nil]

The current Database log.

[View source]


87
88
89
# File 'lib/ronin/database/database.rb', line 87

def Database.log
  @@ronin_database_log ||= nil
end

setup

public setup(configuration = Database.config, &block)

Sets up the Database.

Meta Tags

Parameters:

[String, Hash] configuration

The DataMapper configuration to use to setup the Database.

Yields:

[Object]
The block to call after the Database has been setup, but before it is updated.
[View source]


152
153
154
155
156
157
158
159
160
161
# File 'lib/ronin/database/database.rb', line 152

def Database.setup(configuration=Database.config,&block)
  # setup the database log
  Database.setup_log unless Database.log

  # setup the database repository
  DataMapper.setup(Model::REPOSITORY_NAME, configuration)

  Database.update!(&block)
  return nil
end

setup?

public Boolean setup?

Meta Tags

Returns:

[Boolean]

Specifies wether or not the Database is setup.

[View source]


122
123
124
125
126
# File 'lib/ronin/database/database.rb', line 122

def Database.setup?
  repository = DataMapper.repository(Model::REPOSITORY_NAME)

  return repository.class.adapters.has_key?(repository.name)
end

setup_log

public DataMapper::Logger setup_log(options = {})

Setup the Database log.

Meta Tags

Parameters:

Options Hash options
Key Name Default Value Accepted Types Description
:path DEFAULT_LOG_PATH [String]

The path of the log file.

:stream N/A [IO]

The stream to use for the log.

:level N/A [Symbol]

The level of messages to log. May be either :fatal, :error, :warn, :info or :debug.

Returns:

[DataMapper::Logger]

The new Database log.

[View source]


110
111
112
113
114
115
116
# File 'lib/ronin/database/database.rb', line 110

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

  return @@ronin_database_log = DataMapper::Logger.new(stream,level)
end

update!

public update!(&block)

Updates the Database, by running auto-upgrades, but only if the Database is already setup.

Meta Tags

Yields:

[Object]
The block to call before the Database is updated.
[View source]


135
136
137
138
139
140
# File 'lib/ronin/database/database.rb', line 135

def Database.update!(&block)
  block.call if block

  DataMapper.auto_upgrade!(Model::REPOSITORY_NAME) if Database.setup?
  return nil
end
Generated on Friday, September 25 2009 at 02:57:31 PM by YARD 0.2.3.5 (ruby-1.8.6).