Class: RailsERD::Domain::Relationship::Cardinality
- Inherits:
-
Object
- Object
- RailsERD::Domain::Relationship::Cardinality
- 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
-
#destination_range ⇒ Object
readonly
Returns a range that indicates the destination (right) cardinality.
-
#source_range ⇒ Object
readonly
Returns a range that indicates the source (left) cardinality.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#cardinality_class ⇒ Object
Returns an array with the cardinality classes for the source and destination of this cardinality.
-
#destination_optional? ⇒ Boolean
Returns
true
if the destination (right side) is not mandatory. -
#initialize(source_range, destination_range) ⇒ Cardinality
constructor
Create a new cardinality based on a source range and a destination range.
-
#inverse ⇒ Object
Returns the inverse cardinality.
-
#name ⇒ Object
Returns the name of this cardinality, based on its two cardinal numbers (for source and destination).
-
#source_optional? ⇒ Boolean
Returns
true
if the source (left side) is not mandatory.
Methods included from Inspectable
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_range ⇒ Object (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_range ⇒ Object (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_class ⇒ Object
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.
54 55 56 |
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 54 def destination_optional? destination_range.first < 1 end |
#inverse ⇒ Object
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 |
#name ⇒ Object
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.
49 50 51 |
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 49 def source_optional? source_range.first < 1 end |