Module: Hooked

Defined in:
lib/hooked.rb,
lib/hooked/hook.rb,
lib/hooked/graph.rb,
lib/hooked/aspect.rb,
lib/hooked/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Aspect, Graph, Hook

Constant Summary collapse

VERSION =
"0.3.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hookedObject (readonly)

Returns the value of attribute hooked.



7
8
9
# File 'lib/hooked.rb', line 7

def hooked
  @hooked
end

Class Method Details

.included(klass) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hooked.rb', line 45

def self.included(klass)
  return unless Class === klass
  
  klass.instance_variable_set :@instance_hooked, {}
  klass.extend ClassMethods
  
  class << klass
    alias_method :new_without_hooked, :new
    alias_method :new, :new_with_hooked
  end
end

Instance Method Details

#hook!(pointcut) ⇒ Object



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

def hook!(pointcut)
  @hooked ||= {}
  return if hooked[pointcut]
  
  hooked[pointcut] = Hooked::Hook.new method(pointcut)
  (class << self; self; end).class_eval do
    undef_method pointcut
    define_method pointcut do |*args, &block|
      hooked[pointcut].call *args, &block
    end
  end
end

#instead_of(pointcut, advice = nil, &block) ⇒ Object



16
17
18
19
# File 'lib/hooked.rb', line 16

def instead_of(pointcut, advice = nil, &block)
  hook! pointcut
  hooked[pointcut].instead_pointcut = (advice || block)
end

#unhook!(pointcut) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/hooked.rb', line 34

def unhook!(pointcut)
  return unless hooked && hooked[pointcut]
  
  p = hooked[pointcut].pointcut
  (class << self; self; end).class_eval do
    remove_method pointcut
    define_method pointcut, &p
  end
  hooked.delete pointcut
end