Module: Bubot::ClassMethods

Defined in:
lib/bubot.rb

Instance Method Summary collapse

Instance Method Details

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



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

def watch(method_name, timeout: 0, with: nil, &block)
  define_callbacks method_name

  past_time_block = with || (block if block_given?)

  define_method("#{method_name}_with_bubot") do |*args, &block|
    run_callbacks method_name do
      send("#{method_name}_without_bubot".to_sym, *args, &block)
    end
  end

  alias_method_chain_or_register_for_chaining method_name

  set_callback method_name, :around, ->(r, &block) do
    start_time = Time.now

    method_return_value = block.call

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