Module: Bubot

Defined in:
lib/bubot.rb,
lib/bubot/version.rb

Constant Summary collapse

VERSION =
"0.0.6"

Instance Method Summary collapse

Instance Method Details

#watch(method_name, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bubot.rb', line 5

def watch(method_name, options={})
  defaults = { timeout: 0 }
  defaults.merge!(options)
  define_method("#{method_name}_with_feature") do |*args, &block|
    start_time = Time.now

    method_return_value = send("#{method_name}_without_feature".to_sym, *args, &block)

    if (total_time = Time.now - start_time) > defaults[:timeout]
      if options[:with]
        options[:with].call(self, total_time, method_return_value)
      else
        yield(self, total_time, method_return_value)
      end
    end

    method_return_value
  end

  alias_method_chain_or_register(method_name)
end