Sha256: 57d2a31e93db7fd9a444ca904c03f10d9e0f412a456b59115822d27fd537e50f
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
module ExpressAnalytics class DailyStatistic < ActiveRecord::Base def self.all_statistics Dir.glob(File.join(Rails.root, 'app', 'statistics', '*.rb')).map do |statistic| file_name = File.basename(statistic).gsub('.rb', '') class_name = file_name.classify class_name = class_name.pluralize if file_name.match(/s$/) class_name.constantize end end def self.calculate_all!(date=nil) all_statistics.each { |stat| stat.calculate!(date) } end def self.calculate(&block) define_method(calculate_method_name, &block) end def self.calculate!(date_or_date_range = nil) _arrayify(date_or_date_range||Date.today).each do |date| _calculate_for_date!(date) end end def calculate! self.value = send(self.class.calculate_method_name, calculated_for) end private def self._arrayify(date_or_date_range) if date_or_date_range.present? && date_or_date_range.kind_of?(Range) date_or_date_range.to_a else [date_or_date_range].flatten.compact end end def self._calculate_for_date!(date) stat = find_by_calculated_for(date.to_date) || new(calculated_for: date.to_date) stat.calculate! stat.save stat end def self.calculate_method_name "calculate_#{self.class.to_s.underscore}!".to_sym end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
express_analytics-0.0.2 | app/models/express_analytics/daily_statistic.rb |
express_analytics-0.0.1 | app/models/express_analytics/daily_statistic.rb |