lib/timeframe.rb in timeframe-0.2.1 vs lib/timeframe.rb in timeframe-1.0.0

- old
+ new

@@ -1,9 +1,9 @@ require 'date' require 'multi_json' -require 'active_support/version' -require 'active_support/core_ext' if ActiveSupport::VERSION::MAJOR >= 3 +require 'active_support' +require 'active_support/core_ext/date' require 'timeframe/iso_8601' # 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. @@ -92,14 +92,27 @@ # Deprecated def multiyear(*args) # :nodoc: new *args end + def to_date(v) + case v + when NilClass + nil + when Date + v + when Time + v.to_date + else + Date.parse v + end + end + private def make_dates(start_date, end_date) - [start_date.to_date, end_date.to_date] + [to_date(start_date), to_date(end_date)] end end attr_reader :start_date attr_reader :end_date @@ -129,12 +142,12 @@ elsif year = options[:year] start_date = Date.new(year, 1, 1) end_date = Date.new(year+1, 1, 1) end - start_date = args.shift.to_date if start_date.nil? and args.any? - end_date = args.shift.to_date if end_date.nil? and args.any? + start_date ||= Timeframe.to_date(args[0]) + end_date ||= Timeframe.to_date(args[1]) raise ArgumentError, "Please supply a start and end date, `#{args.map(&:inspect).to_sentence}' is not enough" if start_date.nil? or end_date.nil? raise ArgumentError, "Start date #{start_date} should be earlier than end date #{end_date}" if start_date > end_date @start_date, @end_date = start_date, end_date @@ -158,11 +171,11 @@ # puts "checking to see if #{date} is between #{start_date} and #{end_date}" if Emitter::DEBUG case obj when Date (start_date...end_date).include?(obj) when Time - # (start_date...end_date).include?(obj.to_date) + # (start_date...end_date).include?(Date.parse(obj)) raise "this wasn't previously supported, but it could be" when Timeframe start_date <= obj.start_date and end_date >= obj.end_date end end @@ -273,10 +286,13 @@ gaps_left_by(*timeframes).empty? end # Returns the same Timeframe, only a year earlier def last_year - self.class.new((start_date - 1.year), (end_date - 1.year)) + self.class.new( + Date.new(start_date.year - 1, start_date.month, start_date.day), + Date.new(end_date.year - 1, end_date.month, end_date.day) + ) end def as_json(*) iso8601 end