Module: Squixtures
- Defined in:
- lib/squixtures/version.rb,
lib/squixtures.rb,
lib/squixtures/loader.rb,
lib/squixtures/fixture.rb,
lib/squixtures/fixtures.rb,
lib/squixtures/exceptions.rb,
lib/squixtures/helper_factory.rb,
lib/squixtures/sqlite3_helper.rb,
lib/squixtures/postgres_helper.rb
Overview
! /usr/bin/env ruby Copyright ©, 2012 Peter Wood
Defined Under Namespace
Modules: Fixtures, HelperFactory Classes: Fixture, Loader, PostgresHelper, SQLite3Helper, SquixtureError
Constant Summary collapse
- DEFAULT_FIXTURES_DIR =
Definition of the default fixtures directory.
"#{Dir.getwd}/test/fixtures"
- DEFAULT_DATABASE_CFG_FILE =
Definition for the default database configuration file name.
"database.yml"
- DEFAULT_CFG_SEARCH_PATHS =
Definition of the default configuration search paths.
["#{Dir.getwd}/config", "#{Dir.getwd}/test/fixtures", Dir.getwd]
- FIXTURES_SEARCH_PATHS =
Definition of the fixtures search paths.
["#{Dir.getwd}/test/fixtures", "#{Dir.getwd}/fixtures"]
- CONFIGURATION_DEFAULTS =
Definition of the configuration defaults.
{:clear_tables => true, :database => nil, :environment => "test", :search_paths => DEFAULT_CFG_SEARCH_PATHS, :transactional => false}
- VERSION =
"0.0.3"
- @@configuration =
Module configuration store.
{}.merge(CONFIGURATION_DEFAULTS)
Class Method Summary collapse
-
.configuration ⇒ Object
This method fetches configuration associated with the Squixtures module.
-
.configuration=(configuration) ⇒ Object
This method allows the configuration of the Squixtures module.
-
.environment ⇒ Object
This method fetches the current environment setting for the Squixtures module.
-
.environment=(setting) ⇒ Object
This method is used to set the environment that the Squixtures module will use when loading fixtures.
-
.find_fixtures_dir ⇒ Object
This method searches for a directory containing fixtures files, returning a string with a path to the directory if it’s found or nil if it isn’t.
-
.fixtures(*names) ⇒ Object
This method performs the actual configuration and load of a set of fixtures.
-
.fixtures_dir ⇒ Object
This method provides a means of fetching the path to the directory that is expected to contain the fixtures files.
-
.fixtures_dir=(path) ⇒ Object
This method allows for the specification of the directory that contains the fixture files.
-
.get_connection_url(settings) ⇒ Object
This method provides a means of converting a typical set of database connection settings, such as those in a Rails database.yml file, into the URL to be used to connect to the database using the Sequel library.
-
.load_database_configuration ⇒ Object
This method loads the database configuration details into the current Squixtures configuration.
-
.transactional=(setting) ⇒ Object
This method allows the toggling of transactional fixture loading.
-
.transactional? ⇒ Boolean
This method provides a means of checking whether transactional fixtures have been activated in the library.
Class Method Details
.configuration ⇒ Object
This method fetches configuration associated with the Squixtures module. Note that, due to the late loading of the database configuration, the connection details will not be available until after a load.
60 61 62 |
# File 'lib/squixtures.rb', line 60 def self.configuration @@configuration end |
.configuration=(configuration) ⇒ Object
This method allows the configuration of the Squixtures module.
53 54 55 |
# File 'lib/squixtures.rb', line 53 def self.configuration=(configuration) @@configuration = CONFIGURATION_DEFAULTS.merge(configuration) end |
.environment ⇒ Object
This method fetches the current environment setting for the Squixtures module.
79 80 81 |
# File 'lib/squixtures.rb', line 79 def self.environment @@configuration[:environment] end |
.environment=(setting) ⇒ Object
This method is used to set the environment that the Squixtures module will use when loading fixtures. Note that calling this method will also invoke an immediate reload of database adapter/connection details.
Parameters
- setting
-
A String, usually one of “test”, “development” or “production” but can be anything else as long as it matches an entry in the database configuration settings.
72 73 74 75 |
# File 'lib/squixtures.rb', line 72 def self.environment=(setting) @@configuration[:environment] = setting Squixtures.load_database_configuration end |
.find_fixtures_dir ⇒ Object
This method searches for a directory containing fixtures files, returning a string with a path to the directory if it’s found or nil if it isn’t.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/squixtures.rb', line 148 def self.find_fixtures_dir return @@configuration[:fixtures_dir] if !@@configuration[:fixtures_dir].nil? FIXTURES_SEARCH_PATHS.find do |entry| found = false if File.exist?(entry) found = Dir.glob("#{entry}/*.yml").size > 0 @@configuration[:fixtures_dir] = entry if found end found end end |
.fixtures(*names) ⇒ Object
This method performs the actual configuration and load of a set of fixtures.
Parameters
- *names
-
The list of fixture names to be loaded.
47 48 49 50 |
# File 'lib/squixtures.rb', line 47 def self.fixtures(*names) load_database_configuration if !@@configuration[:database] Loader.new(@@configuration).load(*names) end |
.fixtures_dir ⇒ Object
This method provides a means of fetching the path to the directory that is expected to contain the fixtures files.
85 86 87 |
# File 'lib/squixtures.rb', line 85 def self.fixtures_dir @@configuration[:fixtures_dir].nil? ? Squixtures.find_fixtures_dir : @@configuration[:fixtures_dir] end |
.fixtures_dir=(path) ⇒ Object
This method allows for the specification of the directory that contains the fixture files.
Parameters
- path
-
The path of the fixtures directory.
94 95 96 |
# File 'lib/squixtures.rb', line 94 def self.fixtures_dir=(path) @@configuration[:fixtures_dir] = path end |
.get_connection_url(settings) ⇒ Object
This method provides a means of converting a typical set of database connection settings, such as those in a Rails database.yml file, into the URL to be used to connect to the database using the Sequel library.
Parameters
- settings
-
A Hash of the settings that will be used to generate the connection URL.
167 168 169 |
# File 'lib/squixtures.rb', line 167 def self.get_connection_url(settings) HelperFactory.create_helper(settings).get_connection_url(settings) end |
.load_database_configuration ⇒ Object
This method loads the database configuration details into the current Squixtures configuration.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/squixtures.rb', line 115 def self.load_database_configuration log = LogJam.get_logger("squixtures") log.debug "Loading database configuration." success = @@configuration[:search_paths].find do |path| found = false file_path = "#{path}/#{DEFAULT_DATABASE_CFG_FILE}" log.debug "Checking for the existence of #{file_path}." if File.exist?(file_path) begin log.debug "#{file_path} exists, attempting a load." settings = YAML.load_file(file_path) environment = @@configuration[:environment] if settings.include?(environment) @@configuration[:database] = settings[environment] found = true log.debug "Database configuration loaded from #{file_path}." else log.warn "The #{file_path} database configuration does not contain a #{environment} entry." end rescue => error log.warn "Load of the #{file_path} database configuration file failed." end end found end if !success raise SquixtureError.new("Unable to locate a database configuration file.") end end |
.transactional=(setting) ⇒ Object
This method allows the toggling of transactional fixture loading.
Parameters
- setting
-
A boolean value indicating whether transactional loading should be used with the fixtures.
109 110 111 |
# File 'lib/squixtures.rb', line 109 def self.transactional=(setting) @@configuration[:transactional] = setting end |
.transactional? ⇒ Boolean
This method provides a means of checking whether transactional fixtures have been activated in the library.
100 101 102 |
# File 'lib/squixtures.rb', line 100 def self.transactional? @@configuration[:transactional] end |