Module: Modesty::MetricMethods

Included in:
API
Defined in:
lib/modesty/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#metricsObject



9
10
11
12
13
14
15
# File 'lib/modesty/metric.rb', line 9

def metrics
  @metrics ||= Hash.new do |h, k|
    raise Metric::Error, <<-msg.squish
      Unrecognized metric #{k.inspect}
    msg
  end
end

Instance Method Details

#add_metric(metric) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/modesty/metric.rb', line 21

def add_metric(metric)
  raise Metric::Error, <<-msg if self.metrics.include? metric.slug
    Metric #{metric.slug.inspect} already defined!
  msg
  self.metrics[metric.slug] = metric
end

#metrics_starting_with(name) ⇒ Object



17
18
19
# File 'lib/modesty/metric.rb', line 17

def metrics_starting_with(name)
  self.metrics.select{|k, v| k.to_s.starts_with?(name)}
end

#new_metric(slug, parent = nil, options = {}) {|Metric::Builder.new(metric)| ... } ⇒ Object

Yields:



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/modesty/metric.rb', line 28

def new_metric(slug, parent=nil, options={}, &block)
  if parent.is_a? Hash
    options=parent
  else
    options[:parent] = parent
  end

  metric = Metric.new(slug, options)
  yield Metric::Builder.new(metric) if block_given?
  add_metric(metric)
  metric
end

#track!(name, *args) ⇒ Object

Tracking



42
43
44
45
46
# File 'lib/modesty/metric.rb', line 42

def track!(name, *args)
  self.metrics[name.to_sym].track! *args
rescue Modesty::Metric::Error
  # Fail silently in the event that a metric is not found.
end