Class: Hooked::Aspect

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns 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

#adviceObject (readonly)

Returns the value of attribute advice.



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

def advice
  @advice
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#pointcutObject

Returns the value of attribute pointcut.



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

def pointcut
  @pointcut
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#after?Boolean

Returns:

  • (Boolean)


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

def after?
  type == :after
end

#around?Boolean

Returns:

  • (Boolean)


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

def around?
  type == :around
end

#before?Boolean

Returns:

  • (Boolean)


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

def before?
  type == :before
end

#call(*args, &block) ⇒ Object



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