module TheHelp::ProvidesCallbacks

Adds a callback DSL to including classes

@example

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"

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

Public Class Methods

included(other) click to toggle source
# File lib/the_help/provides_callbacks.rb, line 43
def self.included(other)
  other.class_eval do
    extend TheHelp::ProvidesCallbacks::ClassMethods
  end
end

Public Instance Methods

callback(callback_name) click to toggle source
# 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

Private Instance Methods

_provides_callbacks_callback_defined?(callback_name) click to toggle source
# File lib/the_help/provides_callbacks.rb, line 60
def _provides_callbacks_callback_defined?(callback_name)
  self.class.send(:_provides_callbacks_callback_defined?, callback_name)
end