Class: Sniff::Database
- Inherits:
-
Object
- Object
- Sniff::Database
- Defined in:
- lib/sniff/database.rb
Instance Attribute Summary collapse
-
#fixtures ⇒ Object
Returns the value of attribute fixtures.
-
#fixtures_path ⇒ Object
Returns the value of attribute fixtures_path.
-
#lib_path ⇒ Object
Returns the value of attribute lib_path.
-
#load_data ⇒ Object
Returns the value of attribute load_data.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
-
.define_schema(&blk) ⇒ Object
Used within an emitter’s custom schema definition - aggregates muliple schemas into a single schema generation transaction.
-
.init(local_root, options = {}) ⇒ Object
Initialize a database used for testing emitter gems.
-
.schemas ⇒ Object
The list of schemas that have been loaded via define_schema.
Instance Method Summary collapse
- #init ⇒ Object
-
#initialize(root, options) ⇒ Database
constructor
A new instance of Database.
- #load_data? ⇒ Boolean
- #load_supporting_libs ⇒ Object
- #log(str) ⇒ Object
- #populate_fixtures ⇒ Object
- #read_fixtures ⇒ Object
- #read_schema ⇒ Object
- #schema_path ⇒ Object
Constructor Details
#initialize(root, options) ⇒ Database
Returns a new instance of Database.
95 96 97 98 99 100 101 |
# File 'lib/sniff/database.rb', line 95 def initialize(root, ) self.root = root self.lib_path = File.join(root, 'lib', 'test_support') self.load_data = [:load_data] self.fixtures_path = [:fixtures_path] self.logger = Logger.new [:logdev] end |
Instance Attribute Details
#fixtures ⇒ Object
Returns the value of attribute fixtures.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def fixtures @fixtures end |
#fixtures_path ⇒ Object
Returns the value of attribute fixtures_path.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def fixtures_path @fixtures_path end |
#lib_path ⇒ Object
Returns the value of attribute lib_path.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def lib_path @lib_path end |
#load_data ⇒ Object
Returns the value of attribute load_data.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def load_data @load_data end |
#logger ⇒ Object
Returns the value of attribute logger.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def logger @logger end |
#root ⇒ Object
Returns the value of attribute root.
92 93 94 |
# File 'lib/sniff/database.rb', line 92 def root @root end |
Class Method Details
.define_schema(&blk) ⇒ Object
Used within an emitter’s custom schema definition - aggregates muliple schemas into a single schema generation transaction.
Instead of: ActiveRecord::Schema.define(:version => XYZ) do It’s: Sniff::Database.define_schema do
41 42 43 |
# File 'lib/sniff/database.rb', line 41 def define_schema(&blk) schemas << blk end |
.init(local_root, options = {}) ⇒ Object
Initialize a database used for testing emitter gems
local_root: Root directory of the emitter gem to be tested (path to the repo) options:
-
:earth is the list of domains Earth.init should load (default: none)
-
:load_data determines whether fixture data is loaded (default: true)
-
:sqllogdev is a Logger log device used by ActiveRecord (default: nil)
-
:fixtures_path is the path to your gem’s fixtures (default: local_root/lib/db/fixtures)
-
:logdev is a Logger log device used for general logging (default: nil)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sniff/database.rb', line 18 def init(local_root, = {}) db_init earth_init([:earth]) environments = [] environments << init_environment(local_root, ) unless local_root == Sniff.root environments << init_environment(Sniff.root) end load_all_schemas environments.each { |e| e.populate_fixtures } end |
.schemas ⇒ Object
The list of schemas that have been loaded via define_schema
46 47 48 49 |
# File 'lib/sniff/database.rb', line 46 def schemas @schemas = [] unless defined?(@schemas) @schemas end |
Instance Method Details
#init ⇒ Object
124 125 126 127 128 |
# File 'lib/sniff/database.rb', line 124 def init load_supporting_libs read_schema read_fixtures if load_data? end |
#load_data? ⇒ Boolean
107 108 109 110 |
# File 'lib/sniff/database.rb', line 107 def load_data? @load_data = true if @load_data.nil? @load_data end |
#load_supporting_libs ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/sniff/database.rb', line 157 def load_supporting_libs $:.unshift File.join(root, 'lib') Dir[File.join(root, 'lib', 'test_support', '*.rb')].each do |lib| log "Loading #{lib}" require lib end end |
#log(str) ⇒ Object
103 104 105 |
# File 'lib/sniff/database.rb', line 103 def log(str) logger.info str end |
#populate_fixtures ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sniff/database.rb', line 144 def populate_fixtures fixtures.each do |fixture_file| klass = File.basename(fixture_file, '.csv'). camelize.singularize pluralized_klass = klass.pluralize klass = klass.pluralize unless Object.const_defined?(klass) if Object.const_defined?(klass) and klass.constantize.table_exists? log "Loading fixture #{fixture_file}" Fixtures.create_fixtures(fixtures_path, fixture_file[(fixtures_path.size + 1)..-5]) end end end |
#read_fixtures ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/sniff/database.rb', line 135 def read_fixtures require 'active_record/fixtures' log "Reading fixtures from #{fixtures_path}/**/*.{yml,csv}" Dir["#{fixtures_path}/**/*.{yml,csv}"].each do |fixture_file| fixtures << fixture_file end end |
#read_schema ⇒ Object
130 131 132 133 |
# File 'lib/sniff/database.rb', line 130 def read_schema log "Reading schema #{schema_path}" load(schema_path) if File.exist?(schema_path) end |
#schema_path ⇒ Object
112 113 114 |
# File 'lib/sniff/database.rb', line 112 def schema_path @schema_path ||= File.join(lib_path, 'db', 'schema.rb') end |