Class: ApiClient::Resource::Base

Inherits:
Base
  • Object
show all
Extended by:
Mixins::Configuration, Mixins::Delegation
Defined in:
lib/api_client/resource/base.rb

Instance Attribute Summary

Attributes included from Mixins::Scoping

#default_scopes

Attributes included from Mixins::ConnectionHooks

#connection_hooks

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Delegation

delegate

Methods included from Mixins::Configuration

dsl_accessor

Methods inherited from Base

#id, #inspect, parse, subkey_class

Methods included from Mixins::Inheritance

#inherited

Methods included from Mixins::Instantiation

#build, #build_many, #build_one, extended

Methods included from Mixins::Scoping

#always, #scope, #scope_in_thread, #scope_thread_attribute_name, #scoped

Methods included from Mixins::ConnectionHooks

#connection

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ApiClient::Base

Class Method Details

.inherited(subclass) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/api_client/resource/base.rb', line 15

def inherited(subclass)
  super
  small_name = NameResolver.resolve(subclass.name)
  subclass.namespace small_name
  subclass.prefix    self.prefix
  subclass.always do
    name    = small_name
    pre_fix = prefix
    path ["", prefix, "#{name}s"].compact.join('/')
  end
end

.scope(options = {}) ⇒ Object



27
28
29
# File 'lib/api_client/resource/base.rb', line 27

def scope(options = {})
  scope_in_thread || ApiClient::Resource::Scope.new(self).params(options)
end

Instance Method Details

#destroyObject



41
42
43
# File 'lib/api_client/resource/base.rb', line 41

def destroy
  get_scope.destroy(self.id)
end

#get_scopeObject



59
60
61
# File 'lib/api_client/resource/base.rb', line 59

def get_scope
  original_scope || self.class
end

#payloadObject



45
46
47
48
49
# File 'lib/api_client/resource/base.rb', line 45

def payload
  hash = self.to_hash
  hash.delete('id') # This key is never required
  hash
end

#persisted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/api_client/resource/base.rb', line 33

def persisted?
  !!self.id
end

#remote_createObject



55
56
57
# File 'lib/api_client/resource/base.rb', line 55

def remote_create
  get_scope.create(payload)
end

#remote_updateObject



51
52
53
# File 'lib/api_client/resource/base.rb', line 51

def remote_update
  get_scope.update(self.id, payload)
end

#saveObject



37
38
39
# File 'lib/api_client/resource/base.rb', line 37

def save
  self.persisted? ? remote_update : remote_create
end