Created
January 28, 2009 22:31
-
-
Save gaffneyc/54224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based of Jay Fields' implementation (http://blog.jayfields.com/2007/11/ruby-timeis.html) | |
# | |
# Usage: | |
# Time.freeze do |time| | |
# Time.now.should.equal(time) | |
# end | |
class Time | |
def self.metaclass | |
class << self; self; end | |
end | |
def self.freeze(point_in_time = Time.now) | |
new_time = case point_in_time | |
when String then Time.parse(point_in_time) | |
when Time then point_in_time | |
else raise ArgumentError.new("argument should be a string or time instance") | |
end | |
class << self | |
alias old_now now | |
end | |
metaclass.class_eval do | |
define_method :now do | |
new_time | |
end | |
end | |
yield(new_time) | |
class << self | |
alias now old_now | |
undef old_now | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment