Module: TheHelp::ProvidesCallbacks

Included in:
Service
Defined in:
lib/the_help/provides_callbacks.rb

Overview

Adds a callback DSL to including classes

If the including class defines a #logger instance method, a debug-level message will be logged indicating that the callback was invoked.

Examples:

class Foo
  attr_accessor :collaborator

  def do_something
    collaborator.do_some_other_thing(when_done: callback(:it_was_done))
    collaborator
      .do_some_other_thing(when_done: callback(:it_was_done_method))
  end

  callback(:it_was_done) do |some_arg:|
    puts "Yay! #{some_arg}"
  end

  def it_was_done_method(some_arg:)
    puts "In a method: #{some_arg}"
  end
  callback :it_was_done_method
end

class Bar
 def do_some_other_thing(when_done:)
   when_done.call('done by Bar')
 end
end

f = Foo.new
f.collaborator = Bar.new
f.do_something
# STDOUT: "Yay! done by Bar"
# STDOUT: "In a method: done by Bar"

Defined Under Namespace

Modules: ClassMethods Classes: CallbackNotDefinedError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



43
44
45
46
47
# File 'lib/the_help/provides_callbacks.rb', line 43

def self.included(other)
  other.class_eval do
    extend TheHelp::ProvidesCallbacks::ClassMethods
  end
end

Instance Method Details

#callback(callback_name) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/the_help/provides_callbacks.rb', line 49

def callback(callback_name)
  return method(callback_name) if _provides_callbacks_callback_defined?(
    callback_name
  )

  raise CallbackNotDefinedError,
        "The callback :#{callback_name} has not been defined."
end