Class: Dbwatcher::Services::DiagramData::Entity

Inherits:
Base
  • Object
show all
Defined in:
lib/dbwatcher/services/diagram_data/entity.rb

Overview

Entity representing a node in any diagram

This class provides a standardized representation for all diagram entities (nodes, tables, models, etc.) with consistent validation and serialization.

Examples:

entity = Entity.new(
  id: "users",
  name: "User",
  type: "table",
  attributes: [
    Attribute.new(name: "id", type: "integer", nullable: false, metadata: { primary_key: true })
  ],
  metadata: { columns: ["id", "name", "email"] }
)
entity.valid? # => true
entity.to_h   # => { id: "users", name: "User", ... }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, from_h, from_json, #hash, #to_h, #to_json, #to_s, #valid?

Constructor Details

#initialize(id:, name:, type: "default", attributes: [], metadata: {}) ⇒ Entity

Initialize entity

Parameters:

  • id (String)

    unique identifier for the entity

  • name (String)

    display name for the entity

  • type (String) (defaults to: "default")

    entity type (table, model, etc.)

  • attributes (Array<Attribute>) (defaults to: [])

    entity attributes/properties

  • metadata (Hash) (defaults to: {})

    additional type-specific information



35
36
37
38
39
40
41
42
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 35

def initialize(id:, name:, type: "default", attributes: [], metadata: {})
  super() # Initialize parent class
  @id = id.to_s
  @name = name.to_s
  @type = type.to_s
  @attributes = attributes.is_a?(Array) ? attributes : []
  @metadata = .is_a?(Hash) ?  : {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.extract_constructor_args(hash) ⇒ Object

Override base class method to handle attributes array



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 105

def self.extract_constructor_args(hash)
  attrs = []
  if hash[:attributes] || hash["attributes"]
    attr_data = hash[:attributes] || hash["attributes"]
    attrs = attr_data.map { |attr| Attribute.from_h(attr) }
  end

  {
    id: hash[:id] || hash["id"],
    name: hash[:name] || hash["name"],
    type: hash[:type] || hash["type"] || "default",
    attributes: attrs,
    metadata: hash[:metadata] || hash["metadata"] || {}
  }
end

Instance Method Details

#add_attribute(attribute) ⇒ Attribute

Add an attribute to the entity

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if attribute is invalid



82
83
84
85
86
87
88
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 82

def add_attribute(attribute)
  raise ArgumentError, "Attribute must be an Attribute instance" unless attribute.is_a?(Attribute)
  raise ArgumentError, "Attribute is invalid: #{attribute.validation_errors.join(", ")}" unless attribute.valid?

  @attributes << attribute
  attribute
end

#comparable_attributesObject

Implementation for Base class



45
46
47
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 45

def comparable_attributes
  [id, name, type, attributes, ]
end

#foreign_key_attributesArray<Attribute>

Get foreign key attributes

Returns:

  • (Array<Attribute>)

    foreign key attributes



100
101
102
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 100

def foreign_key_attributes
  attributes.select(&:foreign_key?)
end

#inspectString

Detailed string representation

Returns:

  • (String)

    detailed string representation



124
125
126
127
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 124

def inspect
  "#{self.class.name}(id: #{id.inspect}, name: #{name.inspect}, " \
    "type: #{type.inspect}, attributes: #{attributes.length}, metadata: #{.inspect})"
end

#primary_key_attributesArray<Attribute>

Get primary key attributes

Returns:

  • (Array<Attribute>)

    primary key attributes



93
94
95
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 93

def primary_key_attributes
  attributes.select(&:primary_key?)
end

#serializable_attributesObject

Implementation for Base class



50
51
52
53
54
55
56
57
58
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 50

def serializable_attributes
  {
    id: id,
    name: name,
    type: type,
    attributes: attributes.map(&:to_h),
    metadata: 
  }
end

#validation_errorsObject

Implementation for Base class



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 61

def validation_errors
  errors = []
  errors << "ID cannot be blank" if id.nil? || id.to_s.strip.empty?
  errors << "Name cannot be blank" if name.nil? || name.to_s.strip.empty?
  errors << "Type cannot be blank" if type.nil? || type.to_s.strip.empty?
  errors << "Attributes must be an Array" unless attributes.is_a?(Array)
  errors << "Metadata must be a Hash" unless .is_a?(Hash)

  # Validate all attributes
  attributes.each_with_index do |attribute, index|
    errors << "Attribute at index #{index} is invalid" unless attribute.is_a?(Attribute) && attribute.valid?
  end

  errors
end