Class: Dbwatcher::Services::DiagramData::Relationship
- Defined in:
- lib/dbwatcher/services/diagram_data/relationship.rb
Overview
Standard relationship between entities
This class provides a standardized representation for all diagram relationships (edges, connections, associations, foreign keys, etc.) with consistent validation.
Constant Summary collapse
- VALID_CARDINALITIES =
Valid cardinality types
[ "one_to_one", "one_to_many", "many_to_one", "many_to_many", nil ].freeze
- CARDINALITY_MAPPING =
Cardinality mapping for relationship types
{ "has_many" => "one_to_many", "belongs_to" => "many_to_one", "has_one" => "one_to_one", "has_and_belongs_to_many" => "many_to_many" }.freeze
- ERD_NOTATIONS =
ERD cardinality notations
{ "one_to_many" => "||--o{", "many_to_one" => "}o--||", "one_to_one" => "||--||", "many_to_many" => "}|--|{" }.freeze
- DEFAULT_ERD_NOTATION =
Default ERD notation
"||--o{"
Instance Attribute Summary collapse
-
#cardinality ⇒ Object
Returns the value of attribute cardinality.
-
#label ⇒ Object
Returns the value of attribute label.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#source_id ⇒ Object
Returns the value of attribute source_id.
-
#target_id ⇒ Object
Returns the value of attribute target_id.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.extract_constructor_args(hash) ⇒ Object
Override base class method to handle simple hash initialization.
Instance Method Summary collapse
-
#comparable_attributes ⇒ Object
Implementation for Base class.
-
#erd_cardinality_notation ⇒ String
Get cardinality for ERD notation.
-
#infer_cardinality ⇒ String?
Infer cardinality from relationship type if not explicitly set.
-
#initialize(params) ⇒ Relationship
constructor
Initialize relationship.
-
#serializable_attributes ⇒ Object
Implementation for Base class.
-
#valid? ⇒ Boolean
Check if relationship is valid.
-
#validation_errors ⇒ Array<String>
Get validation errors.
Methods inherited from Base
#==, from_h, from_json, #hash, #inspect, #to_h, #to_json, #to_s
Constructor Details
#initialize(params) ⇒ Relationship
Initialize relationship
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 60 def initialize(params) super() # Initialize parent class params = RelationshipParams.new(params) if params.is_a?(Hash) @source_id = params.source_id.to_s @target_id = params.target_id.to_s @type = params.type.to_s @label = params.label&.to_s @cardinality = params.cardinality&.to_s @metadata = params..is_a?(Hash) ? params. : {} end |
Instance Attribute Details
#cardinality ⇒ Object
Returns the value of attribute cardinality.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def cardinality @cardinality end |
#label ⇒ Object
Returns the value of attribute label.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def label @label end |
#metadata ⇒ Object
Returns the value of attribute metadata.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def @metadata end |
#source_id ⇒ Object
Returns the value of attribute source_id.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def source_id @source_id end |
#target_id ⇒ Object
Returns the value of attribute target_id.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def target_id @target_id end |
#type ⇒ Object
Returns the value of attribute type.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26 def type @type end |
Class Method Details
.extract_constructor_args(hash) ⇒ Object
Override base class method to handle simple hash initialization
114 115 116 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 114 def self.extract_constructor_args(hash) hash end |
Instance Method Details
#comparable_attributes ⇒ Object
Implementation for Base class
119 120 121 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 119 def comparable_attributes [source_id, target_id, type, label, cardinality, ] end |
#erd_cardinality_notation ⇒ String
Get cardinality for ERD notation
108 109 110 111 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 108 def erd_cardinality_notation # Default to one-to-many if not recognized ERD_NOTATIONS[infer_cardinality] || DEFAULT_ERD_NOTATION end |
#infer_cardinality ⇒ String?
Infer cardinality from relationship type if not explicitly set
99 100 101 102 103 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 99 def infer_cardinality return cardinality if cardinality CARDINALITY_MAPPING[type] end |
#serializable_attributes ⇒ Object
Implementation for Base class
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 124 def serializable_attributes { source_id: source_id, target_id: target_id, type: type, label: label, cardinality: cardinality, metadata: } end |
#valid? ⇒ Boolean
Check if relationship is valid
75 76 77 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 75 def valid? validation_errors.empty? end |
#validation_errors ⇒ Array<String>
Get validation errors
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 82 def validation_errors errors = [] errors << "Source ID cannot be blank" if source_id.nil? || source_id.to_s.strip.empty? errors << "Target ID cannot be blank" if target_id.nil? || target_id.to_s.strip.empty? errors << "Type cannot be blank" if type.nil? || type.to_s.strip.empty? # Allow self-referential relationships when explicitly marked as such errors << "Source and target cannot be the same" if ![:self_referential] && (source_id == target_id) errors << "Invalid cardinality: #{cardinality}" if cardinality && !VALID_CARDINALITIES.include?(cardinality) errors << "Metadata must be a Hash" unless .is_a?(Hash) errors end |