Class: Sniff::Database
- Inherits:
-
Object
- Object
- Sniff::Database
- Defined in:
- lib/sniff/database.rb
Instance Attribute Summary collapse
-
#fixtures_path ⇒ Object
Returns the value of attribute fixtures_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.
-
#test_support_path ⇒ Object
Returns the value of attribute test_support_path.
Class Method Summary collapse
-
.db_init(options) ⇒ Object
Connect to the database and set up an ActiveRecord logger.
-
.earth_init(domains) ⇒ Object
Initialize Earth, tell it to load schemas defined by each domain model’s data_miner definition.
-
.init(local_root, options = {}) ⇒ Object
Initialize a database used for testing emitter gems.
- .init_environment(root, options = {}) ⇒ Object
Instance Method Summary collapse
- #create_emitter_table ⇒ Object
- #emitter_class ⇒ Object
- #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
Constructor Details
#initialize(root, options) ⇒ Database
Returns a new instance of Database.
59 60 61 62 63 64 65 |
# File 'lib/sniff/database.rb', line 59 def initialize(root, ) self.root = root self.test_support_path = File.join(root, 'features', 'support') self.load_data = [:load_data] self.fixtures_path = [:fixtures_path] self.logger = Sniff.logger end |
Instance Attribute Details
#fixtures_path ⇒ Object
Returns the value of attribute fixtures_path.
56 57 58 |
# File 'lib/sniff/database.rb', line 56 def fixtures_path @fixtures_path end |
#load_data ⇒ Object
Returns the value of attribute load_data.
56 57 58 |
# File 'lib/sniff/database.rb', line 56 def load_data @load_data end |
#logger ⇒ Object
Returns the value of attribute logger.
56 57 58 |
# File 'lib/sniff/database.rb', line 56 def logger @logger end |
#root ⇒ Object
Returns the value of attribute root.
56 57 58 |
# File 'lib/sniff/database.rb', line 56 def root @root end |
#test_support_path ⇒ Object
Returns the value of attribute test_support_path.
56 57 58 |
# File 'lib/sniff/database.rb', line 56 def test_support_path @test_support_path end |
Class Method Details
.db_init(options) ⇒ Object
Connect to the database and set up an ActiveRecord logger
36 37 38 39 40 41 42 |
# File 'lib/sniff/database.rb', line 36 def db_init() [:db_adapter] ||= 'sqlite3' [:db_name] ||= ':memory:' ActiveRecord::Base.logger = Sniff.logger ActiveRecord::Base.establish_connection :adapter => [:db_adapter], :database => [:db_name] end |
.earth_init(domains) ⇒ Object
Initialize Earth, tell it to load schemas defined by each domain model’s data_miner definition
46 47 48 49 50 51 52 53 |
# File 'lib/sniff/database.rb', line 46 def earth_init(domains) domains ||= :none domains = [domains] unless domains.is_a? Array args = domains args << {:apply_schemas => true} Earth.init *args 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)
-
:fixtures_path is the path to your gem’s fixtures (default: local_root/features/support/db/fixtures)
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sniff/database.rb', line 16 def init(local_root, = {}) db_init earth_init([:earth]) environments = [] environments << init_environment(local_root, ) unless local_root == Sniff.root fixtures_path = File.join(Sniff.root, 'lib', 'test_support', 'db', 'fixtures') environments << init_environment(Sniff.root, :fixtures_path => fixtures_path) end end |
.init_environment(root, options = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/sniff/database.rb', line 29 def init_environment(root, = {}) db = new root, db.init db end |
Instance Method Details
#create_emitter_table ⇒ Object
97 98 99 |
# File 'lib/sniff/database.rb', line 97 def create_emitter_table emitter_class.auto_upgrade! if emitter_class end |
#emitter_class ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sniff/database.rb', line 86 def emitter_class return @emitter_class unless @emitter_class.nil? record_class_path = Dir.glob(File.join(test_support_path, '*_record.rb')).first if record_class_path require record_class_path record_class = File.read(record_class_path) klass = record_class.scan(/class ([^\s]*Record)/).flatten.first @emitter_class = klass.constantize end end |
#init ⇒ Object
80 81 82 83 84 |
# File 'lib/sniff/database.rb', line 80 def init load_supporting_libs create_emitter_table Fixture.load_fixtures fixtures_path end |
#load_data? ⇒ Boolean
71 72 73 74 |
# File 'lib/sniff/database.rb', line 71 def load_data? @load_data = true if @load_data.nil? @load_data end |
#load_supporting_libs ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/sniff/database.rb', line 105 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
67 68 69 |
# File 'lib/sniff/database.rb', line 67 def log(str) logger.info str end |
#populate_fixtures ⇒ Object
101 102 103 |
# File 'lib/sniff/database.rb', line 101 def populate_fixtures Fixture.load_fixtures fixtures_path end |