class Hollow::Application

A RESTful service provider framework which provides dynamic access to REST resource objects, allowing routers to service reqeusts without any knowledge of the types that exist in business code.

encapsulate all or part of a system's business logic.

Stateless or Stateful modules.

configuration (see settings). Different application instances can use the same underlying resources, but they will filter access to those resources differently. Requests through one application instance may succeed while requests through another may raise access exceptions.

handle_request method.

Constants

DEFAULT_SETTINGS

Default application settings.

Attributes

settings[R]

@return [Hash] the application settings

Public Class Methods

new(settings = {}) click to toggle source

Create a new application instance. @param [Hash] settings application settings. See {DEFAULT_SETTINGS} for

defaults.

@option settings [Array<Symbol>, Array<String>] :resource_methods

Resource method names which may be invoked on resource instances by
this application. This effectively defines the service provider API.

@option settings [String] :autorequire

location in the file system relative to which other filesystem locations
are evaluated.

@option settings [Array<String>] :autorequire

file system locations where resource classes may be found; all classes
in these directories will be required immediately. These locations are
relative to `:autorequire[:root]`.
# File lib/hollow/application.rb, line 80
def initialize(settings = {})
  @settings = DEFAULT_SETTINGS.merge(settings)
  @settings[:resource_methods].map! { |m| m.to_sym }
  @settings[:autorequire][:directories].each do |dir|
    require_all "#{@settings[:autorequire][:root]}/#{dir}"
  end
end

Private Class Methods

get_resource(resource) click to toggle source
# File lib/hollow/application.rb, line 124
def Application::get_resource(resource)
  if Module.const_defined?(resource)
    it = Object.const_get(resource)

    if it.is_a?(Class) &&
        it.class != Hollow::Resource &&
        it.is_a?(Hollow::Resource)
      return it
    else
      fail ResourceException.new resource
    end
  end
end

Public Instance Methods

handle_request(resource: nil, method: nil, data: {}) click to toggle source

Attempt to invoke a resource method with the given data.

@param [String, Symbol] resource

The case-sensitive name of a desired resource.

@param [String, Symbol] method

The case-sensitive resource method name to invoke.

@param [Hash] data

Any data which the resource may or may not use to handle the reqeust.

@raise [ResourceException]

If the indicated resource is not defined, is not a Class, is
{Hollow::Resource} itself, or is not a type of {Hollow::Resource}.

@raise [ResourceMethodException]

If the indicated resource exists and:
  1. The indicated method is not accessible or defined, or
  2. The method name is not included in `settings[:resource_methods]`.
# File lib/hollow/application.rb, line 103
def handle_request(resource: nil, method: nil, data: {})
  begin
    resource_class = Application::get_resource(resource.to_sym)
    handler = resource_class.get_instance
    method = method.to_sym.downcase
  rescue NoMethodError, NameError
    fail ResourceException.new resource
  end

  if @settings[:resource_methods].include?(method) &&
      handler.respond_to?(method)
    invoke_chain(resource_class, data, :before, method)
    response = handler.public_send(method, data)
    invoke_chain(resource_class, data, :after, method)
    return response
  else
    fail ResourceMethodException.new resource, method
  end
end

Private Instance Methods

invoke_chain(resource, request, chain, method) click to toggle source
# File lib/hollow/application.rb, line 138
def invoke_chain(resource, request, chain, method)
  chains = resource.class_variable_get(:@@chains)
  links = (chains[chain][:all] || []) + (chains[chain][method] || [])
  links.each { |chain_link| chain_link.call(request) }
end