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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/easy_talk/model.rb', line 77

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/easy_talk/model.rb', line 120

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



110
111
112
# File 'lib/easy_talk/model.rb', line 110

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

#initialize(attributes = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/easy_talk/model.rb', line 48

def initialize(attributes = {})
  @additional_properties = {}
  super # Perform initial mass assignment

  # After initial assignment, instantiate nested EasyTalk::Model objects
  schema_def = self.class.schema_definition

  # Only proceed if we have a valid 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|
    # Get the defined type and the currently assigned value
    defined_type = prop_definition[:type]
    current_value = public_send(prop_name)
    nilable_type = defined_type.respond_to?(:nilable?) && defined_type.nilable?

    next if nilable_type && current_value.nil?

    defined_type = T::Utils::Nilable.get_underlying_type(defined_type) if nilable_type

    # Check if the type is another EasyTalk::Model and the value is a Hash
    next unless defined_type.is_a?(Class) && defined_type.include?(EasyTalk::Model) && current_value.is_a?(Hash)

    # Instantiate the nested model and assign it back
    nested_instance = defined_type.new(current_value)
    public_send("#{prop_name}=", nested_instance)
  end
end

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

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/easy_talk/model.rb', line 93

def respond_to_missing?(method_name, include_private = false)
  method_string = method_name.to_s
  method_string.end_with?('=') ? method_string.chomp('=') : method_string
  self.class.additional_properties_allowed? || super
end

#to_hObject

to_h includes both defined and additional properties



115
116
117
# File 'lib/easy_talk/model.rb', line 115

def to_h
  to_hash.merge(@additional_properties)
end

#to_hashObject

Add to_hash method to convert defined properties to hash



100
101
102
103
104
105
106
107
# File 'lib/easy_talk/model.rb', line 100

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