Module: EasyTalk::Model::InstanceMethods

Defined in:
lib/easy_talk/model.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy_talk/model.rb', line 50

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

#as_json(_options = {}) ⇒ Object

Override as_json to include both defined and additional properties



82
83
84
# File 'lib/easy_talk/model.rb', line 82

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

#initialize(attributes = {}) ⇒ Object



45
46
47
48
# File 'lib/easy_talk/model.rb', line 45

def initialize(attributes = {})
  @additional_properties = {}
  super
end

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

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/easy_talk/model.rb', line 66

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



73
74
75
76
77
78
79
# File 'lib/easy_talk/model.rb', line 73

def to_hash
  return {} unless self.class.properties

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