Class: DataMapper::Associations::ManyToMany::Collection

Inherits:
OneToMany::Collection show all
Defined in:
lib/dm-core/associations/many_to_many.rb

Overview

class Relationship

Instance Attribute Summary

Attributes inherited from OneToMany::Collection

#relationship, #source

Attributes inherited from Collection

#query

Attributes inherited from LazyArray

#head, #tail

Instance Method Summary collapse

Methods inherited from OneToMany::Collection

#clear, #reload, #replace, #update, #update!

Methods inherited from Collection

#<<, #[], #[]=, #all, #at, #clean?, #clear, #collect!, #concat, #create, #create!, #delete, #delete_at, #delete_if, #difference, #dirty?, #each, #first, #first_or_create, #first_or_new, #get, #get!, #hash, #insert, #inspect, #intersection, #last, #loaded_entries, #model, #model_key, #new, #pop, #properties, #push, #reject!, #relationships, #reload, #replace, #repository, #respond_to?, #reverse, #reverse!, #save, #save!, #set, #shift, #slice!, #union, #unshift, #update, #update!

Methods inherited from LazyArray

#<<, #==, #[], #[]=, #any?, #at, #clear, #concat, #delete_at, #delete_if, #empty?, #eql?, #fetch, #first, #freeze, #frozen?, #include?, #index, #insert, #kind_of?, #last, #lazy_possible?, #load_with, #loaded?, #pop, #push, #replace, #respond_to?, #reverse, #reverse!, #shift, #slice!, #to_a, #unshift, #values_at

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DataMapper::Collection

Instance Method Details

#destroyBoolean

Remove every Resource in the m:m Collection from the repository

This performs a deletion of each Resource in the Collection from the repository and clears the Collection.

Returns:

  • (Boolean)

    true if the resources were successfully destroyed



322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/dm-core/associations/many_to_many.rb', line 322

def destroy
  assert_source_saved 'The source must be saved before mass-deleting the collection'

  # make sure the records are loaded so they can be found when
  # the intermediaries are removed
  lazy_load

  unless intermediaries.all(via => self).destroy
    return false
  end

  super
end

#destroy!Boolean

Remove every Resource in the m:m Collection from the repository, bypassing validation

This performs a deletion of each Resource in the Collection from the repository and clears the Collection while skipping validation.

Returns:

  • (Boolean)

    true if the resources were successfully destroyed



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/dm-core/associations/many_to_many.rb', line 346

def destroy!
  assert_source_saved 'The source must be saved before mass-deleting the collection'

  model      = self.model
  key        = model.key(repository_name)
  conditions = Query.target_conditions(self, key, key)

  unless intermediaries.all(via => self).destroy!
    return false
  end

  unless model.all(:repository => repository, :conditions => conditions).destroy!
    return false
  end

  each do |resource|
    resource.persisted_state = Resource::State::Immutable.new(resource)
  end

  clear

  true
end

#intermediariesCollection

Return the intermediaries linking the source to the targets

Returns:



376
377
378
379
380
381
382
383
384
385
# File 'lib/dm-core/associations/many_to_many.rb', line 376

def intermediaries
  through = self.through
  source  = self.source

  @intermediaries ||= if through.loaded?(source)
    through.get_collection(source)
  else
    reset_intermediaries
  end
end

#intermediary_forHash (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map the resources in the collection to the intermediaries

Returns:

  • (Hash)

    the map of resources to their intermediaries



395
396
397
# File 'lib/dm-core/associations/many_to_many.rb', line 395

def intermediary_for
  @intermediary_for ||= {}
end

#throughObject (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



400
401
402
# File 'lib/dm-core/associations/many_to_many.rb', line 400

def through
  relationship.through
end

#viaObject (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



405
406
407
# File 'lib/dm-core/associations/many_to_many.rb', line 405

def via
  relationship.via
end