class DataMapper::Associations::OneToMany::Relationship
Public Class Methods
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 127 def initialize(name, target_model, source_model, options = {}) target_model ||= DataMapper::Inflector.camelize(DataMapper::Inflector.singularize(name.to_s)) options = { :min => 0, :max => source_model.n }.update(options) super end
Public Instance Methods
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 21 def child_key inverse.child_key end
Returns a Collection for this relationship with a given source
@param [Resource] source
A Resource to scope the collection with
@param [Query] other_query (optional)
A Query to further scope the collection with
@return [Collection]
The collection scoped to the relationship, source and query
@api private
# 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
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 120 def default_for(source) collection_for(source).replace(Array(super)) end
initialize the inverse “many to one” relationships explicitly before initializing other relationships. This makes sure that foreign key properties always appear in the order they were declared.
@api public
# File lib/dm-core/associations/one_to_many.rb, line 108 def finalize child_model.relationships.each do |relationship| # TODO: should this check #inverse? # relationship.child_key if inverse?(relationship) if relationship.kind_of?(Associations::ManyToOne::Relationship) relationship.finalize end end inverse.finalize end
Loads and returns association targets (ex.: articles) for given source resource (ex.: author)
@api semipublic
# 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
@api private
# File lib/dm-core/associations/one_to_many.rb, line 63 def get_collection(source) get!(source) end
Loads association targets and sets resulting value on given source resource
@param [Resource] source
the source resource for the association
@return [undefined]
@api private
# 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
Sets value of association targets (ex.: paragraphs) for given source resource (ex.: article)
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 71 def set(source, targets) lazy_load(source) get!(source).replace(targets) end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 77 def set_collection(source, target) set!(source, target) end
Private Instance Methods
@api private
# File lib/dm-core/associations/one_to_many.rb, line 172 def child_properties super || parent_key.map do |parent_property| "#{inverse_name}_#{parent_property.name}".to_sym end end
Returns collection class used by this type of relationship
@api private
# File lib/dm-core/associations/one_to_many.rb, line 153 def collection_class OneToMany::Collection end
Sets the association targets in the resource
@param [Resource] source
the source to set
@param [Array<Resource>] targets
the target collection for the association
@param [Query, Hash] query
the query to scope the association with
@return [undefined]
@api private
# File lib/dm-core/associations/one_to_many.rb, line 145 def eager_load_targets(source, targets, query) set!(source, collection_for(source, query).set(targets)) end
Returns the inverse relationship class
@api private
# File lib/dm-core/associations/one_to_many.rb, line 160 def inverse_class ManyToOne::Relationship end
Returns the inverse relationship name
@api private
# File lib/dm-core/associations/one_to_many.rb, line 167 def inverse_name super || DataMapper::Inflector.underscore(DataMapper::Inflector.demodulize(source_model.name)).to_sym end