Module: Fozzie::Sniff

Defined in:
lib/fozzie/sniff.rb

Instance Method Summary collapse

Instance Method Details

#_monitor(bucket_name = nil) ⇒ Object



8
9
10
11
# File 'lib/fozzie/sniff.rb', line 8

def _monitor(bucket_name = nil)
  @_monitor_flag = true
  @_bucket_name  = bucket_name
end

#_monitor_meth(target, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fozzie/sniff.rb', line 13

def _monitor_meth(target, &blk)
  return if @_monitor_flag.nil? || !@_monitor_flag

  @_monitor_flag = false
  bin            = @_bucket_name || [self.name.snakecase, target.to_s.snakecase]
  feature        = :monitor
  aliased_target = target.to_s.sub(/([?!=])$/, '')
  punctuation    = $1

  with    = "#{aliased_target}_with_#{feature}#{punctuation}"
  without = "#{aliased_target}_without_#{feature}#{punctuation}"

  blk.call(with, without, feature, bin)
end

#method_added(target) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fozzie/sniff.rb', line 28

def method_added(target)
  _monitor_meth(target) do |with, without, feature, bin|
    define_method(with) do |*args, &blk|
      S.time_for(bin) do
        args.empty? ? self.send(without, &blk) : self.send(without, *args, &blk)
      end
    end
    self.alias_method_chain(target, feature)
  end
end

#singleton_method_added(target) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/fozzie/sniff.rb', line 39

def singleton_method_added(target)
  _monitor_meth(target) do |with, without, feature, bin|
    define_singleton_method(with) do |*args, &blk|
      S.time_for(bin) do
        args.empty? ? send(without, &blk) : send(without, *args, &blk)
      end
    end
    self.singleton_class.class_eval { alias_method_chain target, feature }
  end
end