Class: Graft::Hook
- Inherits:
-
Object
- Object
- Graft::Hook
- Defined in:
- lib/graft/hook.rb
Constant Summary collapse
- DEFAULT_STRATEGY =
HookPoint::DEFAULT_STRATEGY
Instance Attribute Summary collapse
-
#point ⇒ Object
readonly
Returns the value of attribute point.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Class Method Summary collapse
- .[](hook_point, strategy = DEFAULT_STRATEGY) ⇒ Object
- .add(hook_point, strategy = DEFAULT_STRATEGY, &block) ⇒ Object
- .ignore ⇒ Object
Instance Method Summary collapse
- #add(&block) ⇒ Object
- #after(tag = nil, opts = {}, &block) ⇒ Object
- #append(tag = nil, opts = {}, &block) ⇒ Object
- #before(tag = nil, opts = {}, &block) ⇒ Object
- #callback_name(tag = nil) ⇒ Object
- #dependency? ⇒ Boolean
- #depends_on(&block) ⇒ Object
- #disable ⇒ Object
- #disabled? ⇒ Boolean
- #enable ⇒ Object
-
#initialize(hook_point, dependency_test = nil, strategy = DEFAULT_STRATEGY) ⇒ Hook
constructor
A new instance of Hook.
- #install ⇒ Object
- #uninstall ⇒ Object
- #unshift(tag = nil, opts = {}, &block) ⇒ Object
- #wrapper(hook) ⇒ Object
Constructor Details
#initialize(hook_point, dependency_test = nil, strategy = DEFAULT_STRATEGY) ⇒ Hook
Returns a new instance of Hook.
30 31 32 33 34 35 |
# File 'lib/graft/hook.rb', line 30 def initialize(hook_point, dependency_test = nil, strategy = DEFAULT_STRATEGY) @disabled = false @point = hook_point.is_a?(HookPoint) ? hook_point : HookPoint.new(hook_point, strategy) @dependency_test = dependency_test || proc { point.exist? } @stack = Stack.new end |
Instance Attribute Details
#point ⇒ Object (readonly)
Returns the value of attribute point.
28 29 30 |
# File 'lib/graft/hook.rb', line 28 def point @point end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
28 29 30 |
# File 'lib/graft/hook.rb', line 28 def stack @stack end |
Class Method Details
.[](hook_point, strategy = DEFAULT_STRATEGY) ⇒ Object
13 14 15 |
# File 'lib/graft/hook.rb', line 13 def self.[](hook_point, strategy = DEFAULT_STRATEGY) @hooks[hook_point] ||= new(hook_point, nil, strategy) end |
.add(hook_point, strategy = DEFAULT_STRATEGY, &block) ⇒ Object
17 18 19 |
# File 'lib/graft/hook.rb', line 17 def self.add(hook_point, strategy = DEFAULT_STRATEGY, &block) self[hook_point, strategy].add(&block) end |
.ignore ⇒ Object
21 22 23 24 25 26 |
# File 'lib/graft/hook.rb', line 21 def self.ignore Thread.current[:hook_entered] = true yield ensure Thread.current[:hook_entered] = false end |
Instance Method Details
#add(&block) ⇒ Object
43 44 45 |
# File 'lib/graft/hook.rb', line 43 def add(&block) tap { instance_eval(&block) } end |
#after(tag = nil, opts = {}, &block) ⇒ Object
63 64 65 |
# File 'lib/graft/hook.rb', line 63 def after(tag = nil, opts = {}, &block) # TODO end |
#append(tag = nil, opts = {}, &block) ⇒ Object
51 52 53 |
# File 'lib/graft/hook.rb', line 51 def append(tag = nil, opts = {}, &block) @stack << Callback.new(callback_name(tag), opts, &block) end |
#before(tag = nil, opts = {}, &block) ⇒ Object
59 60 61 |
# File 'lib/graft/hook.rb', line 59 def before(tag = nil, opts = {}, &block) # TODO end |
#callback_name(tag = nil) ⇒ Object
47 48 49 |
# File 'lib/graft/hook.rb', line 47 def callback_name(tag = nil) point.to_s << (tag ? ":#{tag}" : "") end |
#dependency? ⇒ Boolean
37 38 39 40 41 |
# File 'lib/graft/hook.rb', line 37 def dependency? return true if @dependency_test.nil? @dependency_test.call end |
#depends_on(&block) ⇒ Object
67 68 69 |
# File 'lib/graft/hook.rb', line 67 def depends_on(&block) @dependency_test = block end |
#disable ⇒ Object
75 76 77 |
# File 'lib/graft/hook.rb', line 75 def disable @disabled = true end |
#disabled? ⇒ Boolean
79 80 81 |
# File 'lib/graft/hook.rb', line 79 def disabled? @disabled end |
#enable ⇒ Object
71 72 73 |
# File 'lib/graft/hook.rb', line 71 def enable @disabled = false end |
#install ⇒ Object
83 84 85 86 87 |
# File 'lib/graft/hook.rb', line 83 def install return unless point.exist? point.install("hook", &Hook.wrapper(self)) end |
#uninstall ⇒ Object
89 90 91 92 93 |
# File 'lib/graft/hook.rb', line 89 def uninstall return unless point.exist? point.uninstall("hook") end |
#unshift(tag = nil, opts = {}, &block) ⇒ Object
55 56 57 |
# File 'lib/graft/hook.rb', line 55 def unshift(tag = nil, opts = {}, &block) @stack.unshift Callback.new(callback_name(tag), opts, &block) end |
#wrapper(hook) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/graft/hook.rb', line 97 def wrapper(hook) proc do |*args, &block| env = { self: self, args: args, block: block } supa = proc { |*args, &block| super(*args, &block) } mid = proc { |_, env| {return: supa.call(*env[:args], &env[:block])} } stack = hook.stack.dup stack << mid stack.call(env) end end |