Simplified and DRYed up logic for ReportingPeriod#first, #next and #previous. - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 52fc6a795cb9b10fce03b8283a6d461389871e02
(DIR) parent 33fe1197f6f34ee2f9da7516e818559713fba948
(HTM) Author: Myron Marston <myron.marston@gmail.com>
Date: Fri, 3 Apr 2009 09:12:17 +0800
Simplified and DRYed up logic for ReportingPeriod#first, #next and #previous.
Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Diffstat:
M lib/kvlr/reports_as_sparkline/repo… | 38 ++++++-------------------------
1 file changed, 7 insertions(+), 31 deletions(-)
---
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/reporting_period.rb b/lib/kvlr/reports_as_sparkline/reporting_period.rb
@@ -21,17 +21,7 @@ module Kvlr #:nodoc:
# * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
# * <tt>limit</tt> - The number of reporting periods until the first one
def self.first(grouping, limit)
- return case grouping.identifier
- when :hour
- self.new(grouping, DateTime.now - limit.hours)
- when :day
- self.new(grouping, DateTime.now - limit.days)
- when :week
- self.new(grouping, DateTime.now - limit.weeks)
- when :month
- date = DateTime.now - limit.months
- self.new(grouping, Date.new(date.year, date.month, 1))
- end
+ self.new(grouping, DateTime.now).offset(-limit)
end
def self.from_db_string(grouping, db_string) #:nodoc:
@@ -51,30 +41,16 @@ module Kvlr #:nodoc:
# Returns the next reporting period (that is next hour/day/month/year)
def next
- return case @grouping.identifier
- when :hour
- self.class.new(@grouping, @date_time + 1.hour)
- when :day
- self.class.new(@grouping, @date_time + 1.day)
- when :week
- self.class.new(@grouping, @date_time + 1.week)
- when :month
- self.class.new(@grouping, @date_time + 1.month)
- end
+ self.offset(1)
end
# Returns the previous reporting period (that is next hour/day/month/year)
def previous
- return case @grouping.identifier
- when :hour
- self.class.new(@grouping, @date_time - 1.hour)
- when :day
- self.class.new(@grouping, @date_time - 1.day)
- when :week
- self.class.new(@grouping, @date_time - 1.week)
- when :month
- self.class.new(@grouping, @date_time - 1.month)
- end
+ self.offset(-1)
+ end
+
+ def offset(val)
+ self.class.new(@grouping, @date_time + val.send(@grouping.identifier))
end
def ==(other) #:nodoc: