Class: Timecop::TimeStackItem
- Inherits:
-
Object
- Object
- Timecop::TimeStackItem
- 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
-
#mock_type ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #date(date_klass = Date) ⇒ Object
- #datetime(datetime_klass = DateTime) ⇒ Object
- #day ⇒ Object
- #hour ⇒ Object
-
#initialize(mock_type, *args) ⇒ TimeStackItem
constructor
A new instance of TimeStackItem.
- #min ⇒ Object
- #month ⇒ Object
- #scaled_time ⇒ Object
- #scaling_factor ⇒ Object
- #sec ⇒ Object
-
#time(time_klass = Time) ⇒ Object
:nodoc:.
- #travel_offset ⇒ Object
- #travel_offset_days ⇒ Object
- #utc_offset ⇒ Object
- #year ⇒ Object
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_type ⇒ Object (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 |
#day ⇒ Object
25 26 27 |
# File 'lib/timecop/time_stack_item.rb', line 25 def day time.day end |
#hour ⇒ Object
29 30 31 |
# File 'lib/timecop/time_stack_item.rb', line 29 def hour time.hour end |
#min ⇒ Object
33 34 35 |
# File 'lib/timecop/time_stack_item.rb', line 33 def min time.min end |
#month ⇒ Object
21 22 23 |
# File 'lib/timecop/time_stack_item.rb', line 21 def month time.month end |
#scaled_time ⇒ Object
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_factor ⇒ Object
53 54 55 |
# File 'lib/timecop/time_stack_item.rb', line 53 def scaling_factor @scaling_factor end |
#sec ⇒ Object
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_offset ⇒ Object
45 46 47 |
# File 'lib/timecop/time_stack_item.rb', line 45 def travel_offset @travel_offset unless mock_type == :freeze end |
#travel_offset_days ⇒ Object
49 50 51 |
# File 'lib/timecop/time_stack_item.rb', line 49 def travel_offset_days (@travel_offset / 60 / 60 / 24).round end |
#utc_offset ⇒ Object
41 42 43 |
# File 'lib/timecop/time_stack_item.rb', line 41 def utc_offset time.utc_offset end |
#year ⇒ Object
17 18 19 |
# File 'lib/timecop/time_stack_item.rb', line 17 def year time.year end |