class DataMapper::Repository

Attributes

name[R]

@api semipublic

to_sym[R]

@api semipublic

Public Class Methods

adapters() click to toggle source

Get the list of adapters registered for all Repositories, keyed by repository name.

TODO: create example

@return [Hash(Symbol => Adapters::AbstractAdapter)]

the adapters registered for all Repositories

@api private

# File lib/dm-core/repository.rb, line 17
def self.adapters
  @adapters ||= {}
end
context() click to toggle source

Get the stack of current repository contexts

TODO: create example

@return [Array]

List of Repository contexts for the current Thread

@api private

# File lib/dm-core/repository.rb, line 29
def self.context
  Thread.current[:dm_repository_contexts] ||= []
end
default_name() click to toggle source

Get the default name of this Repository

TODO: create example

@return [Symbol]

the default name of this repository

@api private

# File lib/dm-core/repository.rb, line 41
def self.default_name
  :default
end
new(name) click to toggle source

Initializes a new Repository

TODO: create example

@param [Symbol] name

The name of the Repository

@api semipublic

# File lib/dm-core/repository.rb, line 221
def initialize(name)
  @name          = name.to_sym
  @identity_maps = {}
end

Public Instance Methods

adapter() click to toggle source

Get the adapter for this repository

Lazy loads adapter setup from registered adapters

TODO: create example

@return [Adapters::AbstractAdapter]

the adapter for this repository

@raise [RepositoryNotSetupError]

if there is no adapter registered for a repository named @name

@api semipublic

# File lib/dm-core/repository.rb, line 64
def adapter
  # Make adapter instantiation lazy so we can defer repository setup until it's actually
  # needed. Do not remove this code.
  @adapter ||=
    begin
      adapters = self.class.adapters

      unless adapters.key?(@name)
        raise RepositoryNotSetupError, "Adapter not set: #{@name}. Did you forget to setup?"
      end

      adapters[@name]
    end
end
create(resources) click to toggle source

Create one or more resource instances in this repository.

TODO: create example

@param [Enumerable(Resource)] resources

The list of resources (model instances) to create

@return [Integer]

The number of records that were actually saved into the data-store

@api semipublic

# File lib/dm-core/repository.rb, line 145
def create(resources)
  adapter.create(resources)
end
delete(collection) click to toggle source

Delete one or more resource instances

TODO: create example

@param [Collection] collection

collection of records to be deleted

@return [Integer]

the number of records deleted

@api semipublic

# File lib/dm-core/repository.rb, line 194
def delete(collection)
  return 0 unless collection.query.valid?
  adapter.delete(collection)
end
identity_map(model) click to toggle source

Get the identity for a particular model within this repository.

If one doesn't yet exist, create a new default in-memory IdentityMap for the requested model.

TODO: create example

@param [Model] model

Model whose identity map should be returned

@return [IdentityMap]

The IdentityMap for model in this Repository

@api private

# File lib/dm-core/repository.rb, line 93
def identity_map(model)
  @identity_maps[model.base_model] ||= IdentityMap.new
end
inspect() click to toggle source

Return a human readable representation of the repository

TODO: create example

@return [String]

human readable representation of the repository

@api private

# File lib/dm-core/repository.rb, line 207
def inspect
  "#<#{self.class.name} @name=#{@name}>"
end
new_query(model, options = {}) click to toggle source

Create a Query or subclass instance for this repository.

@param [Model] model

the Model to retrieve results from

@param [Hash] options

the conditions and scope

@return [Query]

@api semipublic

# File lib/dm-core/repository.rb, line 130
def new_query(model, options = {})
  adapter.new_query(self, model, options)
end
read(query) click to toggle source

Retrieve a collection of results of a query

TODO: create example

@param [Query] query

composition of the query to perform

@return [Array]

result set of the query

@api semipublic

# File lib/dm-core/repository.rb, line 160
def read(query)
  return [] unless query.valid?
  query.model.load(adapter.read(query), query)
end
scope() { |self| ... } click to toggle source

Executes a block in the scope of this Repository

TODO: create example

@yieldparam [Repository] repository

yields self within the block

@yield

execute block in the scope of this Repository

@api private

# File lib/dm-core/repository.rb, line 108
def scope
  context = Repository.context

  context << self

  begin
    yield self
  ensure
    context.pop
  end
end
update(attributes, collection) click to toggle source

Update the attributes of one or more resource instances

TODO: create example

@param [Hash(Property => Object)] attributes

hash of attribute values to set, keyed by Property

@param [Collection] collection

collection of records to be updated

@return [Integer]

the number of records updated

@api semipublic

# File lib/dm-core/repository.rb, line 178
def update(attributes, collection)
  return 0 unless collection.query.valid? && attributes.any?
  adapter.update(attributes, collection)
end