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

Constant Summary

Constants included from Resource::Attributes

Resource::Attributes::Uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::Validation

#remote_errors, #remote_errors=, #save!, #valid?

Methods included from Resource::Dirty

#loaded!, #reset_changes, #save!, #write_raw_attribute

Methods included from Resource::Operational

#operation

Methods included from Resource::Locatable

#primary_key?, #url

Methods included from Resource::Persistence

#delete, #new?, #persisted?, #save, #save!, #update_attributes, #update_attributes!

Methods included from Resource::Loadable

#loaded!, #loaded?

Methods included from Resource::Attributes

#attributes, #attributes=, #initialize, #read_attribute, #write_attribute, #write_attributes, #write_local_attribute, #write_raw_attribute

Methods included from Resource::Initialization

#initialize

Class Method Details

.allObject 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, &block)
  find_single nil, params: attrs.extract_options!, &block
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:



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:



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

def need_primary_key?
  false
end