Class: Hooked::Aspect

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Aspect) initialize(type, advice = nil, dependencies = {}, &block)

A new instance of Aspect



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hooked/aspect.rb', line 6

def initialize(type, advice = nil, dependencies = {}, &block)
  if (advice && block) || (!advice && !block)
    raise ArgumentError, "Pass either an advice argument or a block"
  end
  
  if Hash === advice
    advice, dependencies = nil, advice
  end

  [:before, :after].each {|d| dependencies[d] ||= [] }
  @type, @advice, @dependencies = type, (advice || block), dependencies
end

Instance Attribute Details

- (Object) advice (readonly)

Returns the value of attribute advice



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

def advice
  @advice
end

- (Object) dependencies (readonly)

Returns the value of attribute dependencies



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

def dependencies
  @dependencies
end

- (Object) pointcut

Returns the value of attribute pointcut



4
5
6
# File 'lib/hooked/aspect.rb', line 4

def pointcut
  @pointcut
end

- (Object) type (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

- (Boolean) after?

Returns:

  • (Boolean)


37
38
39
# File 'lib/hooked/aspect.rb', line 37

def after?
  type == :after
end

- (Boolean) around?

Returns:

  • (Boolean)


41
42
43
# File 'lib/hooked/aspect.rb', line 41

def around?
  type == :around
end

- (Boolean) before?

Returns:

  • (Boolean)


33
34
35
# File 'lib/hooked/aspect.rb', line 33

def before?
  type == :before
end

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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hooked/aspect.rb', line 19

def call(*args, &block)
  advice.call(args, &block) if before?

  result = if around?
    advice.call pointcut, *args, &block
  else
    pointcut.call *args, &block
  end
  
  advice.call result if after?
  
  result
end