Class: Modesty::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/modesty/metric.rb,
lib/modesty/metric/base.rb,
lib/modesty/metric/data.rb,
lib/modesty/metric/builder.rb

Defined Under Namespace

Classes: Builder, Error

Constant Summary collapse

ATTRIBUTES =
[
  :description
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, options = {}) ⇒ Metric

Returns a new instance of Metric.



24
25
26
27
28
# File 'lib/modesty/metric/base.rb', line 24

def initialize(slug, options={})
  @slug = slug
  @parent = options[:parent]
  @experiment = options[:experiment]
end

Instance Attribute Details

#experimentObject (readonly)

Returns the value of attribute experiment.



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

def experiment
  @experiment
end

#parentObject (readonly)

Returns the value of attribute parent.



15
16
17
# File 'lib/modesty/metric/base.rb', line 15

def parent
  @parent
end

#slugObject (readonly)

Returns the value of attribute slug.



14
15
16
# File 'lib/modesty/metric/base.rb', line 14

def slug
  @slug
end

Instance Method Details

#/(sym) ⇒ Object



34
35
36
# File 'lib/modesty/metric/base.rb', line 34

def /(sym)
  Modesty.metrics[@slug/sym] || (raise NoMetricError, "Undefined metric :'#{@slug/sym}'")
end

#count(*dates) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/modesty/metric/data.rb', line 30

def count(*dates)
  date_or_range = parse_date_or_range(*dates)

  case date_or_range
  when Range
    if self.data.respond_to? :count_by_range
      self.data.count_range(date_or_range)
    else
      date_or_range.map do |date|
        self.data.count(date)
      end
    end
  when Date, :all
    self.data.count(date_or_range)
  end
end

#dataObject



4
5
6
# File 'lib/modesty/metric/data.rb', line 4

def data
  @data ||= (Modesty.data.class)::MetricData.new(self)
end

#experimentsObject

metrics should know what experiments use them, to enable smart tracking.



20
21
22
# File 'lib/modesty/metric/base.rb', line 20

def experiments
  @experiments ||= []
end

#inspectObject



30
31
32
# File 'lib/modesty/metric/base.rb', line 30

def inspect
  "#<Modesty::Metric[ #{self.slug.inspect} ]>"
end

#parse_date(date) ⇒ Object



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

def parse_date(date)
  if date.is_a? Symbol
    return date if date == :all
    Date.send(date)
  elsif date.nil?
    Date.today
  else
    date.to_date
  end
end

#parse_date_or_range(start = nil, fin = nil) ⇒ Object



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

def parse_date_or_range(start=nil,fin=nil)
  if fin
    parse_date(start)..parse_date(fin)
  elsif start.is_a?(Range)
    parse_date(start.first)..parse_date(start.last)
  else
    parse_date(start)
  end
end

#track!(count = 1, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/modesty/metric/data.rb', line 94

def track!(count=1, options={})
  if count.is_a? Hash
    options = count
    count = options[:count] || 1
  end

  with = options[:with] || {}

  with[:user] ||= Modesty.identity if Modesty.identity

  self.experiments.each do |exp|
    # only track the for the experiment group if
    # the user has previously hit the experiment
    identity_slug = exp.identity_for(self)
    identity = with[identity_slug]

    if !identity && exp.metric_contexts.has_key?(self.slug)
      raise IdentityError, <<-msg.squish
        #{exp.inspect} requires #{self.inspect} to be tracked
        with #{identity_slug.to_s.singularize.to_sym.inspect}.

        It was tracked :with => #{with.inspect}
      msg
    end

    if identity
      alt = exp.data.get_cached_alternative(identity)
      if alt
        (self/(exp.slug/alt)).data.track!(count, with)
      end
    end
  end

  self.data.track!(count, with)
  @parent.track!(count, :with => with) if @parent
rescue Datastore::ConnectionError => e
  Modesty.handle_error(e)
end