class DBDiagram::Domain::Relationship::Cardinality

Constants

N

Attributes

destination_range[R]

Returns a range that indicates the destination (right) cardinality.

source_range[R]

Returns a range that indicates the source (left) cardinality.

Public Instance Methods

cardinality_class() click to toggle source

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
destination_optional?() click to toggle source

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
inverse() click to toggle source

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
name() click to toggle source

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
source_optional?() click to toggle source

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

destination_cardinality_class() click to toggle source

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
source_cardinality_class() click to toggle source

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

compare_with(other) { |self| ... } click to toggle source
# File lib/db_diagram/domain/relationship/cardinality.rb, line 112
def compare_with(other, &block)
  yield(self) <=> yield(other)
end
compose_range(r) click to toggle source
# 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