lib/timeframe.rb in timeframe-0.0.6 vs lib/timeframe.rb in timeframe-0.0.7
- old
+ new
@@ -4,16 +4,13 @@
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 'to_json_fix'
require 'timeframe/ykk'
# 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.
#
@@ -52,12 +49,12 @@
elsif year = options[:year]
from = Date.new(year, 1, 1)
to = Date.new(year+1, 1, 1)
end
- from ||= args.shift.andand.to_date
- to ||= args.shift.andand.to_date
+ from = args.shift.to_date if from.nil? and args.any?
+ to = args.shift.to_date if to.nil? and args.any?
raise ArgumentError, "Please supply a start and end date, `#{args.map(&:inspect).to_sentence}' is not enough" if from.nil? or to.nil?
raise ArgumentError, "Start date #{from} should be earlier than end date #{to}" if from > to
raise ArgumentError, 'Timeframes that cross year boundaries are dangerous' unless options[:skip_year_boundary_crossing_check] or from.year == to.yesterday.year or from == to
@@ -246,29 +243,33 @@
def last_year
self.class.new((from - 1.year), (to - 1.year))
end
# Just a string that can be processed by Timeframe.interval... identical to #to_param
- # accepts multiple arguments because of disagreements between active_support/json and json gem
def to_json(*)
to_param
end
# URL-friendly like "2008-10-25/2009-11-12"
def to_param
"#{from}/#{to}"
end
class << self
+ def make_dates(from, to) # :nodoc:
+ return from.to_date, to.to_date
+ end
+
# Shortcut method to return the Timeframe representing the current year (as defined by Time.now)
def this_year
new :year => Time.now.year
end
# Construct a new Timeframe, but constrain it by another
def constrained_new(from, to, constraint)
- raise ArgumentError, 'Need Date, Date, Timeframe as args' unless from.is_a? Date and to.is_a? Date and constraint.is_a? Timeframe
+ from, to = make_dates from, to
+ raise ArgumentError, 'Constraint must be a Timeframe' unless constraint.is_a? Timeframe
raise ArgumentError, "Start date #{from} should be earlier than end date #{to}" if from > to
if to <= constraint.from or from >= constraint.to
new constraint.from, constraint.from
elsif from.year == to.yesterday.year
new(from, to) & constraint
@@ -279,12 +280,11 @@
end
end
# Shortcut for #new that automatically skips year boundary crossing checks
def multiyear(from, to)
- from = Date.parse(from) if from.is_a?(String)
- to = Date.parse(to) if to.is_a?(String)
+ from, to = make_dates from, to
new from, to, :skip_year_boundary_crossing_check => true
end
# Create a multiyear timeframe +/- number of years around today
def mid(number)
@@ -296,11 +296,10 @@
# Construct a new Timeframe by parsing an ISO 8601 time interval string
# http://en.wikipedia.org/wiki/ISO_8601#Time_intervals
def interval(str)
raise ArgumentError, 'Intervals should be specified as a string' unless str.is_a? String
raise ArgumentError, 'Intervals should be specified according to ISO 8601, method 1, eliding times' unless str =~ /^\d\d\d\d-\d\d-\d\d\/\d\d\d\d-\d\d-\d\d$/
-
- new(*str.split('/').map { |date| Date.parse date })
+ multiyear *str.split('/')
end
alias :from_json :interval
end
end