Class: Guacamole::Callbacks::CallbackProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/guacamole/callbacks.rb

Overview

A proxy class around the callback class itself.

The sole reason for its existence is to specify multiple callback runs at once. The alternative would have been to nest the run_callbacks calls within the caller. It was decided to have bit more complex proxy class to hide those details from the caller.

Examples:

callbacks = Callbacks.callbacks_for(model)
callbacks.run_callbacks :save, :create do
  CakeCollection.create model
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callbacks) ⇒ CallbackProxy

Create a new proxy with the original callbacks class as input

Parameters:

  • callbacks (Callbacks)

    The original callback class to be executed



184
185
186
# File 'lib/guacamole/callbacks.rb', line 184

def initialize(callbacks)
  @callbacks = callbacks
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



179
180
181
# File 'lib/guacamole/callbacks.rb', line 179

def callbacks
  @callbacks
end

Instance Method Details

#run_callbacks(*callbacks_to_run) { ... } ⇒ Object

Runs the given kinds of callbacks

Parameters:

  • callbacks_to_run (Array<Symbol>)

    One or more kinds of callbacks to be run

Yields:

  • Will call the code block wrapped by the given callbacks



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/guacamole/callbacks.rb', line 192

def run_callbacks(*callbacks_to_run, &block)
  outer = callbacks_to_run.pop

  if callbacks_to_run.empty?
    @callbacks.run_callbacks(outer, &block)
  else
    @callbacks = run_callbacks(*callbacks_to_run) do
      @callbacks.run_callbacks(outer, &block)
    end
  end
end