Module: FatFreeCRM::Callback
- Defined in:
- lib/fat_free_crm/callback.rb
Defined Under Namespace
Constant Summary collapse
- @@classes =
Classes that inherit from FatFreeCRM::Callback::Base.
[]
- @@responder =
Class instances that respond to (i.e. implement) hook methods.
{}
Class Method Summary collapse
-
.add(klass) ⇒ Object
Adds a class inherited from from FatFreeCRM::Callback::Base.
-
.hook(method, caller, context = {}) ⇒ Object
Invokes the hook named :method and captures its output.
-
.responder(method) ⇒ Object
Finds class instance that responds to given method.
-
.view_hook(hook, caller, context = {}) ⇒ Object
Invokes the view hook Proc stored under :hook and captures its output.
-
.view_responder(method) ⇒ Object
Find class instances that contain operations for the given view hook.
Class Method Details
.add(klass) ⇒ Object
Adds a class inherited from from FatFreeCRM::Callback::Base.
17 18 19 |
# File 'lib/fat_free_crm/callback.rb', line 17 def self.add(klass) @@classes << klass end |
.hook(method, caller, context = {}) ⇒ Object
Invokes the hook named :method and captures its output.
32 33 34 35 36 37 38 |
# 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) ⇒ Object
Finds class instance that responds to given method.
26 27 28 |
# 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 = {}) ⇒ 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.
57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
Find class instances that contain operations for the given view hook.
45 46 47 |
# 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 |