Class: Dbwatcher::Services::DiagramData::Attribute

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

Overview

Attribute representing a property of an entity

This class provides a standardized representation for entity attributes (columns, fields, properties) with consistent validation and serialization.

Examples:

attribute = Attribute.new(
  name: "email",
  type: "string",
  nullable: false,
  default: nil,
  metadata: { unique: true }
)
attribute.valid? # => true
attribute.to_h   # => { name: "email", type: "string", ... }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(name:, type: nil, nullable: true, default: nil, metadata: {}) ⇒ Attribute

Initialize attribute

Parameters:

  • name (String)

    attribute name

  • type (String) (defaults to: nil)

    attribute data type

  • nullable (Boolean) (defaults to: true)

    whether attribute can be null

  • default (Object) (defaults to: nil)

    default value

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

    additional type-specific information



33
34
35
36
37
38
39
40
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 33

def initialize(name:, type: nil, nullable: true, default: nil, metadata: {})
  super() # Initialize parent class
  @name = name.to_s
  @type = type.to_s
  @nullable = nullable == true
  @default = default
  @metadata = .is_a?(Hash) ?  : {}
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



24
25
26
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24

def default
  @default
end

#metadataObject

Returns the value of attribute metadata.



24
25
26
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



24
25
26
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24

def name
  @name
end

#nullableObject

Returns the value of attribute nullable.



24
25
26
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24

def nullable
  @nullable
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24

def type
  @type
end

Class Method Details

.extract_constructor_args(hash) ⇒ Object

Override base class method to handle nullable default



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

def self.extract_constructor_args(hash)
  {
    name: hash[:name],
    type: hash[:type],
    nullable: hash.key?(:nullable) ? hash[:nullable] : true,
    default: hash[:default],
    metadata: hash[:metadata] || {}
  }
end

Instance Method Details

#comparable_attributesObject

Implementation for Base class



43
44
45
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 43

def comparable_attributes
  [name, type, nullable, default, ]
end

#foreign_key?Boolean

Check if attribute is a foreign key

Returns:

  • (Boolean)

    true if attribute is a foreign key



76
77
78
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 76

def foreign_key?
  [:foreign_key] == true || name.to_s.end_with?("_id")
end

#primary_key?Boolean

Check if attribute is a primary key

Returns:

  • (Boolean)

    true if attribute is a primary key



69
70
71
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 69

def primary_key?
  [:primary_key] == true
end

#serializable_attributesObject

Implementation for Base class



48
49
50
51
52
53
54
55
56
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 48

def serializable_attributes
  {
    name: name,
    type: type,
    nullable: nullable,
    default: default,
    metadata: 
  }
end

#validation_errorsObject

Implementation for Base class



59
60
61
62
63
64
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 59

def validation_errors
  errors = []
  errors << "Name cannot be blank" if name.nil? || name.to_s.strip.empty?
  errors << "Metadata must be a Hash" unless .is_a?(Hash)
  errors
end