Sha256: b7459568b6478537d1aa66cfdb7dfd867b546d6352a409ce16c6acf1e7dc43f8

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

#	This file is part of the "Utopia Framework" project, and is licensed under the GNU AGPLv3.
#	Copyright 2010 Samuel Williams. All rights reserved.
#	See <utopia.rb> for licensing details.

# These amendments allow for Date <=> DateTime <=> Time, and so on.
# Use only if required. This implementation works for Ruby 1.9.2.

require 'date'

class Date
	alias_method :old_compare, :<=>
	
	def <=>(other)
		if other.class == Date
			old_compare(other)
		else
			if Time === other
				other = other.to_datetime
			end
			
		 	if DateTime === other
				result = old_compare(other.to_date)
				if result == 0 && other.day_fraction > 0
					-1
				else
					result
				end
			end
		end
	end
end

class Time
	alias_method :old_compare, :<=>
	
	def <=>(other)
		if other.class == Date
			(other <=> self) * -1
		elsif Time === other
			old_compare(other)
		else
			if DateTime === other
				other = other.to_time
			end
			
			old_compare(other)
		end
	end
end

class DateTime
	alias_method :old_compare, :<=>
	
	def <=>(other)
		if other.class == Date
			(other <=> self) * -1
		elsif DateTime === other
			old_compare(other)
		else
			if Time === other
				other = other.to_datetime
			end
			
			old_compare(other)
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
utopia-0.9.58 lib/utopia/extensions/date.rb
utopia-0.9.57 lib/utopia/extensions/date.rb