Class: Hooked::Aspect
- Inherits:
-
Object
- Object
- Hooked::Aspect
- Defined in:
- lib/hooked/aspect.rb
Instance Attribute Summary collapse
-
#advice ⇒ Object
readonly
Returns the value of attribute advice.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#pointcut ⇒ Object
Returns the value of attribute pointcut.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #after? ⇒ Boolean
- #around? ⇒ Boolean
- #before? ⇒ Boolean
- #call(*args, &block) ⇒ Object
-
#initialize(type, advice = nil, dependencies = {}, &block) ⇒ Aspect
constructor
A new instance of Aspect.
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
#advice ⇒ Object (readonly)
Returns the value of attribute advice.
3 4 5 |
# File 'lib/hooked/aspect.rb', line 3 def advice @advice end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
3 4 5 |
# File 'lib/hooked/aspect.rb', line 3 def dependencies @dependencies end |
#pointcut ⇒ Object
Returns the value of attribute pointcut.
4 5 6 |
# File 'lib/hooked/aspect.rb', line 4 def pointcut @pointcut end |
#type ⇒ Object (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
37 38 39 |
# File 'lib/hooked/aspect.rb', line 37 def after? type == :after end |
#around? ⇒ Boolean
41 42 43 |
# File 'lib/hooked/aspect.rb', line 41 def around? type == :around end |
#before? ⇒ 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 |