metrify 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -112,12 +112,10 @@ Example Controller:
112
112
 
113
113
  class DashboardController < ApplicationController
114
114
  include MetrifyController
115
-
116
- private
117
- def metrified_class
118
- @metrified_class || @metrified_class = MetricsClass.new
115
+
116
+ def metrify_model
117
+ Dashboard # Dashboard is the class using the Metrify module
119
118
  end
120
-
121
119
  end
122
120
 
123
121
  Example View for graph:
@@ -1,6 +1,9 @@
1
1
  module MetrifyController
2
2
 
3
3
  def self.included(base)
4
+ base.class_eval do
5
+ before_filter :set_metrify_class
6
+ end
4
7
  base.extend ClassMethods
5
8
  base.send :include, InstanceMethods
6
9
  base.helper MetrifyHelper
@@ -8,16 +11,19 @@ module MetrifyController
8
11
 
9
12
  module InstanceMethods
10
13
  def index
11
- @metrify = metrified_class
12
14
  setup_historical_stats
13
- @stat_names = metrified_class.stat_names
15
+ @stat_names = @metrified_class.stat_names
14
16
  @historical_site_stats.reverse!
15
17
  end
18
+
19
+ def set_metrify_class
20
+ @metrified_class = metrify_model
21
+ end
16
22
 
17
23
  def setup_historical_stats
18
24
  @unit = unit
19
25
  @number_of_stats = number_of_stats
20
- @historical_site_stats = metrified_class.historical_values(Time.now.beginning_of_week, number_of_stats, unit)
26
+ @historical_site_stats = @metrified_class.historical_values(Time.now.beginning_of_week, number_of_stats, unit)
21
27
  end
22
28
 
23
29
  def number_of_stats
@@ -35,19 +41,18 @@ module MetrifyController
35
41
  end
36
42
 
37
43
  def parsed_stat_names
38
- !params[:stat_names].blank? ? params[:stat_names].split(',') : metrified_class.stat_names
44
+ !params[:stat_names].blank? ? params[:stat_names].split(',') : @metrified_class.stat_names
39
45
  end
40
46
 
41
47
  # chart_data.json?filters[type][]=letters&filters[type][]=animals&filters[furriness][]=not_furry
42
48
  def chart_data
43
- @metrify = metrified_class
44
- @stat_names = metrified_class.stat_names(parsed_stat_names, params[:filters])
49
+ @stat_names = @metrified_class.stat_names(parsed_stat_names, params[:filters])
45
50
  @unit = params[:unit] || unit
46
51
 
47
52
  @number_of_stats = params[:number_of_stats] || number_of_stats
48
- @historical_site_stats = metrified_class.historical_values(Time.now.beginning_of_week, number_of_stats, unit)
53
+ @historical_site_stats = @metrified_class.historical_values(Time.now.beginning_of_week, number_of_stats, unit)
49
54
 
50
- json = @stat_names.map{|s| {:name => @template.pretty_col_name(s, @metrify),
55
+ json = @stat_names.map{|s| {:name => @metrified_class.display_name(s),
51
56
  :pointInterval => (1.send(@unit) * 1000),
52
57
  :pointStart => (@number_of_stats.send(@unit).ago.to_i * 1000),
53
58
  :data => @historical_site_stats.map{|h| h.send(s)}}}
@@ -55,7 +60,7 @@ module MetrifyController
55
60
  setup_historical_stats
56
61
  respond_to do |format|
57
62
  format.json {
58
- render :layout => false , :json => @stat_names.map{|s| {:name => @template.pretty_col_name(s, @metrify),
63
+ render :layout => false , :json => @stat_names.map{|s| {:name => @metrified_class.display_name(s),
59
64
  :pointInterval => (1.send(@unit) * 1000),
60
65
  :pointStart => (@number_of_stats.send(@unit).ago.to_i * 1000),
61
66
  :data => @template.get_stat_arr(s, @historical_site_stats)}}.to_json
@@ -64,18 +69,19 @@ module MetrifyController
64
69
 
65
70
  end
66
71
 
72
+ # def set_metrify_model; end
73
+
67
74
  private
68
75
 
69
76
  def prepare_for_graph
70
- @metrify = metrified_class
71
77
  setup_historical_stats
72
- set_classname
73
78
  end
74
79
 
75
80
  end
76
81
 
77
82
 
78
83
  module ClassMethods
84
+
79
85
  end
80
86
  end
81
87
 
@@ -1,14 +1,5 @@
1
1
  module MetrifyHelper
2
2
 
3
- def pretty_col_name(name, metrify)
4
- configured_name = metrify.send(name + "_name")
5
- return configured_name if configured_name
6
- s = ""
7
- name = name.split('_')
8
- name.each {|word| s << ' '<< word.capitalize}
9
- s
10
- end
11
-
12
3
  def get_stat_arr(stat, historical_stats = @historical_site_stats)
13
4
  stat_over_time = []
14
5
  historical_stats.each do |s|
@@ -19,19 +10,19 @@ module MetrifyHelper
19
10
  stat_over_time
20
11
  end
21
12
 
22
- def sorted_stat_names(metrify)
23
- metrify.sort_stat_names(@stat_names)
13
+ def sorted_stat_names
14
+ @metrified_class.sort_stat_names(@stat_names)
24
15
  end
25
16
 
26
- def print_stat_value(stat, stat_name, metrify, previous_stat = nil)
17
+ def print_stat_value(stat, stat_name, previous_stat = nil)
27
18
  val = stat.send(stat_name)
28
- val = number_with_precision(val, :precision => metrify.value_precision(stat_name)) if metrify.value_precision(stat_name)
29
- if metrify.value_type(stat_name) == "currency"
19
+ val = number_with_precision(val, :precision => @metrified_class.value_precision(stat_name)) if @metrified_class.value_precision(stat_name)
20
+ if @metrified_class.value_type(stat_name) == "currency"
30
21
  str = number_to_currency(val)
31
22
  else
32
23
  str = val.to_s
33
24
  end
34
- str += colorized_percent_diff(previous_stat.send(stat_name).to_f, stat.send(stat_name).to_f) if metrify.show_variance(stat_name) && previous_stat && previous_stat.send(stat_name) != 0 && stat.send(stat_name) != 0 && previous_stat.send(stat_name) != stat.send(stat_name)
25
+ str += colorized_percent_diff(previous_stat.send(stat_name).to_f, stat.send(stat_name).to_f) if @metrified_class.show_variance(stat_name) && previous_stat && previous_stat.send(stat_name) != 0 && stat.send(stat_name) != 0 && previous_stat.send(stat_name) != stat.send(stat_name)
35
26
  str
36
27
  end
37
28
 
@@ -7,9 +7,9 @@
7
7
  <thead>
8
8
  <tr>
9
9
  <td>Finish Date</td>
10
- <% sorted_stat_names(@metrify).each do |s| %>
10
+ <% sorted_stat_names.each do |s| %>
11
11
  <td>
12
- <%= link_to pretty_col_name(s, @metrify), :action => "graph_stats", :stat_names => s, :unit=>@unit %>
12
+ <%= link_to @metrified_class.display_name(s), :action => "graph_stats", :stat_names => s, :unit=>@unit %>
13
13
  </td>
14
14
  <% end %>
15
15
  </tr>
@@ -18,9 +18,9 @@
18
18
  <% @historical_site_stats.each do |stat| %>
19
19
  <tr>
20
20
  <td><%= stat.finish_date %></td>
21
- <% sorted_stat_names(@metrify).each do |s| %>
21
+ <% sorted_stat_names.each do |s| %>
22
22
  <% idx = @historical_site_stats.index(stat); last = @historical_site_stats.last == stat %>
23
- <td><%= print_stat_value(stat, s, @metrify, last ? nil : @historical_site_stats[idx+1]) %></td>
23
+ <td><%= print_stat_value(stat, s, last ? nil : @historical_site_stats[idx+1]) %></td>
24
24
  <% end %>
25
25
  </tr>
26
26
  <% end %>
@@ -28,7 +28,7 @@
28
28
  },
29
29
  get_filter_values : function(){
30
30
  var paramStrings = "";
31
- <% @metrify.filters.each_pair do |filter, subfilters| %>
31
+ <% @metrified_class.filters.each_pair do |filter, subfilters| %>
32
32
  <% subfilters.each_pair do |subfilter, attributes| %>
33
33
  var param = "<%= 'filters[' + filter.to_s + '[' + subfilter.to_s + ']]' %>";
34
34
  e = document.getElementById(param);
@@ -69,7 +69,7 @@
69
69
  <% showDateRangeOptions = ""
70
70
  showDateRangeOptions += "$(\'dateRangeOptions\').style.display = \'block\';\n" %>
71
71
  <div class="metrify_graph" id="stat_chart"></div>
72
- <% @metrify.filters.each_pair do |filter, subfilters| %>
72
+ <% @metrified_class.filters.each_pair do |filter, subfilters| %>
73
73
  <div><br><%= filter %>
74
74
  <% subfilters.each_pair do |subfilter, attributes| %>
75
75
  <input type="checkbox" id="<%= 'filters[' + filter.to_s + '[' + subfilter.to_s + ']]' %>" name="<%= subfilter.to_s %>" onClick="breakdown.create_new_chart();"/><%= subfilter.to_s %><br>
@@ -14,12 +14,19 @@ module Metrify
14
14
  raise MetrifyInclusionError, "The base class must be a descendent of active record." unless base.respond_to?(:descends_from_active_record?)
15
15
  base.class_eval do
16
16
  cattr_accessor :metrify_data
17
- cattr_accessor :start_date
18
- cattr_accessor :end_date
19
17
  end
20
18
  base.send :extend, ClassMethods
21
19
  end
22
20
 
21
+ module InstanceMethods
22
+ def method_missing(method, *args, &block)
23
+ self.class.stat_names.each do |name|
24
+ return stat_hash[name] if (name == method.to_s)
25
+ end
26
+ super
27
+ end
28
+ end
29
+
23
30
  module ClassMethods
24
31
 
25
32
  def acts_as_metrify(file = self.name.underscore + '_metrify.yml', test = false)
@@ -31,13 +38,6 @@ module Metrify
31
38
  self.metrify_data = YAML.load_file(File.join(RAILS_ROOT, 'config', file))
32
39
  end
33
40
  end
34
- end
35
-
36
- module InstanceMethods
37
-
38
- def metrify_data
39
- self.class.metrify_data
40
- end
41
41
 
42
42
  def filters
43
43
  metrify_data['filters']
@@ -59,7 +59,12 @@ module Metrify
59
59
  end
60
60
 
61
61
  def display_name(stat_name)
62
- config_val(stat_name, 'display_name')
62
+ configured_name = config_val(stat_name, 'display_name')
63
+ return configured_name if configured_name
64
+ s = ""
65
+ name = stat_name.split('_')
66
+ name.each {|word| s << ' '<< word.capitalize}
67
+ s
63
68
  end
64
69
 
65
70
  def config_val(stat_name, val)
@@ -72,10 +77,8 @@ module Metrify
72
77
  return display_name(name)
73
78
  elsif (CALC + name == method.to_s)
74
79
  new_meth = method.to_s[CALC.length,method.to_s.length]
75
- raise MetrifyInclusionError, "Base class must implement method: #{new_meth}." if !self.class.respond_to?(new_meth)
76
- return self.class.send(new_meth, *args, &block)
77
- elsif (name == method.to_s)
78
- return stat_hash[method.to_s]
80
+ raise MetrifyInclusionError, "Base class must implement method: #{new_meth}." if !self.respond_to?(new_meth)
81
+ return self.send(new_meth, *args, &block)
79
82
  end
80
83
  end
81
84
  super
@@ -140,13 +143,13 @@ module Metrify
140
143
  end
141
144
 
142
145
  def lookup(end_date = Time.now.midnight, interval = 1)
143
- self.class.find(:first, :conditions => {:finish_date => end_date, :number_of_days => interval})
146
+ find(:first, :conditions => {:finish_date => end_date, :number_of_days => interval})
144
147
  end
145
148
 
146
149
  def generate(end_date = Time.now.midnight, number_of_days = 1)
147
- s = self.class.find_or_create_by_finish_date_and_number_of_days(:finish_date => end_date, :number_of_days => number_of_days)
148
- self.start_date = end_date - number_of_days.days
149
- self.end_date = end_date
150
+ s = find_or_create_by_finish_date_and_number_of_days(:finish_date => end_date, :number_of_days => number_of_days)
151
+ start_date = end_date - number_of_days.days
152
+ end_date = end_date
150
153
  s.stat_hash = {}
151
154
  stat_names.each do |stat_name|
152
155
  # raise MetrifyInclusionError, "Base class must implement method: #{stat_name}." unless self.class.respond_to?(stat_name)
@@ -158,4 +161,9 @@ module Metrify
158
161
  s
159
162
  end
160
163
  end
164
+
165
+ module InstanceMethods
166
+
167
+
168
+ end
161
169
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stephen Abrams
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-18 00:00:00 -05:00
18
+ date: 2010-11-19 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency