Class: ActiveMetric::Stat

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/active_metric/stat.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property, *args) ⇒ Stat



13
14
15
16
# File 'lib/active_metric/stat.rb', line 13

def initialize(property, *args)
  super(*args)
  self.property = property
end

Class Method Details

.access_name(property = nil) ⇒ Object



33
34
35
36
37
# File 'lib/active_metric/stat.rb', line 33

def self.access_name(property = nil)
  title = name.split("::").last.underscore
  title += "_#{property}" if property
  title.to_sym
end

.approximationObject



39
40
41
# File 'lib/active_metric/stat.rb', line 39

def self.approximation
  nil
end

.class_for(stat) ⇒ Object



29
30
31
# File 'lib/active_metric/stat.rb', line 29

def self.class_for(stat)
  eval(stat.to_s.classify)
end

.create_custom_stat(name_of_stat, value_type, default, calculate_block) ⇒ Object

TODO Make custom classes namespaced to where they are being defined



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_metric/stat.rb', line 44

def self.create_custom_stat(name_of_stat, value_type, default, calculate_block)
  class_name = name_of_stat.to_s.camelcase
  if ActiveMetric.const_defined?(class_name)
    ActiveMetric.logger.warn "#{class_name} is already defined. It won't be defined again."
    return ActiveMetric.const_get(class_name)
  end
  klass = Class.new(Custom) do
    define_method(:calculate, calculate_block)
  end
  klass.send(:field, :value, :type => value_type, :default => default)
  ActiveMetric.const_set(class_name, klass)
  return klass
end

Instance Method Details

#access_nameObject



18
19
20
# File 'lib/active_metric/stat.rb', line 18

def access_name
  self.class.access_name(property)
end

#calculate(measurement) ⇒ Object



22
23
24
# File 'lib/active_metric/stat.rb', line 22

def calculate(measurement)
  raise CannotInstantiateBaseStat
end

#completeObject



26
27
# File 'lib/active_metric/stat.rb', line 26

def complete
end

#property_from(measurement) ⇒ Object



62
63
64
65
# File 'lib/active_metric/stat.rb', line 62

def property_from(measurement)
  return nil unless measurement
  measurement.send(self.property)
end

#subjectObject



58
59
60
# File 'lib/active_metric/stat.rb', line 58

def subject
  self.calculable.samplable
end