Class: FlakeySpecCatcher::TimecopManager
- Inherits:
-
Object
- Object
- FlakeySpecCatcher::TimecopManager
- Defined in:
- lib/flakey_spec_catcher/timecop_manager.rb
Class Method Summary collapse
- .current_month ⇒ Object
- .current_year ⇒ Object
- .generate_dates(count) ⇒ Object
- .last_day_of_month(year, month) ⇒ Object
- .last_day_of_year(year) ⇒ Object
- .prioritized_dates ⇒ Object
- .random_date ⇒ Object
- .random_day(year, month) ⇒ Object
- .random_hour ⇒ Object
- .random_minutes ⇒ Object
- .random_month ⇒ Object
- .random_seconds ⇒ Object
- .randomly_travel_in_time(date) ⇒ Object
Class Method Details
.current_month ⇒ Object
17 18 19 |
# File 'lib/flakey_spec_catcher/timecop_manager.rb', line 17 def self.current_month Time.now.month end |
.current_year ⇒ Object
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_dates ⇒ Object
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_date ⇒ Object
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_hour ⇒ Object
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_minutes ⇒ Object
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_month ⇒ Object
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_seconds ⇒ Object
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 |