lib/timeframe.rb in timeframe-0.0.1 vs lib/timeframe.rb in timeframe-0.0.2
- old
+ new
@@ -4,15 +4,17 @@
active_support/core_ext/array/extract_options
active_support/core_ext/string/conversions
active_support/core_ext/date/conversions
active_support/core_ext/integer/time
active_support/core_ext/numeric/time
+ active_support/json/encoding
}.each do |active_support_3_requirement|
require active_support_3_requirement
end if ActiveSupport::VERSION::MAJOR == 3
require 'andand'
require 'timeframe/ykk'
+require 'timeframe/date_to_json'
# Encapsulates a timeframe between two dates. The dates provided to the class are always until the last date. That means
# that the last date is excluded.
#
# # from 2007-10-01 00:00:00.000 to 2007-10-31 23:59:59.999
@@ -75,13 +77,13 @@
(to - from).to_i
end
# Returns a string representation of the timeframe
def to_s
- if [from.day, from.month, to.day, to.month].uniq == [1]
+ if (from.year == to.year - 1) and [from.day, from.month, to.day, to.month].uniq == [1]
from.year.to_s
- elsif from.day == 1 and to.day == 1 and to.month - from.month == 1
+ elsif from.year == to.year and from.day == 1 and to.day == 1 and to.month - from.month == 1
"#{Date::MONTHNAMES[from.month]} #{from.year}"
else
"the period from #{from.strftime('%d %B')} to #{to.yesterday.strftime('%d %B %Y')}"
end
end
@@ -118,11 +120,10 @@
#--
# This needs to be an integer or else it won't use #eql?
def hash
from.hash + to.hash
end
- alias :to_param :hash
# Returns an array of month-long subtimeframes
#--
# TODO: rename to month_subtimeframes
def months
@@ -246,9 +247,14 @@
self.class.new((from - 1.year), (to - 1.year))
end
def to_json # :nodoc:
{ :from => from, :to => to }.to_json
+ end
+
+ # URL-friendly like "2008-10-25/2009-11-12"
+ def to_param
+ "#{from}/#{to}"
end
class << self
# Shortcut method to return the Timeframe representing the current year (as defined by Time.now)
def this_year