Class: Hooked::Hook

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Hook) initialize(pointcut = nil, &block)

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

- (Object) graph (readonly)

Returns the value of attribute graph



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

def graph
  @graph
end

- (Object) instead_pointcut

Returns the value of attribute instead_pointcut



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

def instead_pointcut
  @instead_pointcut
end

- (Object) pointcut (readonly)

Returns the value of attribute pointcut



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

def pointcut
  @pointcut
end

Instance Method Details

- (Object) add_aspect(*args, &block)



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

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

- (Object) build_chain



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

- (Object) call(*args, &block)



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