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.




27
28
29
# File 'lib/fat_free_crm/callback.rb', line 27

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.




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

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.




36
37
38
# File 'lib/fat_free_crm/callback.rb', line 36

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.




67
68
69
70
71
72
73
74
75
# File 'lib/fat_free_crm/callback.rb', line 67

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.




56
57
58
# File 'lib/fat_free_crm/callback.rb', line 56

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