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


331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/dm-core/associations/many_to_many.rb', line 331

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


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/dm-core/associations/many_to_many.rb', line 355

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.persistence_state = Resource::PersistenceState::Immutable.new(resource)
  end

  clear

  true
end

#intermediariesCollection

Return the intermediaries linking the source to the targets

Returns:


385
386
387
388
389
390
391
392
393
394
# File 'lib/dm-core/associations/many_to_many.rb', line 385

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


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

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.


409
410
411
# File 'lib/dm-core/associations/many_to_many.rb', line 409

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.


414
415
416
# File 'lib/dm-core/associations/many_to_many.rb', line 414

def via
  relationship.via
end