Class: DataMapper::Repository

  • Object
    • DataMapper::Repository

Attributes

Instance Attributes

adapter [RW] public

Returns the value of attribute adapter.

identity_maps [RW] public

Returns the value of attribute identity_maps.

name [RW] public

Returns the value of attribute name.

type_map [RW] public

Returns the value of attribute type_map.

Constructor Summary

private initialize(name)
[View source]


204
205
206
207
208
209
210
211
# File 'dm-core/lib/dm-core/repository.rb', line 204

def initialize(name)
  raise ArgumentError, "+name+ should be a Symbol, but was #{name.class}", caller unless Symbol === name
  raise ArgumentError, "Unknown adapter name: #{name}"                            unless self.class.adapters.has_key?(name)

  @name          = name
  @adapter       = self.class.adapters[name]
  @identity_maps = Hash.new { |h,model| h[model] = IdentityMap.new }
end

Public Visibility

Public Class Method Summary

adapters

Returns:

context
default_name

Public Instance Method Summary

#all(model, options)

retrieve a collection of results of a query.

Returns:

#auto_migrate!
#auto_upgrade!
#avg(model, property, options)
#count(model, property, options)
#destroy(resource)

removes the resource from the data-store.

Returns:

#first(model, options)

retrieve a singular instance by query.

Returns:

#get(model, key)

retrieve a specific instance by key.

Returns:

#identity_map(model)
#identity_map_get(model, key)
#identity_map_set(resource)
#map(*args)
#max(model, property, options)
#migrate!
#min(model, property, options)
#save(resource)

save the instance into the data-store, updating if it already exists If the instance has dirty items in it’s associations, they also get saved.

Returns:

#storage_exists?(storage_name)

Returns:

#sum(model, property, options)
#to_s
#transaction

Produce a new Transaction for this Repository.

Returns:

#type_map

Returns the value of attribute type_map..

Public Instance Methods Inherited from Object

validatable?

Public Class Method Details

adapters

public adapters

Meta Tags

Returns:

<Adapter> the adapters registered for this repository

[View source]


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

def self.adapters


  @adapters
end

context

public context
[View source]


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

def self.context


  Thread.current[:dm_repository_contexts] ||= []
end

default_name

public default_name
[View source]


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

def self.default_name


  :default
end

Public Instance Method Details

all

public all(model, options)

retrieve a collection of results of a query

Meta Tags

Parameters:

<Class>

model the specific resource to retrieve from

<Hash,

Query> options composition of the query to perform

Returns:

<Collection> result set of the query

See Also:

DataMapper::Query
[View source]


81
82
83
84
85
86
87
88
89
90
91
92
# File 'dm-core/lib/dm-core/repository.rb', line 81

def all(model, options)
  query = if current_scope = model.send(:current_scope)



    current_scope.merge(options)
  else
    Query.new(self, model, options)
  end

  adapter.read_set(self, query)
end

auto_migrate!

public auto_migrate!
[View source]


160
161
162
163
164
# File 'dm-core/lib/dm-core/repository.rb', line 160

def auto_migrate!


  AutoMigrator.auto_migrate(name)
end

auto_upgrade!

public auto_upgrade!
[View source]


164
165
166
167
168
# File 'dm-core/lib/dm-core/repository.rb', line 164

def auto_upgrade!


  AutoMigrator.auto_upgrade(name)
end

avg

public avg(model, property, options)
[View source]


15
16
17
# File 'dm-more/dm-aggregates/lib/dm-aggregates/repository.rb', line 15

def avg(model, property, options)
  @adapter.avg(self, property, scoped_query(model, options))
end

count

public count(model, property, options)
[View source]


3
4
5
# File 'dm-more/dm-aggregates/lib/dm-aggregates/repository.rb', line 3

def count(model, property, options)
  @adapter.count(self, property, scoped_query(model, options))
end

destroy

public destroy(resource)

removes the resource from the data-store. The instance will remain in active-memory, but will now be marked as a new_record and it’s keys will be revoked

Meta Tags

Parameters:

<Class>

resource the resource to be destroyed

Returns:

<True, False> results of the destruction

[View source]


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'dm-core/lib/dm-core/repository.rb', line 142

def destroy(resource)
  if adapter.delete(self, resource)



    identity_maps[resource.class].delete(resource.key)
    resource.instance_variable_set(:@new_record, true)
    resource.dirty_attributes.clear
    resource.class.properties(name).each do |property|
      resource.dirty_attributes << property if resource.attribute_loaded?(property.name)
    end
    true
  else
    false
  end
end

first

public first(model, options)

retrieve a singular instance by query

Meta Tags

Parameters:

<Class>

model the specific resource to retrieve from

<Hash,

Query> options composition of the query to perform

Returns:

<Class> the first retrieved instance which matches the query

<NilClass> no object could be found which matches that query

See Also:

DataMapper::Query
[View source]


64
65
66
67
68
69
70
71
72
73
74
75
# File 'dm-core/lib/dm-core/repository.rb', line 64

def first(model, options)
  query = if current_scope = model.send(:current_scope)



    current_scope.merge(options.merge(:limit => 1))
  else
    Query.new(self, model, options.merge(:limit => 1))
  end

  adapter.read_set(self, query).first
end

get

public get(model, key)

retrieve a specific instance by key

Meta Tags

Parameters:

<Class>

model the specific resource to retrieve from

<Key>

key The keys to look for

Returns:

<Class> the instance of the Resource retrieved

<NilClass> could not find the instance requested

[View source]


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'dm-core/lib/dm-core/repository.rb', line 41

def get(model, key)
  identity_maps[model][key] || begin
    conditions = Hash[ *model.key(name).zip(key).flatten ]
    conditions.update(:limit => 1)

    query = if current_scope = model.send(:current_scope)



      current_scope.merge(conditions)
    else
      Query.new(self, model, conditions)
    end

    adapter.read_set(self, query).first
  end
end

identity_map

public identity_map(model)
[View source]


30
31
32
# File 'dm-core/lib/dm-core/repository.rb', line 30

def identity_map(model)
  @identity_maps[model]
end

identity_map_get

public identity_map_get(model, key)
[View source]


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

def identity_map_get(model, key)
  identity_map(model)[key]
end

identity_map_set

public identity_map_set(resource)
[View source]


26
27
28
# File 'dm-core/lib/dm-core/repository.rb', line 26

def identity_map_set(resource)
  identity_map(resource.class)[resource.key] = resource
end

map

public map(*args)
[View source]


182
183
184
# File 'dm-core/lib/dm-core/repository.rb', line 182

def map(*args)
  type_map.map(*args)
end

max

public max(model, property, options)
[View source]


11
12
13
# File 'dm-more/dm-aggregates/lib/dm-aggregates/repository.rb', line 11

def max(model, property, options)
  @adapter.max(self, property, scoped_query(model, options))
end

migrate!

public migrate!
[View source]


156
157
158
159
160
# File 'dm-core/lib/dm-core/repository.rb', line 156

def migrate!


  Migrator.migrate(name)
end

min

public min(model, property, options)
[View source]


7
8
9
# File 'dm-more/dm-aggregates/lib/dm-aggregates/repository.rb', line 7

def min(model, property, options)
  @adapter.min(self, property, scoped_query(model, options))
end

save

public save(resource)

save the instance into the data-store, updating if it already exists If the instance has dirty items in it’s associations, they also get saved

Meta Tags

Parameters:

<Class>

resource the resource to return to the data-store

Returns:

<True, False> results of the save

[View source]


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
149
150
# File 'dm-core/lib/dm-core/repository.rb', line 97

def save(resource)
  resource.child_associations.each { |a| a.save }

  model = resource.class

  # set defaults for new resource
  if resource.new_record?



    model.properties(name).each do |property|
      next if resource.attribute_loaded?(property.name)
      property.set(resource, property.default_for(resource))
    end
  end

  success = false

  # save the resource if is dirty, or is a new record with a serial key
  if resource.dirty? || (resource.new_record? && model.key.any? { |p| p.serial? })



    if resource.new_record?



      if adapter.create(self, resource)



        identity_map_set(resource)
        resource.instance_variable_set(:@new_record, false)
        resource.dirty_attributes.clear
        query = DataMapper::Query.new(self, model, Hash[*model.properties(name).key.zip(resource.key).flatten])
        resource.collection = DataMapper::Collection.new(query)
        resource.collection << resource
        success = true
      end
    else
      if adapter.update(self, resource)



        resource.dirty_attributes.clear
        success = true
      end
    end
  end

  resource.parent_associations.each { |a| a.save }

  success
end

storage_exists?

public storage_exists?(storage_name)

Meta Tags

Returns:

<True, False> whether or not the data-store exists for this repo

[View source]


193
194
195
# File 'dm-core/lib/dm-core/repository.rb', line 193

def storage_exists?(storage_name)
  adapter.storage_exists?(storage_name)
end

sum

public sum(model, property, options)
[View source]


19
20
21
# File 'dm-more/dm-aggregates/lib/dm-aggregates/repository.rb', line 19

def sum(model, property, options)
  @adapter.sum(self, property, scoped_query(model, options))
end

to_s

public to_s
[View source]


178
179
180
181
182
# File 'dm-core/lib/dm-core/repository.rb', line 178

def to_s


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

transaction

public transaction

Produce a new Transaction for this Repository

Meta Tags

Returns:

<DataMapper::Adapters::Transaction> a new Transaction (in state :none) that can be used to execute code #with_transaction

[View source]


174
175
176
177
178
# File 'dm-core/lib/dm-core/repository.rb', line 174

def transaction


  DataMapper::Transaction.new(self)
end

type_map

public type_map

Returns the value of attribute type_map

[View source]


186
187
188
189
190
# File 'dm-core/lib/dm-core/repository.rb', line 186

def type_map


  @type_map ||= TypeMap.new(adapter.class.type_map)
end