Class: Dbwatcher::Services::DiagramData::Base
- Inherits:
-
Object
- Object
- Dbwatcher::Services::DiagramData::Base
- Defined in:
- lib/dbwatcher/services/diagram_data/base.rb
Overview
Base class for diagram data objects
Provides common functionality for serialization, validation, and comparison that is shared across Attribute, Entity, and Relationship classes.
Subclasses must implement:
-
comparable_attributes: Array of values used for equality comparison
-
serializable_attributes: Hash of attributes for serialization
-
validation_errors: Array of validation error strings (optional)
Direct Known Subclasses
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
Create object from hash.
-
.from_json(json) ⇒ Object
Create object from JSON.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality with another object of the same class.
-
#comparable_attributes ⇒ Object
Default implementation - subclasses should override.
-
#hash ⇒ Integer
Generate hash code for object.
-
#inspect ⇒ String
Detailed string representation.
-
#serializable_attributes ⇒ Object
Default implementation - subclasses should override.
-
#to_h ⇒ Hash
Serialize object to hash.
-
#to_json(*args) ⇒ String
Serialize object to JSON.
-
#to_s ⇒ String
String representation of object.
-
#valid? ⇒ Boolean
Check if object is valid.
-
#validation_errors ⇒ Object
Default implementation - subclasses should override if validation needed.
Class Method Details
.from_h(hash) ⇒ Object
Create object from hash
71 72 73 74 75 76 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 71 def self.from_h(hash) # Convert string keys to symbols for consistent access hash = hash.transform_keys(&:to_sym) if hash.respond_to?(:transform_keys) && hash.keys.first.is_a?(String) new(**extract_constructor_args(hash)) end |
.from_json(json) ⇒ Object
Create object from JSON
82 83 84 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 82 def self.from_json(json) from_h(JSON.parse(json)) end |
Instance Method Details
#==(other) ⇒ Boolean
Check equality with another object of the same class
40 41 42 43 44 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 40 def ==(other) return false unless other.is_a?(self.class) comparable_attributes == other.comparable_attributes end |
#comparable_attributes ⇒ Object
Default implementation - subclasses should override
103 104 105 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 103 def comparable_attributes raise NotImplementedError, "#{self.class} must implement #comparable_attributes" end |
#hash ⇒ Integer
Generate hash code for object
49 50 51 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 49 def hash comparable_attributes.hash end |
#inspect ⇒ String
Detailed string representation
97 98 99 100 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 97 def inspect attrs = serializable_attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ") "#{self.class.name}(#{attrs})" end |
#serializable_attributes ⇒ Object
Default implementation - subclasses should override
108 109 110 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 108 def serializable_attributes raise NotImplementedError, "#{self.class} must implement #serializable_attributes" end |
#to_h ⇒ Hash
Serialize object to hash
56 57 58 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 56 def to_h serializable_attributes end |
#to_json(*args) ⇒ String
Serialize object to JSON
63 64 65 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 63 def to_json(*args) to_h.to_json(*args) end |
#to_s ⇒ String
String representation of object
89 90 91 92 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 89 def to_s attrs = serializable_attributes.map { |k, v| "#{k}: #{v}" }.join(", ") "#{self.class.name}(#{attrs})" end |
#valid? ⇒ Boolean
Check if object is valid
32 33 34 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 32 def valid? validation_errors.empty? end |
#validation_errors ⇒ Object
Default implementation - subclasses should override if validation needed
113 114 115 |
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 113 def validation_errors [] end |