Class: Dbwatcher::Services::DiagramData::Entity
- 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.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ Object
Returns the value of attribute id.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.extract_constructor_args(hash) ⇒ Object
Override base class method to handle attributes array.
Instance Method Summary collapse
-
#add_attribute(attribute) ⇒ Attribute
Add an attribute to the entity.
-
#comparable_attributes ⇒ Object
Implementation for Base class.
-
#foreign_key_attributes ⇒ Array<Attribute>
Get foreign key attributes.
-
#initialize(id:, name:, type: "default", attributes: [], metadata: {}) ⇒ Entity
constructor
Initialize entity.
-
#inspect ⇒ String
Detailed string representation.
-
#primary_key_attributes ⇒ Array<Attribute>
Get primary key attributes.
-
#serializable_attributes ⇒ Object
Implementation for Base class.
-
#validation_errors ⇒ Object
Implementation for Base class.
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
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
#attributes ⇒ Object
Returns the value of attribute attributes.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 26 def attributes @attributes end |
#id ⇒ Object
Returns the value of attribute id.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 26 def id @id end |
#metadata ⇒ Object
Returns the value of attribute metadata.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 26 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
26 27 28 |
# File 'lib/dbwatcher/services/diagram_data/entity.rb', line 26 def name @name end |
#type ⇒ Object
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
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_attributes ⇒ Object
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_attributes ⇒ Array<Attribute>
Get 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 |
#inspect ⇒ 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_attributes ⇒ Array<Attribute>
Get 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_attributes ⇒ Object
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_errors ⇒ Object
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 |