Module: EasyTalk::Model::InstanceMethods

Defined in:
lib/easy_talk/model.rb

Overview

Instance methods mixed into models that include EasyTalk::Model

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/easy_talk/model.rb', line 117

def method_missing(method_name, *args)
  method_string = method_name.to_s
  if method_string.end_with?('=')
    property_name = method_string.chomp('=')
    if self.class.additional_properties_allowed?
      @additional_properties[property_name] = args.first
    else
      super
    end
  elsif self.class.additional_properties_allowed? && @additional_properties.key?(method_string)
    @additional_properties[method_string]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object

Allow comparison with hashes



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/easy_talk/model.rb', line 169

def ==(other)
  case other
  when Hash
    # Convert both to comparable format for comparison
    self_hash = (self.class.schema_definition.schema[:properties] || {}).keys.each_with_object({}) do |prop, hash|
      hash[prop] = send(prop)
    end

    # Handle both symbol and string keys in the other hash
    other_normalized = other.transform_keys(&:to_sym)
    self_hash == other_normalized
  else
    super
  end
end

#as_json(_options = {}) ⇒ Object

Override as_json to include both defined and additional properties



151
152
153
# File 'lib/easy_talk/model.rb', line 151

def as_json(_options = {})
  to_hash.merge(@additional_properties)
end

#initialize(attributes = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/easy_talk/model.rb', line 58

def initialize(attributes = {})
  @additional_properties = {}
  provided_keys = attributes.keys.to_set(&:to_sym)

  super # Perform initial mass assignment

  schema_def = self.class.schema_definition
  return unless schema_def.respond_to?(:schema) && schema_def.schema.is_a?(Hash)

  (schema_def.schema[:properties] || {}).each do |prop_name, prop_definition|
    process_property_initialization(prop_name, prop_definition, provided_keys)
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
# File 'lib/easy_talk/model.rb', line 133

def respond_to_missing?(method_name, include_private = false)
  return super unless self.class.additional_properties_allowed?

  method_string = method_name.to_s
  method_string.end_with?('=') || @additional_properties.key?(method_string) || super
end

#to_hObject

to_h includes both defined and additional properties



156
157
158
# File 'lib/easy_talk/model.rb', line 156

def to_h
  to_hash.merge(@additional_properties)
end

#to_hashObject

Add to_hash method to convert defined properties to hash



141
142
143
144
145
146
147
148
# File 'lib/easy_talk/model.rb', line 141

def to_hash
  properties_to_include = (self.class.schema_definition.schema[:properties] || {}).keys
  return {} if properties_to_include.empty?

  properties_to_include.each_with_object({}) do |prop, hash|
    hash[prop.to_s] = send(prop)
  end
end

#to_json_schemaHash

Returns a Hash representing the schema in a format compatible with RubyLLM. Delegates to the class method. Required for RubyLLM's with_schema method.

Returns:

  • (Hash)

    The RubyLLM-compatible schema representation



164
165
166
# File 'lib/easy_talk/model.rb', line 164

def to_json_schema
  self.class.to_json_schema
end