Class: ResourcesController::ResourceService

Inherits:
BasicObject
Defined in:
lib/resources_controller.rb

Overview

Proxy class to provide a consistent API for resource_service. This is mostly required for Singleton resources. Also allows decoration of the resource service with custom finders

Direct Known Subclasses

SingletonResourceService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ ResourceService

Returns a new instance of ResourceService.



743
744
745
# File 'lib/resources_controller.rb', line 743

def initialize(controller)
  @controller = controller
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



747
748
749
# File 'lib/resources_controller.rb', line 747

def method_missing(method, *args, **kwargs, &block)
  service.__send__(method, *args, **kwargs, &block)
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



740
741
742
# File 'lib/resources_controller.rb', line 740

def controller
  @controller
end

Instance Method Details

#destroy(*args, **kwargs) ⇒ Object

find the resource If we have a resource service, we call destroy on it with the resource id, so that any callbacks can be triggered Otherwise, just call destroy on the resource



767
768
769
770
771
772
773
774
775
# File 'lib/resources_controller.rb', line 767

def destroy(*args, **kwargs)
  resource = find(*args, **kwargs)
  if enclosing_resource
    service.destroy(*args, **kwargs)
    resource
  else
    resource.destroy
  end
end

#find(*args, **kwargs, &block) ⇒ Object



751
752
753
# File 'lib/resources_controller.rb', line 751

def find(*args, **kwargs, &block)
  resource_specification.find ? resource_specification.find_custom(controller) : super
end

#new(*args, **kwargs, &block) ⇒ Object

build association on the enclosing resource if there is one, otherwise call new



756
757
758
759
760
761
762
# File 'lib/resources_controller.rb', line 756

def new(*args, **kwargs, &block)
  if enclosing_resource
    service.build(*args, **kwargs, &block)
  else
    service.new(*args, **kwargs, &block)
  end
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


777
778
779
# File 'lib/resources_controller.rb', line 777

def respond_to?(method, include_private = false)
  super || service.respond_to?(method)
end

#serviceObject



781
782
783
# File 'lib/resources_controller.rb', line 781

def service
  @service ||= enclosing_resource ? enclosing_resource.send(resource_specification.source) : resource_class
end