Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created May 8, 2009 03:21
Show Gist options
  • Save snusnu/108589 to your computer and use it in GitHub Desktop.
Save snusnu/108589 to your computer and use it in GitHub Desktop.
# Find out what storage engines are installed and this supported by datamapper
# This could be especially useful in testing scenarios, where one wants to run
# specs against all adapters that are available on the machine in use.
require "rubygems"
require "dm-core"
require "spec"
module DataMapper
def self.available_storage_engines
[ :sqlite3, :mysql, :postgres ].select do |name|
storage_engine_available?(name)
end
end
def self.storage_engine_available?(name)
::DataObjects.const_defined?(Extlib::Inflection.classify(name.to_s))
end
end
# spec helper
# set it so that it fits your actually installed do_xxx drivers
AVAILABLE_STORAGE_ENGINES = [
:sqlite3,
:mysql
# :postgres
]
describe 'DataMapper storage engine reflection: ' do
describe 'DataMapper.storage_engine_available?(name)' do
it 'should return true for adapters known to be installed on this machine' do
DataMapper.storage_engine_available?(:sqlite3).should == AVAILABLE_STORAGE_ENGINES.include?(:sqlite3)
DataMapper.storage_engine_available?(:mysql).should == AVAILABLE_STORAGE_ENGINES.include?(:mysql)
DataMapper.storage_engine_available?(:postgres).should == AVAILABLE_STORAGE_ENGINES.include?(:postgres)
end
end
describe 'DataMapper.available_storage_engines' do
it 'should try all adapters shipped with dm-core and return all adapters known to be installed on this machine' do
storage_engines = DataMapper.available_storage_engines
storage_engines.size.should == AVAILABLE_STORAGE_ENGINES.size
storage_engines.each do |storage_engine|
AVAILABLE_STORAGE_ENGINES.should include(storage_engine)
end
end
end
end
# mungo:Desktop snusnu$ spec -cfs storage_engine_reflection.rb
#
# DataMapper storage engine reflection: DataMapper.storage_engine_available?(name)
# - should return true for adapters known to be installed on this machine
#
# DataMapper storage engine reflection: DataMapper.available_storage_engines
# - should try all adapters shipped with dm-core and return all adapters known to be installed on this machine
#
# Finished in 0.002688 seconds
#
# 2 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment