Class: MermaidRailsErd::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/mermaid_rails_erd/relationship.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, foreign_key, relationship_type, label = nil, fk_table = nil, fk_column = nil, pk_table = nil, pk_column = nil, is_polymorphic = false, extra_label = nil) ⇒ Relationship

Returns a new instance of Relationship.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mermaid_rails_erd/relationship.rb', line 12

def initialize(from, to, foreign_key, relationship_type, label = nil,
               fk_table = nil, fk_column = nil, pk_table = nil, pk_column = nil,
               is_polymorphic = false, extra_label = nil)
  @from_table = from
  @to_table = to
  @foreign_key = foreign_key
  @relationship_type = relationship_type
  @is_polymorphic = is_polymorphic
  @extra_label = extra_label

  # Store FK/PK information
  @fk_table = fk_table || from
  @fk_column = fk_column || foreign_key
  @pk_table = pk_table || to
  @pk_column = pk_column || "id"

  # Generate label if not provided
  @label = label || generate_label
end

Instance Attribute Details

#extra_labelObject (readonly)

Returns the value of attribute extra_label.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def extra_label
  @extra_label
end

#fk_columnObject (readonly)

Returns the value of attribute fk_column.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def fk_column
  @fk_column
end

#fk_tableObject (readonly)

Returns the value of attribute fk_table.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def fk_table
  @fk_table
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def foreign_key
  @foreign_key
end

#from_tableObject (readonly) Also known as: from

Returns the value of attribute from_table.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def from_table
  @from_table
end

#is_polymorphicObject (readonly)

Returns the value of attribute is_polymorphic.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def is_polymorphic
  @is_polymorphic
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def label
  @label
end

#pk_columnObject (readonly)

Returns the value of attribute pk_column.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def pk_column
  @pk_column
end

#pk_tableObject (readonly)

Returns the value of attribute pk_table.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def pk_table
  @pk_table
end

#relationship_typeObject (readonly) Also known as: symbol

Returns the value of attribute relationship_type.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def relationship_type
  @relationship_type
end

#to_tableObject (readonly) Also known as: to

Returns the value of attribute to_table.



5
6
7
# File 'lib/mermaid_rails_erd/relationship.rb', line 5

def to_table
  @to_table
end

Instance Method Details

#generate_labelObject



36
37
38
39
# File 'lib/mermaid_rails_erd/relationship.rb', line 36

def generate_label
  base_label = "#{@fk_table}.#{@fk_column} FK → #{@pk_table}.#{@pk_column} PK"
  @is_polymorphic ? "#{base_label} (#{@extra_label})" : base_label
end

#keyObject



32
33
34
# File 'lib/mermaid_rails_erd/relationship.rb', line 32

def key
  [from_table, to_table, foreign_key].sort.join("::")
end