Class: FlakeySpecCatcher::TimecopManager

Inherits:
Object
  • Object
show all
Defined in:
lib/flakey_spec_catcher/timecop_manager.rb

Class Method Summary collapse

Class Method Details

.current_monthObject



17
18
19
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 17

def self.current_month
  Time.now.month
end

.current_yearObject



21
22
23
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 21

def self.current_year
  Time.now.year
end

.generate_dates(count) ⇒ Object



66
67
68
69
70
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 66

def self.generate_dates(count)
  dates = prioritized_dates
  (count - prioritized_dates.count).times { dates << random_date }
  dates.map(&:iso8601).slice(0, count)
end

.last_day_of_month(year, month) ⇒ Object



42
43
44
45
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 42

def self.last_day_of_month(year, month)
  day = Date.new(year, month, -1).day
  Time.local(year, month, day, 23, 59, 59)
end

.last_day_of_year(year) ⇒ Object



47
48
49
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 47

def self.last_day_of_year(year)
  Time.local(year, 12, 31, 11, 59, 59)
end

.prioritized_datesObject



62
63
64
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 62

def self.prioritized_dates
  [last_day_of_month(current_year, current_month), last_day_of_year(current_year)]
end

.random_dateObject



55
56
57
58
59
60
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 55

def self.random_date
  year = current_year
  month = random_month
  day = random_day(year, month)
  Time.local(year, month, day, random_hour, random_minutes, random_seconds)
end

.random_day(year, month) ⇒ Object



37
38
39
40
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 37

def self.random_day(year, month)
  last_day = last_day_of_month(year, month).day
  [1, (1..last_day).to_a.sample, last_day].sample
end

.random_hourObject



51
52
53
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 51

def self.random_hour
  [0, (1..22).to_a.sample, 23].sample
end

.random_minutesObject



33
34
35
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 33

def self.random_minutes
  [0, (1..58).to_a.sample, 59].sample
end

.random_monthObject



25
26
27
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 25

def self.random_month
  (current_month..12).to_a.sample
end

.random_secondsObject



29
30
31
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 29

def self.random_seconds
  [0, 59].sample
end

.randomly_travel_in_time(date) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 9

def self.randomly_travel_in_time(date)
  status = 0
  Timecop.travel(date) do
    status = yield
  end
  status
end