Associations::Relationship
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 127 127: def initialize(name, target_model, source_model, options = {}) 128: target_model ||= DataMapper::Inflector.camelize(DataMapper::Inflector.singularize(name.to_s)) 129: options = { :min => 0, :max => source_model.n }.update(options) 130: super 131: end
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 21 21: def child_key 22: inverse.child_key 23: 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 39: def collection_for(source, other_query = nil) 40: query = query_for(source, other_query) 41: 42: collection = collection_class.new(query) 43: collection.relationship = self 44: collection.source = source 45: 46: # make the collection empty if the source is new 47: collection.replace([]) if source.new? 48: 49: collection 50: end
@api semipublic
# File lib/dm-core/associations/one_to_many.rb, line 120 120: def default_for(source) 121: collection_for(source).replace(Array(super)) 122: 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 108: def finalize 109: child_model.relationships.each do |relationship| 110: # TODO: should this check #inverse? 111: # relationship.child_key if inverse?(relationship) 112: if relationship.kind_of?(Associations::ManyToOne::Relationship) 113: relationship.finalize 114: end 115: end 116: inverse.finalize 117: 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 56: def get(source, query = nil) 57: lazy_load(source) 58: collection = get_collection(source) 59: query ? collection.all(query) : collection 60: end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 63 63: def get_collection(source) 64: get!(source) 65: 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 90: def lazy_load(source) 91: return if loaded?(source) 92: 93: # SEL: load all related resources in the source collection 94: if source.saved? && (collection = source.collection).size > 1 95: eager_load(collection) 96: end 97: 98: unless loaded?(source) 99: set!(source, collection_for(source)) 100: end 101: 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 71: def set(source, targets) 72: lazy_load(source) 73: get!(source).replace(targets) 74: end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 172 172: def child_properties 173: super || parent_key.map do |parent_property| 174: "#{inverse_name}_#{parent_property.name}".to_sym 175: end 176: end
Returns collection class used by this type of relationship
@api private
# File lib/dm-core/associations/one_to_many.rb, line 153 153: def collection_class 154: OneToMany::Collection 155: end
Sets the association targets in the resource
@param [Resource] source
the source to set
@param [Array
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 145: def eager_load_targets(source, targets, query) 146: set!(source, collection_for(source, query).set(targets)) 147: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.