Module: CallbackSupport::ClassMethods

Defined in:
lib/ruby_agent/callback_support.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_agent/callback_support.rb', line 22

def method_missing(method_name, *args, &block)
  if method_name.to_s.start_with?("on_event_")
    event_type = method_name.to_s.sub(/^on_event_/, "")
    @specific_event_callbacks ||= {}
    @specific_event_callbacks[event_type] ||= []
    @specific_event_callbacks[event_type] << (args.first || block)
  else
    super
  end
end

Instance Method Details

#on_event(method_name = nil, &block) ⇒ Object



7
8
9
10
# File 'lib/ruby_agent/callback_support.rb', line 7

def on_event(method_name = nil, &block)
  @on_event_callbacks ||= []
  @on_event_callbacks << (method_name || block)
end

#on_event_callbacksObject



12
13
14
15
16
17
18
19
20
# File 'lib/ruby_agent/callback_support.rb', line 12

def on_event_callbacks
  callbacks = []
  ancestors.each do |ancestor|
    if ancestor.instance_variable_defined?(:@on_event_callbacks)
      callbacks.concat(ancestor.instance_variable_get(:@on_event_callbacks))
    end
  end
  callbacks
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ruby_agent/callback_support.rb', line 33

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?("on_event_") || super
end

#specific_event_callbacks(event_type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_agent/callback_support.rb', line 37

def specific_event_callbacks(event_type)
  callbacks = []
  ancestors.each do |ancestor|
    if ancestor.instance_variable_defined?(:@specific_event_callbacks)
      specific_callbacks = ancestor.instance_variable_get(:@specific_event_callbacks)
      callbacks.concat(specific_callbacks[event_type]) if specific_callbacks[event_type]
    end
  end
  callbacks
end