Module: DataMapper::Associations
- Includes:
- Assertions
- Defined in:
- lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb,
lib/gems/dm-serializer-0.9.9/lib/dm-serializer/to_json.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_one.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/many_to_one.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/many_to_many.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/relationship.rb,
lib/gems/dm-core-0.9.9/lib/dm-core/associations/relationship_chain.rb
Defined Under Namespace
Modules: ManyToMany, ManyToOne, OneToMany, OneToOne Classes: ImmutableAssociationError, Relationship, RelationshipChain, UnsavedParentError
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ DataMapper::Association::ManyToOne
A shorthand, clear syntax for defining many-to-one resource relationships.
-
#has(cardinality, name, options = {}) ⇒ DataMapper::Association::Relationship
A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.
-
#many_to_one_relationships ⇒ Object
private
Returns all relationships that are many-to-one for this model.
- #n ⇒ Object
- #relationships(repository_name = default_repository_name) ⇒ Object
Methods included from Assertions
Instance Method Details
#belongs_to(name, options = {}) ⇒ DataMapper::Association::ManyToOne
A shorthand, clear syntax for defining many-to-one resource relationships.
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb', line 147 def belongs_to(name, ={}) @_valid_relations = false relationship = ManyToOne.setup(name, self, ) # Please leave this in - I will release contextual serialization soon # which requires this -- guyvdb # TODO convert this to a hook in the plugin once hooks work on class # methods self.init_belongs_relationship_for_serialization(relationship) if self.respond_to?(:init_belongs_relationship_for_serialization) relationship end |
#has(cardinality, name, options = {}) ⇒ DataMapper::Association::Relationship
A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb', line 94 def has(cardinality, name, = {}) # NOTE: the reason for this fix is that with the ability to pass in two # hashes into has() there might be instances where people attempt to # pass in the options into the name part and not know why things aren't # working for them. if name.kind_of?(Hash) name_through, through = name.keys.first, name.values.first cardinality_string = cardinality.to_s == 'Infinity' ? 'n' : cardinality.inspect warn("In #{self.name} 'has #{cardinality_string}, #{name_through.inspect} => #{through.inspect}' is deprecated. Use 'has #{cardinality_string}, #{name_through.inspect}, :through => #{through.inspect}' instead") end = .merge(extract_min_max(cardinality)) = .merge(extract_throughness(name)) # do not remove this. There is alot of confusion on people's # part about what the first argument to has() is. For the record it # is the min cardinality and max cardinality of the association. # simply put, it constraints the number of resources that will be # returned by the association. It is not, as has been assumed, # the number of results on the left and right hand side of the # reltionship. if [:min] == n && [:max] == n raise ArgumentError, 'Cardinality may not be n..n. The cardinality specifies the min/max number of results from the association', caller end klass = [:max] == 1 ? OneToOne : OneToMany klass = ManyToMany if [:through] == DataMapper::Resource relationship = klass.setup(.delete(:name), self, ) # Please leave this in - I will release contextual serialization soon # which requires this -- guyvdb # TODO convert this to a hook in the plugin once hooks work on class # methods self.init_has_relationship_for_serialization(relationship) if self.respond_to?(:init_has_relationship_for_serialization) relationship end |
#many_to_one_relationships ⇒ 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.
Returns all relationships that are many-to-one for this model.
Used to find the relationships that require properties in any Repository.
Example: class Plur
include DataMapper::Resource
def self.default_repository_name
:plur_db
end
repository(:plupp_db) do
has 1, :plupp
end
end
This resource has a many-to-one to the Plupp resource residing in the :plupp_db repository, but the Plur resource needs the plupp_id property no matter what repository itself lives in, ie we need to create that property when we migrate etc.
Used in DataMapper::Model.properties_with_subclasses
42 43 44 45 |
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb', line 42 def many_to_one_relationships relationships unless @relationships # needs to be initialized! @relationships.values.collect do |rels| rels.values end.flatten.select do |relationship| relationship.child_model == self end end |
#n ⇒ Object
52 53 54 |
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb', line 52 def n 1.0/0 end |
#relationships(repository_name = default_repository_name) ⇒ Object
47 48 49 50 |
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations.rb', line 47 def relationships(repository_name = default_repository_name) @relationships ||= {} @relationships[repository_name] ||= repository_name == Repository.default_name ? {} : relationships(Repository.default_name).dup end |