module FatFreeCRM::Callback

Public Class Methods

add(klass) click to toggle source

Adds a class inherited from from FatFreeCRM::Callback::Base.

# File lib/fat_free_crm/callback.rb, line 17
def self.add(klass)
  @@classes << klass
end
hook(method, caller, context = {}) click to toggle source

Invokes the hook named :method and captures its output.

# File lib/fat_free_crm/callback.rb, line 32
def self.hook(method, caller, context = {})
  str = "".html_safe
  responder(method).map do |m|
    str << m.send(method, caller, context) if m.respond_to?(method)
  end
  str
end
responder(method) click to toggle source

Finds class instance that responds to given method.

# File lib/fat_free_crm/callback.rb, line 26
def self.responder(method)
  @@responder[method] ||= @@classes.map(&:instance).select { |instance| instance.respond_to?(method) }
end
view_hook(hook, caller, context = {}) click to toggle source

Invokes the view hook Proc stored under :hook and captures its output.

> Instead of defining methods on the class, view hooks are

stored as Procs in a hash. This allows the same hook to be manipulated in
multiple ways from within a single Callback subclass.

The hook returns:

  • empty hash if no hook with this name was detected.

  • a hash of arrays containing Procs and positions to insert content.

# File lib/fat_free_crm/callback.rb, line 57
def self.view_hook(hook, caller, context = {})
  view_responder(hook).each_with_object(Hash.new([])) do |instance, response|
    # Process each operation within each view hook, storing the data in a hash.
    instance.class.view_hooks[hook].each do |op|
      response[op[:position]] += [op[:proc].call(caller, context)]
    end
    response
  end
end
view_responder(method) click to toggle source

Find class instances that contain operations for the given view hook.

# File lib/fat_free_crm/callback.rb, line 45
def self.view_responder(method)
  @@responder[method] ||= @@classes.map(&:instance).select { |instance| instance.class.view_hooks[method] }
end