Module: FatFreeCRM::Callback

Defined in:
lib/fat_free_crm/callback.rb

Defined Under Namespace

Modules: Helper Classes: Base

Constant Summary collapse

@@classes =

Classes that inherit from FatFreeCRM::Callback::Base.

[]
@@responder =
{}

Class Method Summary collapse

Class Method Details

.add(klass) ⇒ Object

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




15
16
17
# File 'lib/fat_free_crm/callback.rb', line 15

def self.add(klass)
  @@classes << klass
end

.hook(method, caller, context = {}) ⇒ Object

Invokes the hook named :method and captures its output. The hook returns:

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

  • array with single item returned by the hook.

  • array with multiple items returned by the hook chain.




32
33
34
35
36
# File 'lib/fat_free_crm/callback.rb', line 32

def self.hook(method, caller, context = {})
  responder(method).map do |m|
    m.send(method, caller, context)
  end
end

.responder(method) ⇒ Object

Finds class instance that responds to given method.




24
25
26
# File 'lib/fat_free_crm/callback.rb', line 24

def self.responder(method)
  @@responder[method] ||= @@classes.map { |klass| klass.instance }.select { |instance| instance.respond_to?(method) }
end

.view_hook(hook, caller, context = {}) ⇒ Object

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.




55
56
57
58
59
60
61
62
63
# File 'lib/fat_free_crm/callback.rb', line 55

def self.view_hook(hook, caller, context = {})
  view_responder(hook).inject(Hash.new([])) do |response, instance|
    # 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) ⇒ Object

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




44
45
46
# File 'lib/fat_free_crm/callback.rb', line 44

def self.view_responder(method)
  @@responder[method] ||= @@classes.map { |klass| klass.instance }.select { |instance| instance.class.view_hooks[method] }
end