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.



27
28
29
30
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 27

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.



23
24
25
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 23

def destination_range
  @destination_range
end

#source_rangeObject (readonly)

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



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

def source_range
  @source_range
end

Instance Method Details

#<=>(other) ⇒ Object



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

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



74
75
76
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 74

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



90
91
92
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 90

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)


56
57
58
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 56

def destination_optional?
  destination_range.first < 1
end

#inverseObject

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



62
63
64
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 62

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


46
47
48
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 46

def name
  CLASSES[cardinality_class]
end

#source_optional?Boolean

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

Returns:

  • (Boolean)


51
52
53
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 51

def source_optional?
  source_range.first < 1
end