Class: Timecop::TimeStackItem

Inherits:
Object
  • Object
show all
Defined in:
lib/timecop/time_stack_item.rb

Overview

A data class for carrying around “time movement” objects. Makes it easy to keep track of the time movements on a simple stack.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mock_type, *args) ⇒ TimeStackItem

Returns a new instance of TimeStackItem.



7
8
9
10
11
12
13
14
15
# File 'lib/timecop/time_stack_item.rb', line 7

def initialize(mock_type, *args)
  raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
  @travel_offset  = @scaling_factor = nil
  @scaling_factor = args.shift if mock_type == :scale
  @mock_type      = mock_type
  @time           = parse_time(*args)
  @time_was       = Time.now_without_mock_time
  @travel_offset  = compute_travel_offset
end

Instance Attribute Details

#mock_typeObject (readonly)

:nodoc:



5
6
7
# File 'lib/timecop/time_stack_item.rb', line 5

def mock_type
  @mock_type
end

Instance Method Details

#date(date_klass = Date) ⇒ Object



77
78
79
# File 'lib/timecop/time_stack_item.rb', line 77

def date(date_klass = Date)
  date_klass.jd(time.__send__(:to_date).jd)
end

#datetime(datetime_klass = DateTime) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/timecop/time_stack_item.rb', line 81

def datetime(datetime_klass = DateTime)
  if Float.method_defined?(:to_r)
    fractions_of_a_second = time.to_f % 1
    datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
  else
    datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
  end
end

#dayObject



25
26
27
# File 'lib/timecop/time_stack_item.rb', line 25

def day
  time.day
end

#hourObject



29
30
31
# File 'lib/timecop/time_stack_item.rb', line 29

def hour
  time.hour
end

#minObject



33
34
35
# File 'lib/timecop/time_stack_item.rb', line 33

def min
  time.min
end

#monthObject



21
22
23
# File 'lib/timecop/time_stack_item.rb', line 21

def month
  time.month
end

#scaled_timeObject



73
74
75
# File 'lib/timecop/time_stack_item.rb', line 73

def scaled_time
  (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f
end

#scaling_factorObject



53
54
55
# File 'lib/timecop/time_stack_item.rb', line 53

def scaling_factor
  @scaling_factor
end

#secObject



37
38
39
# File 'lib/timecop/time_stack_item.rb', line 37

def sec
  time.sec
end

#time(time_klass = Time) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/timecop/time_stack_item.rb', line 57

def time(time_klass = Time) #:nodoc:
  if @time.respond_to?(:in_time_zone)
    time = time_klass.at(@time.dup.localtime)
  else
    time = time_klass.at(@time)
  end

  if travel_offset.nil?
    time
  elsif scaling_factor.nil?
    time_klass.at(Time.now_without_mock_time + travel_offset)
  else
    time_klass.at(scaled_time)
  end
end

#travel_offsetObject



45
46
47
# File 'lib/timecop/time_stack_item.rb', line 45

def travel_offset
  @travel_offset unless mock_type == :freeze
end

#travel_offset_daysObject



49
50
51
# File 'lib/timecop/time_stack_item.rb', line 49

def travel_offset_days
  (@travel_offset / 60 / 60 / 24).round
end

#utc_offsetObject



41
42
43
# File 'lib/timecop/time_stack_item.rb', line 41

def utc_offset
  time.utc_offset
end

#yearObject



17
18
19
# File 'lib/timecop/time_stack_item.rb', line 17

def year
  time.year
end