Class: CARMA::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/carma/models/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_to_request_payload(method_sym) ⇒ Object



32
33
34
# File 'lib/carma/models/base.rb', line 32

def self.after_to_request_payload(method_sym)
  self.request_payload_after_hook = method_sym
end

.request_payload_key(key, *keys) ⇒ Object

Hook allowing the inheriting class to set the attribute keys that should be included when parsing the object to a request_payload object.



26
27
28
29
30
# File 'lib/carma/models/base.rb', line 26

def self.request_payload_key(key, *keys)
  # Assignment vs concat is important for the inheriting class
  # to have it's own array vs referencing Base.request_payload_keys
  self.request_payload_keys += [key, *keys]
end

Instance Method Details

#to_request_payloadObject

rubocop:enable ThreadSafety/ClassAndModuleAttributes



13
14
15
16
17
18
19
20
21
22
# File 'lib/carma/models/base.rb', line 13

def to_request_payload
  request_payload = request_payload_keys.each_with_object({}) do |key, result|
    value = send(key.to_s)
    value = value.to_request_payload if value.class.ancestors.include?(Base)

    result[key.to_s.camelize(:lower)] = value
  end

  request_payload_after_hook ? send(request_payload_after_hook, request_payload) : request_payload
end