Class: Graft::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/graft/hook.rb

Constant Summary collapse

DEFAULT_STRATEGY =
HookPoint::DEFAULT_STRATEGY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pointObject (readonly)

Returns the value of attribute point.



28
29
30
# File 'lib/graft/hook.rb', line 28

def point
  @point
end

#stackObject (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

.ignoreObject



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

Returns:

  • (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

#disableObject



75
76
77
# File 'lib/graft/hook.rb', line 75

def disable
  @disabled = true
end

#disabled?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/graft/hook.rb', line 79

def disabled?
  @disabled
end

#enableObject



71
72
73
# File 'lib/graft/hook.rb', line 71

def enable
  @disabled = false
end

#installObject



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

#uninstallObject



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