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
trueif 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
trueif 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.
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_range ⇒ Object (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_range ⇒ Object (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_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].
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.
56 57 58 |
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 56 def destination_optional? destination_range.first < 1 end |
#inverse ⇒ Object
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 |
#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
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.
51 52 53 |
# File 'lib/rails_erd/domain/relationship/cardinality.rb', line 51 def source_optional? source_range.first < 1 end |