Module: TheHelp::ProvidesCallbacks

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

Overview

Adds a callback DSL to including classes

class Foo

attr_accessor :collaborator

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

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

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”

Callbacks can be given to collaborating objects, but the actual methods are defined as private methods. This allows the object to control which other objects are able to invoke the callbacks (at least to the extent that Ruby lets you do so.)

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(other) ⇒ Object



38
39
40
41
42
43
# File 'lib/the_help/provides_callbacks.rb', line 38

def self.included(other)
  other.class_eval do
    extend TheHelp::ProvidesCallbacks::ClassMethods
    alias_method :callback, :method
  end
end