Class: Sniff::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sniff/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options)
  self.root = root
  self.test_support_path = File.join(root, 'features', 'support')
  self.load_data = options[:load_data]
  self.fixtures_path = options[:fixtures_path]
  self.logger = Sniff.logger
end

Instance Attribute Details

#fixtures_pathObject

Returns the value of attribute fixtures_path.



56
57
58
# File 'lib/sniff/database.rb', line 56

def fixtures_path
  @fixtures_path
end

#load_dataObject

Returns the value of attribute load_data.



56
57
58
# File 'lib/sniff/database.rb', line 56

def load_data
  @load_data
end

#loggerObject

Returns the value of attribute logger.



56
57
58
# File 'lib/sniff/database.rb', line 56

def logger
  @logger
end

#rootObject

Returns the value of attribute root.



56
57
58
# File 'lib/sniff/database.rb', line 56

def root
  @root
end

#test_support_pathObject

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(options)
  options[:db_adapter] ||= 'sqlite3'
  options[:db_name] ||= ':memory:'
  ActiveRecord::Base.logger = Sniff.logger
  ActiveRecord::Base.establish_connection :adapter => options[:db_adapter],
    :database => options[: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, options = {})
  db_init options
  earth_init(options[:earth])

  environments = []
  environments << init_environment(local_root, options)

  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, options = {})
  db = new root, options
  db.init
  db
end

Instance Method Details

#create_emitter_tableObject



97
98
99
# File 'lib/sniff/database.rb', line 97

def create_emitter_table
  emitter_class.auto_upgrade! if emitter_class
end

#emitter_classObject



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

#initObject



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

Returns:

  • (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_libsObject



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_fixturesObject



101
102
103
# File 'lib/sniff/database.rb', line 101

def populate_fixtures
  Fixture.load_fixtures fixtures_path
end