Class: BatchKit::ResourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/batch-kit/resources.rb

Overview

Defines a manager for resource types, such as database connections etc. Resource types are registered with this class, which then adds acquisition methods to the ResourceHelper module. These acquisition methods add the acquired objects to a collection managed by the objects of the class that includes the ResourceHelper, and modify the returned resource objects so that they automatically de-register themselves if they are disposed of explicitly.

Class Method Summary collapse

Class Method Details

.disposal_method(rsrc) ⇒ Object

Returns an unbound method object that represents the method that should be called to dispose of rsrc.



20
21
22
23
# File 'lib/batch-kit/resources.rb', line 20

def disposal_method(rsrc)
    disp_mthd = resource_types[rsrc.class] || resource_types.find{ |rt, _| rt === rsrc }.last rescue nil
    disp_mthd or raise ArgumentError, "No registered resource class matches '#{rsrc.class}'"
end

.register(rsrc_cls, helper_mthd, options = {}, &body) ⇒ Object

Register a resource type for automated resource management.

Parameters:

  • rsrc_cls (Class)

    The class of resource to be managed. This must be the type of the object that will be returned when an instance of this resource is acquired.

  • helper_mthd (Symbol)

    The name of the resource acquisition helper method that should be added to the ResourceHelper module.

  • options (Hash) (defaults to: {})

    An options class.

Options Hash (options):

  • :acquisition_method (Symbol)

    For cases where an existing method can be called directly on the rsrc_cls to obtain a resource (rather than passing in a block containing resource acquisition steps), the name of that method. Defaults to :open.

  • :disposal_method (Symbol)

    The name of the method to be called on the resource to dispose of it. Defaults to :close.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/batch-kit/resources.rb', line 42

def register(rsrc_cls, helper_mthd, options = {}, &body)
    if ResourceHelper.method_defined?(helper_mthd)
        raise ArgumentError, "Resource acquisition method #{helper_mthd} is already registered"
    end
    unless body
        open_mthd = options.fetch(:acquisition_method, :open)
        body = lambda{ |*args| rsrc_cls.send(open_mthd, *args) }
    end
    disp_mthd = options.fetch(:disposal_method, :close)

    if rsrc_cls.method_defined?(disp_mthd)
        if (m = resource_types[rsrc_cls]) && m.name != disp_mthd
            raise ArgumentError, "Resource class #{rsrc_cls} has already been registered" +
                " with a different disposal method (##{m.name})"
        else
            resource_types[rsrc_cls] = rsrc_cls.instance_method(disp_mthd)
        end
    else
        raise ArgumentError, "No method named '#{disp_mthd}' is defined on #{rsrc_cls}"
    end

    # Define the helper method on the ResourceHelper module. This is
    # necessary (as opposed to just calling the block from the
    # acquisition methd) in order to ensure that self etc are set
    # correctly
    ResourceHelper.class_eval{ define_method(helper_mthd, &body) }

    # Now wrap an aspect around the method to handle the tracking of
    # resources acquired, and event notifications
    add_aspect(rsrc_cls, helper_mthd, disp_mthd)
    Events.publish(self, 'resource.registered', rsrc_cls, helper_mthd)
end