Module: Bubot::ClassMethods

Defined in:
lib/bubot.rb

Instance Method Summary collapse

Instance Method Details

#watch(method_name, timeout: 0, with: nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bubot.rb', line 12

def watch(method_name, timeout: 0, with: nil, &block)
  interceptor = const_get "#{self.name}Interceptor"
  interceptor.module_eval do
    past_time_block = with || (block if block_given?)

    define_method(method_name) do |*args, &block|
      start_time = Time.now

      method_return_value = super(*args, &block)

      if (total_time = Time.now - start_time) >= timeout
        past_time_block.call(self, total_time, method_return_value)
      end

      method_return_value
    end
  end
end