Class: Modesty::Metric
- Inherits:
-
Object
- Object
- Modesty::Metric
- Defined in:
- lib/modesty/metric.rb,
lib/modesty/metric/base.rb,
lib/modesty/metric/data.rb,
lib/modesty/metric/builder.rb
Defined Under Namespace
Constant Summary collapse
- ATTRIBUTES =
[ :description ]
Instance Attribute Summary collapse
-
#experiment ⇒ Object
readonly
Returns the value of attribute experiment.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Instance Method Summary collapse
- #/(sym) ⇒ Object
- #count(*dates) ⇒ Object
- #data ⇒ Object
-
#experiments ⇒ Object
metrics should know what experiments use them, to enable smart tracking.
-
#initialize(slug, options = {}) ⇒ Metric
constructor
A new instance of Metric.
- #inspect ⇒ Object
- #parse_date(date) ⇒ Object
- #parse_date_or_range(start = nil, fin = nil) ⇒ Object
- #track!(count = 1, options = {}) ⇒ Object
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, ={}) @slug = slug @parent = [:parent] @experiment = [:experiment] end |
Instance Attribute Details
#experiment ⇒ Object (readonly)
Returns the value of attribute experiment.
16 17 18 |
# File 'lib/modesty/metric/base.rb', line 16 def experiment @experiment end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
15 16 17 |
# File 'lib/modesty/metric/base.rb', line 15 def parent @parent end |
#slug ⇒ Object (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 |
#data ⇒ Object
4 5 6 |
# File 'lib/modesty/metric/data.rb', line 4 def data @data ||= (Modesty.data.class)::MetricData.new(self) end |
#experiments ⇒ Object
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 |
#inspect ⇒ Object
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, ={}) if count.is_a? Hash = count count = [:count] || 1 end with = [: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 |