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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/easy_talk/model.rb', line 82

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



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

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

#initialize(attributes = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/easy_talk/model.rb', line 53

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

  # After initial assignment, instantiate nested EasyTalk::Model objects
  # Get the appropriate schema definition based on model type
  schema_def = if self.class.respond_to?(:active_record_schema_definition)
                 self.class.active_record_schema_definition
               else
                 self.class.schema_definition
               end

  # 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)

    # 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)


98
99
100
101
102
# File 'lib/easy_talk/model.rb', line 98

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_hashObject

Add to_hash method to convert defined properties to hash



105
106
107
108
109
110
111
112
# File 'lib/easy_talk/model.rb', line 105

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