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



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

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



88
89
90
# File 'lib/easy_talk/model.rb', line 88

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

#initialize(attributes = {}) ⇒ Object



51
52
53
54
# File 'lib/easy_talk/model.rb', line 51

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

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

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/easy_talk/model.rb', line 72

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



79
80
81
82
83
84
85
# File 'lib/easy_talk/model.rb', line 79

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