Module: DataMapper::Model::Relationship
- Includes:
- Assertions
- Defined in:
- lib/dm-core/model/relationship.rb
Class Method Summary collapse
-
.extended(model) ⇒ Object
private
Initializes relationships hash for extended model class.
Instance Method Summary collapse
-
#belongs_to(name, *args) ⇒ Association::Relationship
A shorthand, clear syntax for defining many-to-one resource relationships.
-
#has(cardinality, name, *args) ⇒ Association::Relationship
A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.
-
#inherited(model) ⇒ Object
private
When DataMapper model is inherited, relationships of parent are duplicated and copied to subclass model.
-
#n ⇒ Object
Used to express unlimited cardinality of association, see
has
. -
#relationships(repository_name = default_repository_name) ⇒ RelationshipSet
Returns copy of relationships set in given repository.
Methods included from Assertions
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Class Method Details
.extended(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.
Initializes relationships hash for extended model class.
When model calls has n, has 1 or belongs_to, relationships are stored in that hash: keys are repository names and values are relationship sets.
19 20 21 |
# File 'lib/dm-core/model/relationship.rb', line 19 def self.extended(model) model.instance_variable_set(:@relationships, {}) end |
Instance Method Details
#belongs_to(name, *args) ⇒ Association::Relationship
A shorthand, clear syntax for defining many-to-one resource relationships.
* belongs_to :user # many to one user
* belongs_to :friend, :model => 'User' # many to one friend
* belongs_to :reference, :repository => :pubmed # association for repository other than default
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/dm-core/model/relationship.rb', line 156 def belongs_to(name, *args) name = name.to_sym model_name = self.name model = extract_model(args) = (args) if .key?(:through) raise "#{model_name}#belongs_to with :through is deprecated, use 'has 1, :#{name}, #{.inspect}' in #{model_name} instead (#{caller.first})" elsif .key?(:model) && model raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument' end () model ||= .delete(:model) repository_name = repository.name # TODO: change to source_repository_name and target_respository_name [:child_repository_name] = repository_name [:parent_repository_name] = .delete(:repository) relationship = Associations::ManyToOne::Relationship.new(name, self, model, ) relationships(repository_name) << relationship descendants.each do |descendant| descendant.relationships(repository_name) << relationship end create_relationship_reader(relationship) create_relationship_writer(relationship) relationship end |
#has(cardinality, name, *args) ⇒ Association::Relationship
A shorthand, clear syntax for defining one-to-one, one-to-many and many-to-many resource relationships.
* has 1, :friend # one friend
* has n, :friends # many friends
* has 1..3, :friends # many friends (at least 1, at most 3)
* has 3, :friends # many friends (exactly 3)
* has 1, :friend, 'User' # one friend with the class User
* has 3, :friends, :through => :friendships # many friends through the friendships relationship
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 132 133 134 135 136 |
# File 'lib/dm-core/model/relationship.rb', line 96 def has(cardinality, name, *args) name = name.to_sym model = extract_model(args) = (args) min, max = extract_min_max(cardinality) .update(:min => min, :max => max) () if .key?(:model) && model raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument' end model ||= .delete(:model) repository_name = repository.name # TODO: change to :target_respository_name and :source_repository_name [:child_repository_name] = .delete(:repository) [:parent_repository_name] = repository_name klass = if max > 1 .key?(:through) ? Associations::ManyToMany::Relationship : Associations::OneToMany::Relationship else Associations::OneToOne::Relationship end relationship = klass.new(name, model, self, ) relationships(repository_name) << relationship descendants.each do |descendant| descendant.relationships(repository_name) << relationship end create_relationship_reader(relationship) create_relationship_writer(relationship) relationship end |
#inherited(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.
When DataMapper model is inherited, relationships of parent are duplicated and copied to subclass model
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dm-core/model/relationship.rb', line 27 def inherited(model) model.instance_variable_set(:@relationships, {}) @relationships.each do |repository_name, relationships| model_relationships = model.relationships(repository_name) relationships.each { |relationship| model_relationships << relationship } end super end |
#n ⇒ Object
Used to express unlimited cardinality of association, see has
63 64 65 |
# File 'lib/dm-core/model/relationship.rb', line 63 def n Infinity end |
#relationships(repository_name = default_repository_name) ⇒ RelationshipSet
Returns copy of relationships set in given repository.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dm-core/model/relationship.rb', line 45 def relationships(repository_name = default_repository_name) # TODO: create RelationshipSet#copy that will copy the relationships, but assign the # new Relationship objects to a supplied repository and model. dup does not really # do what is needed default_repository_name = self.default_repository_name @relationships[repository_name] ||= if repository_name == default_repository_name RelationshipSet.new else relationships(default_repository_name).dup end end |