Class: RemoteResource::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker, BaseClassMethods, Dsl
Includes:
Attributes, Rescue
Defined in:
lib/remote_resource/base.rb

Overview

the base class for defining an api.

Instance Attribute Summary collapse

Attributes included from Dsl

#client_proc, #resources

Instance Method Summary collapse

Methods included from BaseClassMethods

ensure_loaded, find_descendant, symbol_name, underscore

Methods included from Dsl

attribute, attributes

Methods included from Rescue

#with_error_handling

Methods included from Attributes

#create_attributes, #get_attribute

Methods included from Notifications

#instrument, #instrument_attribute

Constructor Details

#initialize(**args) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
# File 'lib/remote_resource/base.rb', line 16

def initialize(**args)
  @scope = args
  create_attributes(self)
  AttributeMethodAttacher.new(self.class).attach_to(self.singleton_class)
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



14
15
16
# File 'lib/remote_resource/base.rb', line 14

def scope
  @scope
end

Instance Method Details

#clientObject



22
23
24
# File 'lib/remote_resource/base.rb', line 22

def client
  self.class.client_proc.call(@scope)
end

#resource(name = :default, resource_client = nil) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/remote_resource/base.rb', line 26

def resource(name = :default, resource_client = nil)
  if (attr_resource = self.class.resources[name])
    attr_resource.call(resource_client || client, @scope)
  else
    msg = "there is no resource named `#{name}` on #{self.class.name}."
    fail ArgumentError, msg
  end
end