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.



95
96
97
98
99
100
101
# File 'lib/sniff/database.rb', line 95

def initialize(root, options)
  self.root = root
  self.lib_path = File.join(root, 'lib', 'test_support')
  self.load_data = options[:load_data]
  self.fixtures_path = options[:fixtures_path]
  self.logger = Logger.new options[:logdev]
end

Instance Attribute Details

#fixturesObject

Returns the value of attribute fixtures.



92
93
94
# File 'lib/sniff/database.rb', line 92

def fixtures
  @fixtures
end

#fixtures_pathObject

Returns the value of attribute fixtures_path.



92
93
94
# File 'lib/sniff/database.rb', line 92

def fixtures_path
  @fixtures_path
end

#lib_pathObject

Returns the value of attribute lib_path.



92
93
94
# File 'lib/sniff/database.rb', line 92

def lib_path
  @lib_path
end

#load_dataObject

Returns the value of attribute load_data.



92
93
94
# File 'lib/sniff/database.rb', line 92

def load_data
  @load_data
end

#loggerObject

Returns the value of attribute logger.



92
93
94
# File 'lib/sniff/database.rb', line 92

def logger
  @logger
end

#rootObject

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, options = {})
  db_init options
  earth_init(options[:earth])

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

  unless local_root == Sniff.root
    environments << init_environment(Sniff.root)
  end
  
  load_all_schemas

  environments.each { |e| e.populate_fixtures }
end

.schemasObject

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

#initObject



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

Returns:

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



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_fixturesObject



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_fixturesObject



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_schemaObject



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_pathObject



112
113
114
# File 'lib/sniff/database.rb', line 112

def schema_path
  @schema_path ||= File.join(lib_path, 'db', 'schema.rb')
end