Module: Cross::Talk::Listener::ClassMethods

Defined in:
lib/cross-talk/listener.rb

Instance Method Summary collapse

Instance Method Details

#define_hook!(method, &block) ⇒ Object



117
118
119
# File 'lib/cross-talk/listener.rb', line 117

def define_hook!(method, &block)
  define_method(method, &block)
end

#listen(klass, event, timing, &block) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/cross-talk/listener.rb', line 79

def listen(klass, event, timing, &block)
  event_name = "#{klass}##{event}:#{timing}"
  Cross::Talk.manager.notify('__new_hook:before', event_name)
  define_hook! event_name, &block
  registration_hooks << proc { |actor| Cross::Talk.manager.register(event_name, actor) }
  Cross::Talk.manager.notify('__new_hook:after', event_name)
end

#method_added(method) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cross-talk/listener.rb', line 105

def method_added(method)
  # if we're redefining a method, the lock is set to true, so bug out.
  return if @lock
  #don't notify hook methods
  return if method =~ /^.*#.*:.*$/
  # don't notify pseudoprivate methods
  return if method =~ /^__/
  @lock = true
  notify(method)
  @lock = false
end

#notify(method_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cross-talk/listener.rb', line 87

def notify(method_name)
  class_eval %{
    alias __old_#{method_name} #{method_name}

    def #{method_name}(*args, &block)
      __notify_event(#{method_name.inspect}, :before)
      result = __old_#{method_name}(*args, &block)
      __notify_event(#{method_name.inspect}, :after)
      result
    end
  }

  send(:private, method_name) if private_instance_methods.include?("__old_#{method_name}".to_sym)
  send(:protected, method_name) if protected_instance_methods.include?("__old_#{method_name}".to_sym)

  nil
end

#registration_hooksObject



75
76
77
# File 'lib/cross-talk/listener.rb', line 75

def registration_hooks
  @registration_hooks ||= []
end