Module: TimeMachine

Defined in:
lib/time_machine.rb

Overview

class Test::Unit::TestCase # 1

Instance Method Summary collapse

Instance Method Details

#now_as(time) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/time_machine.rb', line 6

def now_as(time)
  time_class = class << Time; self; end   # 5
  date_class = class << Date; self; end
  begin
    Time.class_eval do 
      @now_stack ||= []
      if @now_stack.empty?                # 10
        time_class.class_eval do
          alias_method :old_now, :now
          def now
            @now_stack.last.dup
          end                             # 15
        end
        date_class.class_eval do
          alias_method :old_today, :today
          def today
            Time.now.to_date              # 20
          end
        end
      end
      @now_stack.push(time.dup)
    end                                   # 25 
    yield
  ensure
    Time.class_eval do 
      @now_stack.pop
      if @now_stack.empty?                # 30
        date_class.class_eval {alias_method :today, :old_today}
        time_class.class_eval {alias_method :now, :old_now}
      end
    end
  end                                     # 35
end