Class: Dbwatcher::Services::DiagramData::Base

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

Examples:

class MyClass < Base
  def comparable_attributes
    [name, type, value]
  end

  def serializable_attributes
    { name: name, type: type, value: value }
  end
end

Direct Known Subclasses

Attribute, Entity, Relationship

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Object

Create object from hash

Parameters:

  • hash (Hash)

    object data

Returns:

  • (Object)

    new object instance



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

Parameters:

  • json (String)

    JSON string

Returns:

  • (Object)

    new object instance



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

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Boolean)

    true if objects are equal



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_attributesObject

Default implementation - subclasses should override

Raises:

  • (NotImplementedError)


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

#hashInteger

Generate hash code for object

Returns:

  • (Integer)

    hash code



49
50
51
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 49

def hash
  comparable_attributes.hash
end

#inspectString

Detailed string representation

Returns:

  • (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_attributesObject

Default implementation - subclasses should override

Raises:

  • (NotImplementedError)


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_hHash

Serialize object to hash

Returns:

  • (Hash)

    serialized object data



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

Returns:

  • (String)

    JSON representation



63
64
65
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 63

def to_json(*args)
  to_h.to_json(*args)
end

#to_sString

String representation of object

Returns:

  • (String)

    string representation



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

Returns:

  • (Boolean)

    true if object has no validation errors



32
33
34
# File 'lib/dbwatcher/services/diagram_data/base.rb', line 32

def valid?
  validation_errors.empty?
end

#validation_errorsObject

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