Class: DataMapper::Repository

Inherits:
Object
  • Object
show all
Includes:
Assertions, Migration, Transaction
Defined in:
lib/dm-core/repository.rb

Defined Under Namespace

Modules: Migration, Transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transaction

#transaction

Methods included from Migration

#auto_migrate!, #auto_upgrade!, #map, #migrate!, #storage_exists?, #type_map

Methods included from Assertions

#assert_kind_of

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/dm-core/repository.rb', line 22

def name
  @name
end

Class Method Details

.adaptersAdapter

Returns the adapters registered for this repository.

Returns:

  • (Adapter)

    the adapters registered for this repository



10
11
12
# File 'lib/dm-core/repository.rb', line 10

def self.adapters
  @adapters
end

.contextObject



14
15
16
# File 'lib/dm-core/repository.rb', line 14

def self.context
  Thread.current[:dm_repository_contexts] ||= []
end

.default_nameObject



18
19
20
# File 'lib/dm-core/repository.rb', line 18

def self.default_name
  :default
end

Instance Method Details

#adapterObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/dm-core/repository.rb', line 24

def adapter
  # Make adapter instantiation lazy so we can defer repository setup until it's actually
  # needed. Do not remove this code.
  @adapter ||= begin
    raise ArgumentError, "Adapter not set: #{@name}. Did you forget to setup?" \
      unless self.class.adapters.has_key?(@name)

    self.class.adapters[@name]
  end
end

#create(resources) ⇒ Object



50
51
52
# File 'lib/dm-core/repository.rb', line 50

def create(resources)
  adapter.create(resources)
end

#delete(query) ⇒ Object



79
80
81
# File 'lib/dm-core/repository.rb', line 79

def delete(query)
  adapter.delete(query)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/dm-core/repository.rb', line 83

def eql?(other)
  return true if super
  name == other.name
end

#identity_map(model) ⇒ Object



35
36
37
# File 'lib/dm-core/repository.rb', line 35

def identity_map(model)
  @identity_maps[model]
end

#read_many(query) ⇒ DataMapper::Collection

retrieve a collection of results of a query

Parameters:

  • query (Query)

    composition of the query to perform

Returns:

See Also:



60
61
62
# File 'lib/dm-core/repository.rb', line 60

def read_many(query)
  adapter.read_many(query)
end

#read_one(query) ⇒ DataMapper::Resource, NilClass

retrieve a resource instance by a query

Parameters:

  • query (Query)

    composition of the query to perform

Returns:

  • (DataMapper::Resource)

    the first retrieved instance which matches the query

  • (NilClass)

    no object could be found which matches that query

See Also:



71
72
73
# File 'lib/dm-core/repository.rb', line 71

def read_one(query)
  adapter.read_one(query)
end

#scope(&block) ⇒ Object

TODO: spec this



40
41
42
43
44
45
46
47
48
# File 'lib/dm-core/repository.rb', line 40

def scope(&block)
  Repository.context << self

  begin
    return yield(self)
  ensure
    Repository.context.pop
  end
end

#to_sObject



90
91
92
# File 'lib/dm-core/repository.rb', line 90

def to_s
  "#<DataMapper::Repository:#{@name}>"
end

#update(attributes, query) ⇒ Object



75
76
77
# File 'lib/dm-core/repository.rb', line 75

def update(attributes, query)
  adapter.update(attributes, query)
end