Module: EagleClaw::Callbacks

Included in:
Scraper
Defined in:
lib/eagleclaw/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#register(context, : method_name) ⇒ Object #register(context, &block) ⇒ Object

Register a callback for a given context.

Examples:

Registering a method as a callback

class MyCLS
  include Callbacks

  register(:preprocessing, :setup_db)

  def setup_db
    @database = DB.connect("username", "password")
  end
end

Registering a block as a callback

class MyCLS
  include Callbacks

  register :preprocessing do
    @database = DB.connect("username", "password")
  end
end

Overloads:

  • #register(context, : method_name) ⇒ Object

    Register a method as a callback.

  • #register(context, &block) ⇒ Object

    Register a block as a callback.

Parameters:

  • context (Object)

    an object (such as a symbol or list of symbols) which refers to a certain callback context.

  • meth (optional, Symbol) (defaults to: nil)

    the name of the callback method.



36
37
38
39
# File 'lib/eagleclaw/callbacks.rb', line 36

def register(context, meth = nil, &block)
  callback = block_given? ? block : meth
  ((@callbacks ||= {})[context] ||= []) << callback
end

#run_callbacks(context, recipient = self) ⇒ nil

Run the callbacks for a given context.

Parameters:

  • context
  • recipient (optional, Object) (defaults to: self)

    the object to run methods/procs on.

Returns:

  • (nil)


47
48
49
50
# File 'lib/eagleclaw/callbacks.rb', line 47

def run_callbacks(context, recipient = self)
  (@callbacks[context] || []).each { |callback| run_proc(callback, recipient) }
  nil
end