Class: RailsERD::Domain::Relationship::Cardinality

Inherits:
Object
  • Object
show all
Extended by:
Inspectable
Defined in:
lib/rails_erd/domain/relationship/cardinality.rb

Constant Summary collapse

N =

And beyond.

Infinity = 1.0/0
CLASSES =
{
  [1, 1] => :one_to_one,
  [1, N] => :one_to_many,
  [N, 1] => :many_to_one,
  [N, N] => :many_to_many
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

inspection_attributes

Constructor Details

#initialize(source_range, destination_range) ⇒ Cardinality

Create a new cardinality based on a source range and a destination range. These ranges describe which number of values are valid.



25
26
27
28
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 25

def initialize(source_range, destination_range) # @private :nodoc:
  @source_range = compose_range(source_range)
  @destination_range = compose_range(destination_range)
end

Instance Attribute Details

#destination_rangeObject (readonly)

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



21
22
23
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 21

def destination_range
  @destination_range
end

#source_rangeObject (readonly)

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



18
19
20
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 18

def source_range
  @source_range
end

Instance Method Details

#<=>(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 76

def <=>(other) # @private :nodoc:
  (cardinality_class <=> other.cardinality_class).nonzero? or
  compare_with(other) { |x| x.source_range.first + x.destination_range.first }.nonzero? or
  compare_with(other) { |x| x.source_range.last + x.destination_range.last }.nonzero? or
  compare_with(other) { |x| x.source_range.last }.nonzero? or
  compare_with(other) { |x| x.destination_range.last }
end

#==(other) ⇒ Object



72
73
74
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 72

def ==(other) # @private :nodoc:
  source_range == other.source_range and destination_range == other.destination_range
end

#cardinality_classObject

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].



88
89
90
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 88

def cardinality_class
  [source_cardinality_class, destination_cardinality_class]
end

#destination_optional?Boolean

Returns true if the destination (right side) is not mandatory.

Returns:

  • (Boolean)


54
55
56
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 54

def destination_optional?
  destination_range.first < 1
end

#inverseObject

Returns the inverse cardinality. Destination becomes source, source becomes destination.



60
61
62
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 60

def inverse
  self.class.new destination_range, source_range
end

#nameObject

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


44
45
46
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 44

def name
  CLASSES[cardinality_class]
end

#source_optional?Boolean

Returns true if the source (left side) is not mandatory.

Returns:

  • (Boolean)


49
50
51
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 49

def source_optional?
  source_range.first < 1
end