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 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.
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.
30 31 32 33 34 35 36 |
# File 'lib/fat_free_crm/callback.rb', line 30 def self.hook(method, caller, context = {}) str = "" responder(method).map do |m| str << m.send(method, caller, context) end str 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 |