Class: DataMapper::Associations::OneToMany::Relationship

Inherits:
Relationship
  • Object
show all
Defined in:
lib/dm-core/associations/one_to_many.rb

Direct Known Subclasses

ManyToMany::Relationship

Constant Summary

Constants inherited from Relationship

Relationship::OPTIONS

Instance Attribute Summary

Attributes inherited from Relationship

#child_repository_name, #instance_variable_name, #max, #min, #name, #options, #parent_repository_name, #query, #reader_visibility, #writer_visibility

Instance Method Summary collapse

Methods inherited from Relationship

#==, #child_model, #child_model?, #child_model_name, #eager_load, #eql?, #field, #get!, #hash, #inverse, #loaded?, #parent_key, #parent_model, #parent_model?, #parent_model_name, #query_for, #relative_target_repository_name, #relative_target_repository_name_for, #set!, #source_scope, #valid?

Methods included from Subject

#default?

Methods included from DataMapper::Assertions

#assert_kind_of

Instance Method Details

#child_keyObject Also known as: target_key



21
22
23
# File 'lib/dm-core/associations/one_to_many.rb', line 21

def child_key
  inverse.child_key
end

#collection_for(source, other_query = nil) ⇒ Collection

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.

Returns a Collection for this relationship with a given source

Parameters:

  • source (Resource)

    A Resource to scope the collection with

  • other_query (Query) (defaults to: nil)

    (optional) A Query to further scope the collection with

Returns:

  • (Collection)

    The collection scoped to the relationship, source and query



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dm-core/associations/one_to_many.rb', line 39

def collection_for(source, other_query = nil)
  query = query_for(source, other_query)

  collection = collection_class.new(query)
  collection.relationship = self
  collection.source       = source

  # make the collection empty if the source is new
  collection.replace([]) if source.new?

  collection
end

#default_for(source) ⇒ Object



104
105
106
# File 'lib/dm-core/associations/one_to_many.rb', line 104

def default_for(source)
  collection_for(source).replace(Array(super))
end

#get(source, query = nil) ⇒ Object

Loads and returns association targets (ex.: articles) for given source resource (ex.: author)



56
57
58
59
60
# File 'lib/dm-core/associations/one_to_many.rb', line 56

def get(source, query = nil)
  lazy_load(source)
  collection = get_collection(source)
  query ? collection.all(query) : collection
end

#get_collection(source) ⇒ Object

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.



63
64
65
# File 'lib/dm-core/associations/one_to_many.rb', line 63

def get_collection(source)
  get!(source)
end

#lazy_load(source) ⇒ undefined

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.

Loads association targets and sets resulting value on given source resource

Parameters:

  • source (Resource)

    the source resource for the association

Returns:

  • (undefined)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dm-core/associations/one_to_many.rb', line 90

def lazy_load(source)
  return if loaded?(source)

  # SEL: load all related resources in the source collection
  if source.saved? && (collection = source.collection).size > 1
    eager_load(collection)
  end

  unless loaded?(source)
    set!(source, collection_for(source))
  end
end

#set(source, targets) ⇒ Object

Sets value of association targets (ex.: paragraphs) for given source resource (ex.: article)



71
72
73
74
# File 'lib/dm-core/associations/one_to_many.rb', line 71

def set(source, targets)
  lazy_load(source)
  get!(source).replace(targets)
end

#set_collection(source, target) ⇒ Object

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.



77
78
79
# File 'lib/dm-core/associations/one_to_many.rb', line 77

def set_collection(source, target)
  set!(source, target)
end