Class: Acfs::SingletonResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/acfs/singleton_resource.rb

Overview

Acfs SingletonResources

Usage explanation:

Single.find      => sends GET    request to http://service:port/single
my_single.save   => sends POST   request to http://service:port/single
                               if my_single is a new object
               or sends PUT    request to http://service:port/single
                               if my_single has been requested before
my_single.delete => sends DELETE request to http://service:port/single

SingletonResources do not support the Resource method :all, since always only a single instance of the resource is being returned

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all ⇒ Object Also known as: find_by, find_by!

Undefined, raises NoMethodError. A singleton always only returns one object, therefore the methods :all and :where are not defined. :find_by is not defined on singletons, use :find instead



73
74
75
# File 'lib/acfs/singleton_resource.rb', line 73

def all
  raise ::Acfs::UnsupportedOperation.new
end

.find(id, opts = {}) {|resource| ... } ⇒ self

Find a singleton resource, optionally with params.

Examples:

single = Singleton.find # Will query `http://base.url/singletons/`

Parameters:

  • opts (Hash) (defaults to: {}) —

    Additional options.

Options Hash (opts):

  • :params (Hash) —

    Additional parameters added to request.

Yields:

  • (resource) —

    Callback block to be executed after resource was fetched successfully.

Yield Parameters:

  • resource (self) —

    Fetched resources.

Returns:

  • (self) —

    Resource object.



62
63
64
# File 'lib/acfs/singleton_resource.rb', line 62

def find(*attrs, &)
  find_single(nil, params: attrs.extract_options!, &)
end

.location_default_path(_, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
# File 'lib/acfs/singleton_resource.rb', line 80

def location_default_path(_, path)
  path
end

Instance Method Details

#delete!(**opts) ⇒ undefined

Destroy resource by sending a DELETE request. Will raise an error in case something goes wrong.

Deleting a resource is a synchronous operation.

Returns:

  • (undefined)

Raises:

See Also:

  • #delete


30
31
32
33
34
35
36
37
# File 'lib/acfs/singleton_resource.rb', line 30

def delete!(**opts)
  opts[:params] ||= {}

  operation(:delete, **opts) do |data|
    update_with data
    freeze
  end
end

#need_primary_key? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


40
41
42
# File 'lib/acfs/singleton_resource.rb', line 40

def need_primary_key?
  false
end