Class: DBDiagram::Domain::Relationship
- Inherits:
-
Object
- Object
- DBDiagram::Domain::Relationship
- Extended by:
- Inspectable
- Defined in:
- lib/db_diagram/domain/relationship.rb,
lib/db_diagram/domain/relationship/cardinality.rb
Overview
Describes a relationship between two entities. A relationship is detected based on Active Record associations. One relationship may represent more than one association, however. Related associations are grouped together. Associations are related if they share the same foreign key, or the same join table in the case of many-to-many associations.
Defined Under Namespace
Classes: Cardinality
Constant Summary collapse
- N =
Cardinality::N
Instance Attribute Summary collapse
-
#destination ⇒ Object
readonly
The destination entity.
-
#domain ⇒ Object
readonly
The domain in which this relationship is defined.
-
#source ⇒ Object
readonly
The source entity.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#associations ⇒ Object
Returns all Active Record association objects that describe this relationship.
-
#cardinality ⇒ Object
Returns the cardinality of this relationship.
-
#initialize(domain, associations) ⇒ Relationship
constructor
A new instance of Relationship.
-
#many_to? ⇒ Boolean
Indicates whether the source cardinality class of this relationship is equal to infinity.
-
#mutual? ⇒ Boolean
Indicates whether or not the relationship is defined by two inverse associations (e.g. a
has_manyand a correspondingbelongs_toassociation). -
#one_to? ⇒ Boolean
Indicates whether the source cardinality class of this relationship is equal to one.
-
#recursive? ⇒ Boolean
Indicates whether or not this relationship connects an entity with itself.
-
#strength ⇒ Object
The strength of a relationship is equal to the number of associations that describe it.
-
#to_many? ⇒ Boolean
Indicates whether the destination cardinality class of this relationship is equal to infinity.
-
#to_one? ⇒ Boolean
Indicates whether the destination cardinality class of this relationship is equal to one.
Methods included from Inspectable
Constructor Details
#initialize(domain, associations) ⇒ Relationship
Returns a new instance of Relationship.
63 64 65 66 67 68 69 70 71 |
# File 'lib/db_diagram/domain/relationship.rb', line 63 def initialize(domain, associations) # @private :nodoc: @domain = domain @reverse_associations, @forward_associations = partition_associations(associations) assoc = @forward_associations.first || @reverse_associations.first @source = @domain.entity_by_name(self.class.send(:association_owner, assoc)) @destination = @domain.entity_by_name(self.class.send(:association_target, assoc)) @source, @destination = @destination, @source if assoc.belongs_to? end |
Instance Attribute Details
#destination ⇒ Object (readonly)
The destination entity. It corresponds to the model that has defined a belongs_to association with the other model.
58 59 60 |
# File 'lib/db_diagram/domain/relationship.rb', line 58 def destination @destination end |
#domain ⇒ Object (readonly)
The domain in which this relationship is defined.
50 51 52 |
# File 'lib/db_diagram/domain/relationship.rb', line 50 def domain @domain end |
#source ⇒ Object (readonly)
The source entity. It corresponds to the model that has defined a has_one or has_many association with the other model.
54 55 56 |
# File 'lib/db_diagram/domain/relationship.rb', line 54 def source @source end |
Class Method Details
.from_associations(domain, associations) ⇒ Object
16 17 18 19 |
# File 'lib/db_diagram/domain/relationship.rb', line 16 def from_associations(domain, associations) # @private :nodoc: assoc_groups = associations.group_by { |assoc| association_identity(assoc) } assoc_groups.collect { |_, assoc_group| new(domain, assoc_group.to_a) } end |
Instance Method Details
#<=>(other) ⇒ Object
133 134 135 |
# File 'lib/db_diagram/domain/relationship.rb', line 133 def <=>(other) # @private :nodoc: (source.name <=> other.source.name).nonzero? or (destination.name <=> other.destination.name) end |
#associations ⇒ Object
Returns all Active Record association objects that describe this relationship.
75 76 77 |
# File 'lib/db_diagram/domain/relationship.rb', line 75 def associations @forward_associations + @reverse_associations end |
#cardinality ⇒ Object
Returns the cardinality of this relationship.
80 81 82 83 84 85 86 87 |
# File 'lib/db_diagram/domain/relationship.rb', line 80 def cardinality @cardinality ||= begin reverse_max = any_habtm?(associations) ? N : 1 forward_range = associations_range(@forward_associations, N) reverse_range = associations_range(@reverse_associations, reverse_max) Cardinality.new(reverse_range, forward_range) end end |
#many_to? ⇒ Boolean
Indicates whether the source cardinality class of this relationship is equal to infinity. This is true for many-to-many relationships only.
123 124 125 |
# File 'lib/db_diagram/domain/relationship.rb', line 123 def many_to? cardinality.cardinality_class[0] != 1 end |
#mutual? ⇒ Boolean
Indicates whether or not the relationship is defined by two inverse associations (e.g. a has_many and a corresponding belongs_to association).
92 93 94 |
# File 'lib/db_diagram/domain/relationship.rb', line 92 def mutual? @forward_associations.any? and @reverse_associations.any? end |
#one_to? ⇒ Boolean
Indicates whether the source cardinality class of this relationship is equal to one. This is true for one-to-one or one-to-many relationships only.
117 118 119 |
# File 'lib/db_diagram/domain/relationship.rb', line 117 def one_to? cardinality.cardinality_class[0] == 1 end |
#recursive? ⇒ Boolean
Indicates whether or not this relationship connects an entity with itself.
97 98 99 |
# File 'lib/db_diagram/domain/relationship.rb', line 97 def recursive? @source == @destination end |
#strength ⇒ Object
The strength of a relationship is equal to the number of associations that describe it.
129 130 131 |
# File 'lib/db_diagram/domain/relationship.rb', line 129 def strength if source.generalized? then 1 else associations.size end end |
#to_many? ⇒ Boolean
Indicates whether the destination cardinality class of this relationship is equal to infinity. This is true for one-to-many or many-to-many relationships only.
110 111 112 |
# File 'lib/db_diagram/domain/relationship.rb', line 110 def to_many? cardinality.cardinality_class[1] != 1 end |
#to_one? ⇒ Boolean
Indicates whether the destination cardinality class of this relationship is equal to one. This is true for one-to-one relationships only.
103 104 105 |
# File 'lib/db_diagram/domain/relationship.rb', line 103 def to_one? cardinality.cardinality_class[1] == 1 end |