Class: Hooked::Hook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointcut = nil, &block) ⇒ Hook

Returns a new instance of Hook.



5
6
7
8
9
10
11
# File 'lib/hooked/hook.rb', line 5

def initialize(pointcut = nil, &block)
  if (pointcut && block) || (!pointcut && !block)
    raise ArgumentError, "Pass either an argument or a block"
  end

  @pointcut, @graph = (pointcut || block), Graph.new
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



3
4
5
# File 'lib/hooked/hook.rb', line 3

def graph
  @graph
end

#instead_pointcutObject

Returns the value of attribute instead_pointcut.



3
4
5
# File 'lib/hooked/hook.rb', line 3

def instead_pointcut
  @instead_pointcut
end

#pointcutObject (readonly)

Returns the value of attribute pointcut.



3
4
5
# File 'lib/hooked/hook.rb', line 3

def pointcut
  @pointcut
end

Instance Method Details

#add_aspect(*args, &block) ⇒ Object



13
14
15
# File 'lib/hooked/hook.rb', line 13

def add_aspect(*args, &block)
  graph << Aspect.new(*args, &block)
end

#build_chainObject



27
28
29
30
31
32
33
# File 'lib/hooked/hook.rb', line 27

def build_chain
pc = instead_pointcut || pointcut
  graph.sort
  graph.output.reverse.inject(pc) do |pointcut, aspect|
    aspect.pointcut = pointcut; aspect
  end
end

#call(*args, &block) ⇒ Object



22
23
24
25
# File 'lib/hooked/hook.rb', line 22

def call(*args, &block)
  @chain = build_chain if graph.changed? || !@chain
  @chain.call *args, &block
end