Class: SimpleAcp::Models::Base Abstract
- Inherits:
-
Object
- Object
- SimpleAcp::Models::Base
- Defined in:
- lib/simple_acp/models/base.rb
Overview
Subclass and use Base.attribute to define attributes
Base class for all ACP models providing common serialization.
Provides a simple DSL for declaring attributes with defaults, automatic JSON serialization/deserialization, and equality comparisons.
Direct Known Subclasses
AgentListResponse, AgentManifest, AgentStatus, Annotations, Author, AwaitRequest, AwaitResume, Capability, CitationMetadata, Contributor, Dependency, Event, Link, Message, MessagePart, Metadata, Run, RunCreateRequest, RunResumeRequest, Session, SessionResponse, TrajectoryMetadata
Class Method Summary collapse
-
.attribute(name, type: nil, default: nil, required: false) ⇒ void
Define an attribute on this model.
-
.attributes ⇒ Hash<Symbol, Hash>
Get all attributes including inherited ones.
-
.from_hash(hash) ⇒ Base?
(also: from_h)
Create an instance from a hash.
-
.own_attributes ⇒ Hash<Symbol, Hash>
Get only attributes defined on this class (not inherited).
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality based on all attributes.
-
#hash ⇒ Integer
Compute hash based on all attributes.
-
#initialize(**kwargs) ⇒ Base
constructor
Initialize with keyword arguments.
-
#to_h ⇒ Hash
Convert to a hash for JSON serialization.
-
#to_json(*args) ⇒ String
Convert to JSON string.
Constructor Details
#initialize(**kwargs) ⇒ Base
Initialize with keyword arguments.
81 82 83 84 85 86 |
# File 'lib/simple_acp/models/base.rb', line 81 def initialize(**kwargs) self.class.attributes.each do |name, opts| value = kwargs.fetch(name) { opts[:default].is_a?(Proc) ? opts[:default].call : opts[:default] } instance_variable_set(:"@#{name}", value) end end |
Class Method Details
.attribute(name, type: nil, default: nil, required: false) ⇒ void
This method returns an undefined value.
Define an attribute on this model.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_acp/models/base.rb', line 27 def attribute(name, type: nil, default: nil, required: false) @attributes ||= {} @attributes[name] = { type: type, default: default, required: required } attr_reader name define_method(:"#{name}=") do |value| instance_variable_set(:"@#{name}", value) end end |
.attributes ⇒ Hash<Symbol, Hash>
Get all attributes including inherited ones.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/simple_acp/models/base.rb', line 41 def attributes @attributes ||= {} # Inherit attributes from parent class if superclass.respond_to?(:attributes) superclass.attributes.merge(@attributes) else @attributes end end |
.from_hash(hash) ⇒ Base? Also known as: from_h
Create an instance from a hash.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/simple_acp/models/base.rb', line 63 def from_hash(hash) return nil if hash.nil? instance = new attributes.each do |name, _opts| key = name.to_s value = hash.key?(key) ? hash[key] : hash[name] instance.send(:"#{name}=", value) unless value.nil? end instance end |
.own_attributes ⇒ Hash<Symbol, Hash>
Get only attributes defined on this class (not inherited).
55 56 57 |
# File 'lib/simple_acp/models/base.rb', line 55 def own_attributes @attributes ||= {} end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality based on all attributes.
112 113 114 115 116 117 118 |
# File 'lib/simple_acp/models/base.rb', line 112 def ==(other) return false unless other.is_a?(self.class) self.class.attributes.keys.all? do |name| send(name) == other.send(name) end end |
#hash ⇒ Integer
Compute hash based on all attributes.
125 126 127 |
# File 'lib/simple_acp/models/base.rb', line 125 def hash self.class.attributes.keys.map { |name| send(name) }.hash end |
#to_h ⇒ Hash
Convert to a hash for JSON serialization.
91 92 93 94 95 96 97 98 |
# File 'lib/simple_acp/models/base.rb', line 91 def to_h self.class.attributes.each_with_object({}) do |(name, _opts), hash| value = send(name) next if value.nil? hash[name] = serialize_value(value) end end |
#to_json(*args) ⇒ String
Convert to JSON string.
104 105 106 |
# File 'lib/simple_acp/models/base.rb', line 104 def to_json(*args) to_h.to_json(*args) end |