Module: DataMapper
Setup and Configuration
DataMapper uses URIs or a connection hash to connect to your data-store. URI connections takes the form of:
DataMapper.setup(:default, 'protocol://username:password@localhost:port/path/to/repo')
Breaking this down, the first argument is the name you wish to give this connection. If you do not specify one, it will be assigned :default. If you would like to connect to more than one data-store, simply issue this command again, but with a different name specified.
In order to issue ORM commands without specifying the repository context, you must define the :default database. Otherwise, you’ll need to wrap your ORM calls in repository(:name) { }.
Second, the URI breaks down into the access protocol, the username, the server, the password, and whatever path information is needed to properly address the data-store on the server.
Here’s some examples
DataMapper.setup(:default, "sqlite3://path/to/your/project/db/development.db") DataMapper.setup(:default, "mysql://localhost/dm_core_test") DataMapper.setup(:default, "postgres://root:supahsekret@127.0.0.1/dm_core_test")
Alternatively, you can supply a hash as the second parameter, which would take the form:
DataMapper.setup(:default, {
:adapter => 'adapter_name_here',
:database => "path/to/repo",
:username => 'username',
:password => 'password',
:host => 'hostname'
})
Logging
To turn on error logging to STDOUT, issue:
DataMapper::Logger.new(STDOUT, 0)
You can pass a file location ("/path/to/log/file.log") in place of STDOUT. see DataMapper::Logger for more information.
Attributes
Class Attributes
| logger | [RW] | public |
Sets the attribute logger. |
|---|
Public Visibility
Public Class Method Summary
| auto_migrate!(name = Repository.default_name) |
drops and recreates the repository upwards to match model definitions. |
|---|---|
| migrate!(name = Repository.default_name) |
destructively migrates the repository upwards to match model definitions. |
| prepare(*args, &blk) | |
| repository(*args) | |
| root | |
| setup(name, uri_or_options) |
Setups up a connection to a data-store. Returns: |
Public Class Method Details
auto_migrate!
drops and recreates the repository upwards to match model definitions
199 200 201
# File 'dm-core/lib/dm-core.rb', line 199 def self.auto_migrate!(name = Repository.default_name) repository(name).auto_migrate! end
migrate!
destructively migrates the repository upwards to match model definitions
191 192 193
# File 'dm-core/lib/dm-core.rb', line 191 def self.migrate!(name = Repository.default_name) repository(name).migrate! end
prepare
203 204 205
# File 'dm-core/lib/dm-core.rb', line 203 def self.prepare(*args, &blk) yield repository(*args) end
repository
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
# File 'dm-core/lib/dm-core.rb', line 161 def self.repository(*args) # :yields: current_context raise ArgumentError, "Can only pass in one optional argument, but passed in #{args.size} arguments", caller unless args.size <= 1 raise ArgumentError, "First optional argument must be a Symbol, but was #{args.first.inspect}", caller unless args.empty? || Symbol === args.first name = args.first current_repository = if name Repository.context.detect { |r| r.name == name } || Repository.new(name) else Repository.context.last || Repository.new(Repository.default_name) end return current_repository unless block_given? Repository.context << current_repository begin return yield(current_repository) ensure Repository.context.pop end end
root
99 100 101 102
# File 'dm-core/lib/dm-core.rb', line 99 def self.root @root ||= Pathname(__FILE__).dirname.parent.expand_path end
setup
Setups up a connection to a data-store
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# File 'dm-core/lib/dm-core.rb', line 119 def self.setup(name, uri_or_options) raise ArgumentError, "+name+ must be a Symbol, but was #{name.class}", caller unless Symbol === name case uri_or_options when Hash adapter_name = uri_or_options[:adapter] when String, Addressable::URI uri_or_options = Addressable::URI.parse(uri_or_options) if String === uri_or_options adapter_name = uri_or_options.scheme else raise ArgumentError, "+uri_or_options+ must be a Hash, Addressable::URI or String, but was #{uri_or_options.class}", caller end class_name = Extlib::Inflection.classify(adapter_name) + 'Adapter' unless Adapters::const_defined?(class_name) lib_name = "#{Extlib::Inflection.underscore(adapter_name)}_adapter" begin require root / 'lib' / 'dm-core' / 'adapters' / lib_name rescue LoadError require lib_name end end Repository.adapters[name] = Adapters::const_get(class_name).new(name, uri_or_options) end