Class: Dbwatcher::Services::DiagramData::Relationship

Inherits:
Base
  • Object
show all
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.

Examples:

relationship = Relationship.new(
  source_id: "users",
  target_id: "orders",
  type: "has_many",
  label: "orders",
  cardinality: "one_to_many",
  metadata: { association_type: "has_many" }
)
relationship.valid? # => true
relationship.to_h   # => { source_id: "users", target_id: "orders", ... }

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, from_h, from_json, #hash, #inspect, #to_h, #to_json, #to_s

Constructor Details

#initialize(params) ⇒ Relationship

Initialize relationship

Parameters:



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

#cardinalityObject

Returns the value of attribute cardinality.



26
27
28
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26

def cardinality
  @cardinality
end

#labelObject

Returns the value of attribute label.



26
27
28
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26

def label
  @label
end

#metadataObject

Returns the value of attribute metadata.



26
27
28
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 26

def 
  @metadata
end

#source_idObject

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_idObject

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

#typeObject

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_attributesObject

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_notationString

Get cardinality for ERD notation

Returns:

  • (String)

    ERD cardinality 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_cardinalityString?

Infer cardinality from relationship type if not explicitly set

Returns:

  • (String, nil)

    inferred cardinality or nil if can’t be determined



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_attributesObject

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

Returns:

  • (Boolean)

    true if relationship has required fields



75
76
77
# File 'lib/dbwatcher/services/diagram_data/relationship.rb', line 75

def valid?
  validation_errors.empty?
end

#validation_errorsArray<String>

Get validation errors

Returns:

  • (Array<String>)

    array of validation error messages



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