Class: ObjectMonitor

Inherits:
Object show all
Includes:
Observable
Defined in:
lib/object_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Observable

#add_observers, included, #list_observers, #notify_observer, #observer?

Constructor Details

#initialize(object, *observers) ⇒ ObjectMonitor

Returns a new instance of ObjectMonitor.



17
18
19
20
21
# File 'lib/object_monitor.rb', line 17

def initialize(object, *observers)
  @object = object
  add_observers(*observers)
  @nb_method = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/object_monitor.rb', line 23

def method_missing(meth, *args, &block)
  info = init_method_info(meth, *args, &block)
  start_method(info)
  result = nil
  begin
    result = @object.send(meth, *args, &block)
  rescue Exception => exc
    info[:stop_time] = Time.now
    info[:error] = exc
    stop_method(info)
    raise exc
  ensure
    @nb_method += 1
  end
  info[:stop_time] = Time.now
  info[:result] = result
  stop_method(info)
  result
end

Instance Attribute Details

#nb_methodObject (readonly)

Returns the value of attribute nb_method.



15
16
17
# File 'lib/object_monitor.rb', line 15

def nb_method
  @nb_method
end