class DBDiagram::Domain::Relationship::Cardinality
Constants
- N
Attributes
Returns a range that indicates the destination (right) cardinality.
Returns a range that indicates the source (left) cardinality.
Public Instance Methods
Returns an array with the cardinality classes for the source and destination of this cardinality. Possible return values are: [1, 1]
, [1, N]
, [N, N]
, and (in theory) [N, 1]
.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 88 def cardinality_class [source_cardinality_class, destination_cardinality_class] end
Returns true
if the destination (right side) is not mandatory.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 54 def destination_optional? destination_range.first < 1 end
Returns the inverse cardinality. Destination becomes source, source becomes destination.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 60 def inverse self.class.new destination_range, source_range end
Returns the name of this cardinality, based on its two cardinal numbers (for source and destination). Can be any of :one_to_one:
, :one_to_many
, or :many_to_many
. The name :many_to_one
also exists, but Rails ERD always normalises these kinds of relationships by inverting them, so they become :one_to_many
associations.
You can also call the equivalent method with a question mark, which will return true if the name corresponds to that method. For example:
cardinality.one_to_one? #=> true cardinality.one_to_many? #=> false
# File lib/db_diagram/domain/relationship/cardinality.rb, line 44 def name CLASSES[cardinality_class] end
Returns true
if the source (left side) is not mandatory.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 49 def source_optional? source_range.first < 1 end
Protected Instance Methods
The cardinality class of the destination (right side). Either 1
or Infinity
.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 100 def destination_cardinality_class destination_range.last == 1 ? 1 : N end
The cardinality class of the source (left side). Either 1
or Infinity
.
# File lib/db_diagram/domain/relationship/cardinality.rb, line 95 def source_cardinality_class source_range.last == 1 ? 1 : N end
Private Instance Methods
# File lib/db_diagram/domain/relationship/cardinality.rb, line 112 def compare_with(other, &block) yield(self) <=> yield(other) end
# File lib/db_diagram/domain/relationship/cardinality.rb, line 106 def compose_range(r) return r..r if r.kind_of?(Integer) && r > 0 return (r.begin)..(r.end - 1) if r.exclude_end? r end