Class: Dbwatcher::Services::DiagramData::Attribute
- 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.
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nullable ⇒ Object
Returns the value of attribute nullable.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.extract_constructor_args(hash) ⇒ Object
Override base class method to handle nullable default.
Instance Method Summary collapse
-
#comparable_attributes ⇒ Object
Implementation for Base class.
-
#foreign_key? ⇒ Boolean
Check if attribute is a foreign key.
-
#initialize(name:, type: nil, nullable: true, default: nil, metadata: {}) ⇒ Attribute
constructor
Initialize attribute.
-
#primary_key? ⇒ Boolean
Check if attribute is a primary key.
-
#serializable_attributes ⇒ Object
Implementation for Base class.
-
#validation_errors ⇒ Object
Implementation for Base class.
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
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
#default ⇒ Object
Returns the value of attribute default.
24 25 26 |
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24 def default @default end |
#metadata ⇒ Object
Returns the value of attribute metadata.
24 25 26 |
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24 def name @name end |
#nullable ⇒ Object
Returns the value of attribute nullable.
24 25 26 |
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 24 def nullable @nullable end |
#type ⇒ Object
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_attributes ⇒ Object
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
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
69 70 71 |
# File 'lib/dbwatcher/services/diagram_data/attribute.rb', line 69 def primary_key? [:primary_key] == true end |
#serializable_attributes ⇒ Object
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_errors ⇒ Object
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 |