Module: DataMapper

Extended by:
Assertions
Defined in:
lib/dm-core.rb,
lib/dm-core/is.rb,
lib/dm-core/hook.rb,
lib/dm-core/type.rb,
lib/dm-core/model.rb,
lib/dm-core/query.rb,
lib/dm-core/scope.rb,
lib/dm-core/types.rb,
lib/dm-core/logger.rb,
lib/dm-core/version.rb,
lib/dm-core/migrator.rb,
lib/dm-core/property.rb,
lib/dm-core/resource.rb,
lib/dm-core/type_map.rb,
lib/dm-core/collection.rb,
lib/dm-core/repository.rb,
lib/dm-core/types/text.rb,
lib/dm-core/transaction.rb,
lib/dm-core/associations.rb,
lib/dm-core/identity_map.rb,
lib/dm-core/property_set.rb,
lib/dm-core/types/object.rb,
lib/dm-core/types/serial.rb,
lib/dm-core/types/boolean.rb,
lib/dm-core/support/errors.rb,
lib/dm-core/auto_migrations.rb,
lib/dm-core/dependency_queue.rb,
lib/dm-core/naming_conventions.rb,
lib/dm-core/support/assertions.rb,
lib/dm-core/types/discriminator.rb,
lib/dm-core/adapters/mysql_adapter.rb,
lib/dm-core/types/paranoid_boolean.rb,
lib/dm-core/associations/one_to_one.rb,
lib/dm-core/types/paranoid_datetime.rb,
lib/dm-core/adapters/sqlite3_adapter.rb,
lib/dm-core/associations/many_to_one.rb,
lib/dm-core/associations/one_to_many.rb,
lib/dm-core/adapters/abstract_adapter.rb,
lib/dm-core/adapters/postgres_adapter.rb,
lib/dm-core/associations/many_to_many.rb,
lib/dm-core/associations/relationship.rb,
lib/dm-core/adapters/data_objects_adapter.rb,
lib/dm-core/associations/relationship_chain.rb,
lib/dm-core/migrations/destructive_migrations.rb

Overview

TODO: move to dm-more/dm-migrations

Defined Under Namespace

Modules: Adapters, Assertions, Associations, AutoMigrations, DestructiveMigrations, Hook, Is, Model, NamingConventions, Resource, Scope, Types Classes: AutoMigrator, Collection, DependencyQueue, DestructiveMigrator, IdentityMap, IncompleteResourceError, Logger, MaterializationError, Migrator, ObjectNotFoundError, PersistenceError, PluginNotFoundError, Property, PropertySet, Query, Repository, RepositoryNotSetupError, Transaction, Type, TypeMap, ValidationError

Constant Summary collapse

VERSION =
'0.9.6'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Assertions

assert_kind_of

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/dm-core/logger.rb', line 36

def logger
  @logger
end

Class Method Details

.auto_migrate!(repository_name = nil) ⇒ Object

drops and recreates the repository upwards to match model definitions

Parameters:

  • name (Symbol)

    repository to act on, :default is the default



209
210
211
# File 'lib/dm-core.rb', line 209

def self.auto_migrate!(repository_name = nil)
  AutoMigrator.auto_migrate(repository_name)
end

.auto_upgrade!(repository_name = nil) ⇒ Object



213
214
215
# File 'lib/dm-core.rb', line 213

def self.auto_upgrade!(repository_name = nil)
  AutoMigrator.auto_upgrade(repository_name)
end

.dependency_queueObject



221
222
223
# File 'lib/dm-core.rb', line 221

def self.dependency_queue
  @dependency_queue ||= DependencyQueue.new
end

.migrate!(name = Repository.default_name) ⇒ Object

destructively migrates the repository upwards to match model definitions

Parameters:

  • name (Symbol) (defaults to: Repository.default_name)

    repository to act on, :default is the default



201
202
203
# File 'lib/dm-core.rb', line 201

def self.migrate!(name = Repository.default_name)
  repository(name).migrate!
end

.prepare(*args) {|repository(*args)| ... } ⇒ Object

Yields:



217
218
219
# File 'lib/dm-core.rb', line 217

def self.prepare(*args, &blk)
  yield repository(*args)
end

.repository(*args) {|Proc| ... } ⇒ Object

Block Syntax

Pushes the named repository onto the context-stack,
yields a new session, and pops the context-stack.

Non-Block Syntax

Returns the current session, or if there is none,
a new Session.

Parameters:

  • args (Symbol)

    the name of a repository to act within or return, :default is default

Yields:

  • (Proc)

    (optional) block to execute within the context of the named repository



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dm-core.rb', line 172

def self.repository(*args, &block) # :yields: current_context
  if args.size > 1
    raise ArgumentError, "Can only pass in one optional argument, but passed in #{args.size} arguments", caller
  end

  if args.any? && !args.first.kind_of?(Symbol)
    raise ArgumentError, "First optional argument must be a Symbol, but was #{args.first.inspect}", caller
  end

  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?

  current_repository.scope(&block)
end

.rootObject



108
109
110
# File 'lib/dm-core.rb', line 108

def self.root
  @root ||= Pathname(__FILE__).dirname.parent.expand_path
end

.setup(name, uri_or_options) ⇒ Object

Setups up a connection to a data-store

-

Parameters:

  • Symbol

    name a name for the context, defaults to :default

  • uri_or_options (Hash{Symbol => String}, Addressable::URI, String)

    connection information

Returns:

  • Repository the resulting setup repository

Raises:

  • (ArgumentError)

    uri_or_options must be a Hash, URI or String, but was…” indicates that connection information could not be gleaned from the given uri_or_options<Hash, Addressable::URI, String>



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dm-core.rb', line 129

def self.setup(name, uri_or_options)
  assert_kind_of 'name',           name,           Symbol
  assert_kind_of 'uri_or_options', uri_or_options, Addressable::URI, Hash, String

  case uri_or_options
    when Hash
      adapter_name = uri_or_options[:adapter].to_s
    when String, Addressable::URI
      uri_or_options = Addressable::URI.parse(uri_or_options) if uri_or_options.kind_of?(String)
      adapter_name = uri_or_options.scheme
  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 => e
      begin
        require lib_name
      rescue Exception
        # library not found, raise the original error
        raise e
      end
    end
  end

  Repository.adapters[name] = Adapters::const_get(class_name).new(name, uri_or_options)
end

.Type(primitive_type, options = {}) ⇒ Object

class Type



156
157
158
# File 'lib/dm-core/type.rb', line 156

def self.Type(primitive_type, options = {})
  Class.new(Type).configure(primitive_type, options)
end