Module: Hyperactive::Hooker::Pimp::ClassMethods

Defined in:
lib/hyperactive/hooker.rb

Overview

The class methods of a Pimp.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_hooksObject (readonly)

Return our create_hooks, which can then be treated as any old Array.

These must be callable objects with an arity of 1 that will be sent the instance about to be created (initial insertion into the database system) that take a block argument.

The block argument will be a Proc that actually injects the instance into the database system.

Use this to preprocess, validate and/or postprocess your instances upon creation.



136
137
138
# File 'lib/hyperactive/hooker.rb', line 136

def create_hooks
  @create_hooks
end

#destroy_hooksObject (readonly)

Return our destroy_hooks, which can then be treated as any old Array.

These must be callable objects with an arity of 1 that will be sent the instance about to be destroyed (removal from the database system) that take a block argument.

The block argument will be a Proc that actually removes the instance from the database system.

Use this to preprocess, validate and/or postprocess your instances upon destruction.



151
152
153
# File 'lib/hyperactive/hooker.rb', line 151

def destroy_hooks
  @destroy_hooks
end

#load_hooksObject (readonly)

Return our load_hooks, which can then be treated as any old Array.

These must be callable objects with an arity of 1 that will be sent the instance about to be loaded (insertion into the live hash of the database in question) that take a block argument.

The block argument will be a Proc that actually puts the instance into the live hash.

Use this to preprocess, validate and/or postprocess your instances upon loading.



166
167
168
# File 'lib/hyperactive/hooker.rb', line 166

def load_hooks
  @load_hooks
end

#save_hooksObject (readonly)

Return our save_hooks, which can then be treated as any old Array.

These must be callable objects with an arity of 1 that will be sent [the old version, the new version] of the instance about to be saved (storage into the database system) along with a block argument.

The block argument will be a Proc that actually saves the instance into the database system.

Use this to preprocess, validate and/or postprocess your instances upon saving.



182
183
184
# File 'lib/hyperactive/hooker.rb', line 182

def save_hooks
  @save_hooks
end

Instance Method Details

#inherited(subclass) ⇒ Object

Upon inheritance, our subclass will be given copies of our hooks.



116
117
118
119
120
121
# File 'lib/hyperactive/hooker.rb', line 116

def inherited(subclass)
  subclass.instance_variable_set(:@create_hooks, @create_hooks.clone)
  subclass.instance_variable_set(:@destroy_hooks, @destroy_hooks.clone)
  subclass.instance_variable_set(:@save_hooks, @save_hooks.clone)
  subclass.instance_variable_set(:@load_hooks, @load_hooks.clone)
end

#with_hooks(options = {}, &block) ⇒ Object

Will wrap a block within nested calls to given hooks.

Parameters:

  • :instance: An instance to send methods to, defaults to self

  • :arguments: Extra arguments to send to whatever we call, defaults to self

  • :hooks: An Array of hook objects. They can be either Strings or Symbols, or whatever else can be given to respond_to?, or they can be anything that respond_to?(:call). If the hook is something that respond_to?(:call) it will be called and given the the :instance and *:arguments and the provided block. Else the :instance.respond_to?(the given hook) must be true, and that :instance will be sent the given hook along with the :arguments and a block that does the rest of the hooks.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hyperactive/hooker.rb', line 89

def with_hooks(options = {}, &block)
  instance = options[:instance] || self
  arguments = options[:arguments] || []
  hooks = options[:hooks] || []

  if hooks.empty?
    yield
  else
    first = hooks.first
    rest = hooks[1..-1]
    if first.respond_to?(:call)
      first.call(instance, *arguments) do
        with_hooks(:instance => instance, :hooks => rest, :arguments => arguments, &block)
      end
    elsif instance.respond_to?(first)
      instance.send(first, *arguments) do
        with_hooks(:instance => instance, :hooks => rest, :arguments => arguments, &block)
      end
    else
      raise "You can only send in hooks that are Symbols in the given instance or call'able objects"
    end
  end
end