Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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...
commit 2e5c7dfb4057f5a63cf35bff0620ffe2612e9afa 1 parent 4924e23
@snusnu snusnu authored
Showing with 41 additions and 29 deletions.
  1. +41 −24 lib/dm-core/spec/setup.rb
  2. +0 −5 spec/spec_helper.rb
View
65 lib/dm-core/spec/setup.rb
@@ -1,5 +1,4 @@
-require 'pathname'
-require 'active_support/inflector'
+require 'dm-core'
module DataMapper
module Spec
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
View
5 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
@@ -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)

0 comments on commit 2e5c7df

Please sign in to comment.
Something went wrong with that request. Please try again.