Module: Metrify::ClassMethods

Defined in:
lib/metrify.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/metrify.rb', line 80

def method_missing(method, *args, &block)
  stat_names.each do |name|
    if (name + NAME == method.to_s)
      return display_name(name)
    elsif (CALC + name == method.to_s)
      new_meth = method.to_s[CALC.length,method.to_s.length]
      raise MetrifyInclusionError, "Base class must implement method: #{new_meth}." if !self.respond_to?(new_meth) 
      return self.send(new_meth, *args, &block)
    end
  end
  super
end

Instance Method Details

#acts_as_metrify(file = self.name.underscore + '_metrify.yml', test = false) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/metrify.rb', line 38

def acts_as_metrify(file = self.name.underscore + '_metrify.yml', test = false)
  serialize :stat_hash, Hash
  send :include, InstanceMethods
  if test
    self.metrify_data = YAML::load_file(file) 
  else
    self.metrify_data = YAML.load_file(File.join(RAILS_ROOT, 'config', file))
  end
end

#config_val(stat_name, val) ⇒ Object



76
77
78
# File 'lib/metrify.rb', line 76

def config_val(stat_name, val)
  metrify_data['stats'][stat_name] == nil ? nil : metrify_data['stats'][stat_name][val]
end

#display_name(stat_name) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/metrify.rb', line 67

def display_name(stat_name)
  configured_name = config_val(stat_name, 'display_name')
  return configured_name if configured_name
  s = ""
  name = stat_name.split('_')
  name.each {|word| s << ' '<< word.capitalize}
  s
end

#filtersObject



48
49
50
# File 'lib/metrify.rb', line 48

def filters
  metrify_data['filters']
end

#find_stats_for(end_time, hours) ⇒ Object



147
148
149
150
151
# File 'lib/metrify.rb', line 147

def find_stats_for(end_time, hours)
  end_time = floor_hour(end_time)
  s = lookup(end_time, hours)
  s ||= generate(end_time, hours)
end

#historical_values(end_time, history_length, unit = DEFAULT_UNIT) ⇒ Object

returns an array of defined stats, each containing an array of values over time



138
139
140
141
142
143
144
145
# File 'lib/metrify.rb', line 138

def historical_values(end_time, history_length, unit = DEFAULT_UNIT)
  unit = :hour if (1.send(unit) / 1.hours) < 1
  finish_time = unit == :hour ? floor_hour(end_time) : end_time.midnight
  hours = 1.send(unit) / 1.hours
  (0..(history_length-1)).map{|i| finish_time - i.send(unit)}.reverse.map do |hour|
    find_stats_for(hour, hours)
  end
end

#preferred_stat_orderObject



93
94
95
# File 'lib/metrify.rb', line 93

def preferred_stat_order
  metrify_data['stat_order']
end

#show_variance(stat_name) ⇒ Object

if stat should also have % +/- over previous time period



63
64
65
# File 'lib/metrify.rb', line 63

def show_variance(stat_name)
  config_val(stat_name, 'show_variance')
end

#sorted_stat_names(stat_names = stat_names) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/metrify.rb', line 97

def sorted_stat_names(stat_names = stat_names)
  if preferred_stat_order
    final_list = []
    preferred_stat_order.each do |o_stat|
      final_list << o_stat if stat_names.include?(o_stat)
    end
    final_list
  else
    #sort alphabetically
    stat_names.sort
  end
end

#stat_names(names = nil, my_filters = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/metrify.rb', line 110

def stat_names(names = nil, my_filters = nil)
  #filters = ['type' => ['numbers', 'letters'], 'furriness' => ['furry', 'not_furry']]
  if my_filters
    col_names = names || metrify_data['stats'].keys
    final_col_names = col_names.clone
    my_filters.each do |filter_type, filter_subtypes|
      filter_col_names = []
      filters.keys.each do |filter_type_from_config|
        if (filter_type_from_config == filter_type)
          filter_subtypes.each do |subtype|
            filters[filter_type_from_config][subtype]['set'].each do |col|
              filter_col_names << col
            end
          end
        end
      end
      col_names.each do |ycol|
          final_col_names.delete(ycol.to_s) if !filter_col_names.include?(ycol) 
      end
    end
    final_col_names
  else
    names || metrify_data['stats'].keys
  end
  
end

#value_precision(stat_name) ⇒ Object

int



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

def value_precision(stat_name)
  config_val(stat_name, 'precision')
end

#value_type(stat_name) ⇒ Object

currency



53
54
55
# File 'lib/metrify.rb', line 53

def value_type(stat_name)
  config_val(stat_name, 'value_type')
end