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

Instance Method Summary collapse

Methods inherited from Relationship

#==, #child_model, #child_model?, #child_model_name, #eager_load, #eql?, #get!, #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?

Instance Method Details

#child_keyObject Also known as: target_key

TODO: document



27
28
29
# File 'lib/dm-core/associations/one_to_many.rb', line 27

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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dm-core/associations/one_to_many.rb', line 46

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 not saved
  collection.replace([]) unless source.saved?

  collection
end

#get(source, other_query = nil) ⇒ Object

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



63
64
65
66
67
68
69
70
# File 'lib/dm-core/associations/one_to_many.rb', line 63

def get(source, other_query = nil)
  assert_kind_of 'source', source, source_model

  lazy_load(source) unless loaded?(source)

  collection = get!(source)
  other_query.nil? ? collection : collection.all(other_query)
end

#inherited_by(model) ⇒ 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.

TODO: document



87
88
89
90
# File 'lib/dm-core/associations/one_to_many.rb', line 87

def inherited_by(model)
  model.relationships(source_repository_name)[name] ||
    self.class.new(name, child_model_name, model, options_with_inverse)
end

#set(source, targets) ⇒ Object

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



76
77
78
79
80
81
82
83
# File 'lib/dm-core/associations/one_to_many.rb', line 76

def set(source, targets)
  assert_kind_of 'source',  source,  source_model
  assert_kind_of 'targets', targets, Array

  lazy_load(source) unless loaded?(source)

  get!(source).replace(targets)
end