Class: DataMapper::Spec::Adapters::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-core/spec/setup.rb

Direct Known Subclasses

InMemoryAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Adapter

Returns a new instance of Adapter.



94
95
96
# File 'lib/dm-core/spec/setup.rb', line 94

def initialize(name)
  @name = name.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



92
93
94
# File 'lib/dm-core/spec/setup.rb', line 92

def name
  @name
end

Instance Method Details

#adapterObject Also known as: setup



98
99
100
# File 'lib/dm-core/spec/setup.rb', line 98

def adapter
  @adapter ||= setup!
end

#adapter_nameObject



112
113
114
# File 'lib/dm-core/spec/setup.rb', line 112

def adapter_name
  @adapter_name ||= infer_adapter_name
end

#alternate_storage_nameObject



128
129
130
# File 'lib/dm-core/spec/setup.rb', line 128

def alternate_storage_name
  "datamapper_alternate_tests"
end

#connection_uriObject



116
117
118
# File 'lib/dm-core/spec/setup.rb', line 116

def connection_uri
  "#{adapter_name}://#{username}:#{password}@#{host}/#{storage_name}"
end

#default_storage_nameObject



124
125
126
# File 'lib/dm-core/spec/setup.rb', line 124

def default_storage_name
  "datamapper_default_tests"
end

#hostObject



140
141
142
# File 'lib/dm-core/spec/setup.rb', line 140

def host
  ENV.fetch('DM_DB_HOST', 'localhost')
end

#passwordObject



136
137
138
# File 'lib/dm-core/spec/setup.rb', line 136

def password
  ENV.fetch('DM_DB_PASSWORD', 'datamapper')
end

#setup!Object



104
105
106
107
108
109
110
# File 'lib/dm-core/spec/setup.rb', line 104

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

#storage_nameObject



120
121
122
# File 'lib/dm-core/spec/setup.rb', line 120

def storage_name
  send("#{name}_storage_name")
end

#test_connection(adapter) ⇒ Object

Test the connection

Overwrite this method if you need to perform custom connection testing

Raises:

  • (Exception)


149
150
151
152
153
# File 'lib/dm-core/spec/setup.rb', line 149

def test_connection(adapter)
  if adapter.respond_to?(:select)
    adapter.select('SELECT 1')
  end
end

#usernameObject



132
133
134
# File 'lib/dm-core/spec/setup.rb', line 132

def username
  ENV.fetch('DM_DB_USER', 'datamapper')
end