Method: Guard::Dsl#callback

Defined in:
lib/guard/dsl.rb

#callback(*args) { ... } ⇒ Object

Defines a callback to execute arbitrary code before or after any of the start, stop, reload, run_all, run_on_changes, run_on_additions, run_on_modifications and run_on_removals plugin method.

Examples:

Add callback before the reload action.

callback(:reload_begin) { puts "Let's reload!" }

Add callback before the start and stop actions.


my_lambda = lambda do |plugin, event, *args|
  puts "Let's #{event} #{plugin} with #{args}!"
end

callback(my_lambda, [:start_begin, :start_end])

Parameters:

  • args (Array)

    the callback arguments

Yields:

  • a callback block



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/guard/dsl.rb', line 246

def callback(*args, &block)
  @plugin_options ||= nil
  fail "callback must be called within a guard block" unless @plugin_options

  block, events = if args.size > 1
                    # block must be the first argument in that case, the
                    # yielded block is ignored
                    args
                  else
                    [block, args[0]]
                  end
  @plugin_options[:callbacks] << { events: events, listener: block }
end