Class: A2A::Types::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/types/base_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ BaseModel

Initialize a new model instance

Parameters:

  • The attributes to set



23
24
25
26
27
28
29
30
31
# File 'lib/a2a/types/base_model.rb', line 23

def initialize(**attributes)
  # Set instance variables for all provided attributes
  attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  # Validate the instance after initialization
  validate! if respond_to?(:validate!, true)
end

Class Method Details

.from_h(hash) ⇒ BaseModel

Create an instance from a hash

Parameters:

  • The hash to create from

Returns:

  • The new instance



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/a2a/types/base_model.rb', line 72

def self.from_h(hash)
  return hash if hash.is_a?(self) # Already an instance of this class
  return nil if hash.nil?

  # Convert string keys to symbols and snake_case camelCase keys
  normalized_hash = {}
  hash.each do |key, value|
    snake_key = underscore(key.to_s).to_sym
    normalized_hash[snake_key] = value
  end

  new(**normalized_hash)
end

.from_json(json_string) ⇒ BaseModel

Create an instance from JSON string

Parameters:

  • The JSON string

Returns:

  • The new instance



101
102
103
104
105
# File 'lib/a2a/types/base_model.rb', line 101

def self.from_json(json_string)
  require "json"
  hash = JSON.parse(json_string)
  from_h(hash)
end

.underscore(string) ⇒ String (private)

Convert camelCase to snake_case

Parameters:

  • The string to convert

Returns:

  • The snake_case string



153
154
155
156
157
158
# File 'lib/a2a/types/base_model.rb', line 153

def self.underscore(string)
  string.to_s
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .downcase
end

Instance Method Details

#==(other) ⇒ Boolean

Check equality with another model

Parameters:

  • The other object to compare

Returns:

  • True if equal



112
113
114
115
116
# File 'lib/a2a/types/base_model.rb', line 112

def ==(other)
  return false unless other.is_a?(self.class)

  to_h == other.to_h
end

#camelize(string) ⇒ String (private)

Convert snake_case to camelCase

Parameters:

  • The string to convert

Returns:

  • The camelCase string



144
145
146
# File 'lib/a2a/types/base_model.rb', line 144

def camelize(string)
  string.to_s.gsub(/_([a-z])/) { ::Regexp.last_match(1).upcase }
end

#hashInteger

Generate hash code for the model

Returns:

  • The hash code



122
123
124
# File 'lib/a2a/types/base_model.rb', line 122

def hash
  to_h.hash
end

#to_h(camel_case: true) ⇒ Hash

Convert the model to a hash representation

Parameters:

  • (defaults to: true)

    Whether to convert keys to camelCase

Returns:

  • The model as a hash



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/a2a/types/base_model.rb', line 38

def to_h(camel_case: true)
  hash = {}

  instance_variables.each do |var|
    key = var.to_s.delete("@")
    value = instance_variable_get(var)

    # Convert nested models
    case value
    when BaseModel
      value = value.to_h(camel_case: camel_case)
    when Array
      value = value.map do |item|
        item.is_a?(BaseModel) ? item.to_h(camel_case: camel_case) : item
      end
    when Hash
      value = value.transform_values do |v|
        v.is_a?(BaseModel) ? v.to_h(camel_case: camel_case) : v
      end
    end

    # Convert key to camelCase if requested
    key = camelize(key) if camel_case
    hash[key] = value unless value.nil?
  end

  hash
end

#to_json(**options) ⇒ String

Convert to JSON string

Parameters:

  • JSON generation options

Returns:

  • The JSON representation



91
92
93
94
# File 'lib/a2a/types/base_model.rb', line 91

def to_json(**options)
  require "json"
  to_h.to_json(**options)
end

#valid?Boolean

Check if the model is valid

Returns:

  • True if valid



130
131
132
133
134
135
# File 'lib/a2a/types/base_model.rb', line 130

def valid?
  validate!
  true
rescue StandardError
  false
end

#validate_array_type(field, expected_type) ⇒ Object (private)

Validate that an array field contains only items of the expected type

Parameters:

  • The field name

  • The expected item type

Raises:

  • If any item is not of the expected type



211
212
213
214
215
216
217
218
219
220
# File 'lib/a2a/types/base_model.rb', line 211

def validate_array_type(field, expected_type)
  value = instance_variable_get("@#{field}")
  return if value.nil?

  validate_type(field, Array)

  value.each_with_index do |item, index|
    raise ArgumentError, "#{field}[#{index}] must be a #{expected_type}" unless item.is_a?(expected_type)
  end
end

#validate_inclusion(field, allowed_values) ⇒ Object (private)

Validate that a field is one of the allowed values

Parameters:

  • The field name

  • The allowed values

Raises:

  • If the field value is not allowed



178
179
180
181
182
183
184
185
# File 'lib/a2a/types/base_model.rb', line 178

def validate_inclusion(field, allowed_values)
  value = instance_variable_get("@#{field}")
  return if value.nil?

  return if allowed_values.include?(value)

  raise ArgumentError, "#{field} must be one of: #{allowed_values.join(', ')}"
end

#validate_required(*fields) ⇒ Object (private)

Validate required fields

Parameters:

  • The required field names

Raises:

  • If any required field is missing



165
166
167
168
169
170
# File 'lib/a2a/types/base_model.rb', line 165

def validate_required(*fields)
  fields.each do |field|
    value = instance_variable_get("@#{field}")
    raise ArgumentError, "#{field} is required" if value.nil? || (value.respond_to?(:empty?) && value.empty?)
  end
end

#validate_type(field, expected_type) ⇒ Object (private)

Validate that a field is of the expected type

Parameters:

  • The field name

  • The expected type(s)

Raises:

  • If the field is not of the expected type



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/a2a/types/base_model.rb', line 193

def validate_type(field, expected_type)
  value = instance_variable_get("@#{field}")
  return if value.nil?

  types = expected_type.is_a?(Array) ? expected_type : [expected_type]

  return if types.any? { |type| value.is_a?(type) }

  type_names = types.map(&:to_s).join(" or ")
  raise ArgumentError, "#{field} must be a #{type_names}"
end