Class: Hooked::Hook
- Inherits:
-
Object
- Object
- Hooked::Hook
- Defined in:
- lib/hooked/hook.rb
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#instead_pointcut ⇒ Object
Returns the value of attribute instead_pointcut.
-
#pointcut ⇒ Object
readonly
Returns the value of attribute pointcut.
Instance Method Summary collapse
- #add_aspect(*args, &block) ⇒ Object
- #build_chain ⇒ Object
- #call(*args, &block) ⇒ Object
-
#initialize(pointcut = nil, &block) ⇒ Hook
constructor
A new instance of Hook.
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
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
3 4 5 |
# File 'lib/hooked/hook.rb', line 3 def graph @graph end |
#instead_pointcut ⇒ Object
Returns the value of attribute instead_pointcut.
3 4 5 |
# File 'lib/hooked/hook.rb', line 3 def instead_pointcut @instead_pointcut end |
#pointcut ⇒ Object (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_chain ⇒ Object
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 |