Module: Lockistics::Meterable::ClassMethods

Defined in:
lib/lockistics/meterable.rb

Constant Summary collapse

@@_metered_methods =
{}
@@_meter_all =
false

Instance Method Summary collapse

Instance Method Details

#meter(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lockistics/meterable.rb', line 20

def meter(*args)
  if args.last.kind_of?(Hash)
    options = args.pop
  else
    options = {}
  end
  options = {
    :prefix => self.name.downcase
  }.merge(options)

  if args.first.eql?(:all)
    @@_meter_all = true
    @@_meter_all_options = {:except => []}.merge(options)
  end

  Array(args).each do |meth_name|
    @@_metered_methods[meth_name] = {}
    @@_metered_methods[meth_name][:options] = options
  end
end

#meter_wrap(meth_name) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/lockistics/meterable.rb', line 11

def meter_wrap(meth_name)
  @@_metered_methods[meth_name][:method] = instance_method(meth_name)
  define_method(meth_name) do |*args, &block|
    Lockistics.meter("#{@@_metered_methods[meth_name][:options][:prefix]}_#{meth_name}") do
      @@_metered_methods[meth_name][:method].bind(self).call *args, &block
    end
  end
end

#method_added(meth_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/lockistics/meterable.rb', line 41

def method_added(meth_name)
  super
  if @@_metered_methods && @@_metered_methods[meth_name] && @@_metered_methods[meth_name][:method].nil?
    meter_wrap meth_name
  elsif @@_meter_all && !Array(@@_meter_all_options[:except]).include?(meth_name) && @@_metered_methods[meth_name].nil?
    @@_metered_methods[meth_name] = {}
    @@_metered_methods[meth_name][:options] = @@_meter_all_options
    meter_wrap meth_name
  end
end