Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
DataMapper::Spec.setup now performs adapter setup
The following methods on DataMapper::Spec now
delegate directly to the instance methods on the
currently used spec adapter.

  DataMapper::Spec.setup    # setup and memoize
  DataMapper::Spec.setup!   # always call setup
  DataMapper::Spec.adapter  # same as setup

If the ADAPTER and PLUGINS haven't been configured
at the time one of these methods is invoked, it
will be configured before proceeding with setup.

This cuts down the number of code necessary to get
specs with any adapter and plugin combination up
and running. All that needs to be done is

  require 'dm-core/spec/setup'
  DataMapper::Spec.setup # or .adapter

at the appropriate time, in specs or standalone
snippets.
  • Loading branch information
snusnu committed May 3, 2010
1 parent 4924e23 commit 2e5c7df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
65 changes: 41 additions & 24 deletions lib/dm-core/spec/setup.rb
@@ -1,5 +1,4 @@
require 'pathname'
require 'active_support/inflector'
require 'dm-core'

module DataMapper
module Spec
Expand All @@ -12,10 +11,29 @@ def root=(path)
@root = Pathname(path)
end

def setup(root = default_root)
setup_logger(root)
require_plugins
require_adapter
%w[setup setup! adapter].each do |action|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{action}(kind = :default)
perform_action(kind, action)
end
RUBY
end

def adapter_name(kind = :default)
spec_adapters[kind].adapter_name
end

def configure(root = default_root)
@configured = begin
setup_logger(root)
require_plugins
require_spec_adapter
true
end
end

def configured?
@configured
end

def setup_logger(root = default_root)
Expand All @@ -25,7 +43,7 @@ def setup_logger(root = default_root)
logger
end

def require_adapter
def require_spec_adapter
if ENV['ADAPTER'] == 'in_memory'
ENV['ADAPTER_SUPPORTS'] = 'all'
Adapters.use(Adapters::InMemoryAdapter)
Expand All @@ -40,14 +58,6 @@ def require_plugins
plugins.each { |plugin| require plugin }
end

def adapter(kind = :default)
spec_adapters[kind].adapter
end

def adapter_name(kind = :default)
spec_adapters[kind].adapter_name
end

def spec_adapters
@spec_adapters ||= {}
end
Expand All @@ -58,6 +68,11 @@ def default_root
Pathname(caller[1]).dirname.expand_path
end

def perform_action(kind, action)
configure unless configured?
spec_adapters[kind].send(action)
end

end

module Adapters
Expand All @@ -76,7 +91,17 @@ def initialize(name)
end

def adapter
@adapter ||= setup
@adapter ||= setup!
end

alias :setup :adapter

def setup!
adapter = DataMapper.setup(name, connection_uri)
test_connection(adapter)
adapter
rescue Exception => e
puts "Could not connect to the database using '#{connection_uri}' because of: #{e.inspect}"
end

def adapter_name
Expand Down Expand Up @@ -120,14 +145,6 @@ def test_connection(adapter)

private

def setup
adapter = DataMapper.setup(name, connection_uri)
test_connection(adapter)
adapter
rescue Exception => e
puts "Could not connect to the database using '#{connection_uri}' because of: #{e.inspect}"
end

def infer_adapter_name
demodulized = ActiveSupport::Inflector.demodulize(self.class.name.chomp('Adapter'))
ActiveSupport::Inflector.underscore(demodulized).freeze
Expand Down
5 changes: 0 additions & 5 deletions spec/spec_helper.rb
@@ -1,9 +1,6 @@
require 'pathname'
require 'rubygems'

require 'spec'

require 'dm-core'
require 'dm-core/spec/setup'

SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
Expand All @@ -12,8 +9,6 @@
Pathname.glob((LIB_ROOT + 'dm-core/spec/**/*.rb' ).to_s).each { |file| require file }
Pathname.glob((SPEC_ROOT + '{lib,support,*/shared}/**/*.rb').to_s).each { |file| require file }

DataMapper::Spec.setup # initialize the logger and require the adapter and any given plugins

Spec::Runner.configure do |config|

config.extend( DataMapper::Spec::Adapters::Helpers)
Expand Down

0 comments on commit 2e5c7df

Please sign in to comment.