Class: Checkoff::Timing
- Inherits:
-
Object
- Object
- Checkoff::Timing
- Includes:
- Logging
- Defined in:
- lib/checkoff/timing.rb,
sig/checkoff.rbs
Overview
Common vocabulary for managing time and time periods
Constant Summary collapse
- MINUTE =
60- HOUR =
MINUTE * 60
- DAY =
24 * HOUR
- REALLY_LONG_CACHE_TIME =
HOUR- LONG_CACHE_TIME =
MINUTE * 15
- SHORT_CACHE_TIME =
MINUTE- WDAY_FROM_DAY_OF_WEEK =
{ monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 0, }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#between_relative_days?(date_or_time, beginning_num_days_from_now, end_num_days_from_now) ⇒ Boolean
@param
date_or_time. -
#compound_in_period?(date_or_time, period_name, *args) ⇒ Boolean
@param
date_or_time. -
#day_of_week?(date_or_time, day_of_week) ⇒ Boolean
@param
date_or_time. -
#debug ⇒ void
@param
message. -
#error ⇒ void
@param
message. -
#finer ⇒ void
@param
message. -
#greater_than_or_equal_to_n_days_from_now?(date_or_time, num_days) ⇒ Boolean
@param
date_or_time. -
#greater_than_or_equal_to_n_days_from_today?(date_or_time, num_days) ⇒ Boolean
@param
date_or_time. -
#in_period?(date_or_time, period) ⇒ Boolean
@param
date_or_time. -
#info ⇒ void
@param
message. -
#initialize(today_getter: Date, now_getter: Time) ⇒ Timing
constructor
@param
today_getter. -
#less_than_n_days_ago?(date_or_time, num_days) ⇒ Boolean
@param
date_or_time. -
#less_than_n_days_from_now?(date_or_time, num_days) ⇒ Boolean
@param
date_or_time. -
#log_level ⇒ Symbol
@sg-ignore return type could not be inferred — cascades from the ENV.fetch RBS gap below.
- #logger ⇒ ::Logger
-
#n_days_from_now(num_days) ⇒ ActiveSupport::TimeWithZone
@param
num_days. -
#n_days_from_today(num_days) ⇒ Date
@sg-ignore Checkoff::Timing#n_days_from_today return type could not be inferred.
-
#next_week?(date_or_time) ⇒ Boolean
@param
date_or_time. -
#now_or_before?(date_or_time) ⇒ Boolean
@param
date_or_time. -
#this_week?(date_or_time) ⇒ Boolean
@param
date_or_time. -
#warn ⇒ void
@param
message.
Constructor Details
#initialize(today_getter: Date, now_getter: Time) ⇒ Timing
@param today_getter
@param now_getter
32 33 34 35 36 |
# File 'lib/checkoff/timing.rb', line 32 def initialize(today_getter: Date, now_getter: Time) @today_getter = today_getter @now_getter = now_getter end |
Class Method Details
Instance Method Details
#between_relative_days?(date_or_time, beginning_num_days_from_now, end_num_days_from_now) ⇒ Boolean
@param date_or_time
@param beginning_num_days_from_now
@param end_num_days_from_now
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/checkoff/timing.rb', line 186 def between_relative_days?(date_or_time, beginning_num_days_from_now, end_num_days_from_now) start_date = n_days_from_today(beginning_num_days_from_now || -99_999) end_date = n_days_from_today(end_num_days_from_now || 99_999) return false if date_or_time.nil? date = if date_or_time.is_a?(Time) date_or_time.localtime.to_date else date_or_time end out = date > start_date && date <= end_date finer do "Checkoff::Timing#between_relative_days?(#{date_or_time.inspect} " \ "(as date: #{date.inspect}) " \ "#{beginning_num_days_from_now.inspect}, #{end_num_days_from_now.inspect}) " \ "comparing with #{start_date} and #{end_date} = #{out.inspect}" end out end |
#compound_in_period?(date_or_time, period_name, *args) ⇒ Boolean
@param date_or_time
@param period_name
@param args
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/checkoff/timing.rb', line 210 def compound_in_period?(date_or_time, period_name, *args) return less_than_n_days_ago?(date_or_time, *args) if period_name == :less_than_n_days_ago return less_than_n_days_from_now?(date_or_time, *args) if period_name == :less_than_n_days_from_now if period_name == :greater_than_or_equal_to_n_days_from_now return greater_than_or_equal_to_n_days_from_now?(date_or_time, *args) end if period_name == :greater_than_or_equal_to_n_days_from_today return greater_than_or_equal_to_n_days_from_today?(date_or_time, *args) end return between_relative_days?(date_or_time, *args) if period_name == :between_relative_days raise "Teach me how to handle period [#{period_name.inspect}, #{args.map(&:inspect).join(', ')}]" end |
#day_of_week?(date_or_time, day_of_week) ⇒ Boolean
@param date_or_time
@param day_of_week
152 153 154 155 156 157 158 159 |
# File 'lib/checkoff/timing.rb', line 152 def day_of_week?(date_or_time, day_of_week) return false if date_or_time.nil? wday = WDAY_FROM_DAY_OF_WEEK.fetch(day_of_week) today = @today_getter.today date = date_or_time.to_date date_or_time.wday == wday && date >= today && date < today + 7 end |
#debug ⇒ void
This method returns an undefined value.
@param message
767 |
# File 'sig/checkoff.rbs', line 767
def debug: (?Object? message) -> void
|
#error ⇒ void
This method returns an undefined value.
@param message
758 |
# File 'sig/checkoff.rbs', line 758
def error: (?Object? message) -> void
|
#finer ⇒ void
This method returns an undefined value.
@param message
770 |
# File 'sig/checkoff.rbs', line 770
def finer: (?Object? message) -> void
|
#greater_than_or_equal_to_n_days_from_now?(date_or_time, num_days) ⇒ Boolean
@param date_or_time
@param num_days
79 80 81 82 83 |
# File 'lib/checkoff/timing.rb', line 79 def greater_than_or_equal_to_n_days_from_now?(date_or_time, num_days) return false if date_or_time.nil? date_or_time >= n_days_from_now(num_days) end |
#greater_than_or_equal_to_n_days_from_today?(date_or_time, num_days) ⇒ Boolean
@param date_or_time
@param num_days
68 69 70 71 72 73 74 75 |
# File 'lib/checkoff/timing.rb', line 68 def greater_than_or_equal_to_n_days_from_today?(date_or_time, num_days) return false if date_or_time.nil? date = date_or_time.to_date n_days_from_today = @today_getter.today + num_days date >= n_days_from_today end |
#in_period?(date_or_time, period) ⇒ Boolean
@param date_or_time
@param period — Valid values: :this_week, :now_or_before, :indefinite, [:less_than_n_days_ago, Integer]
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/checkoff/timing.rb', line 42 def in_period?(date_or_time, period) return this_week?(date_or_time) if period == :this_week return next_week?(date_or_time) if period == :next_week return day_of_week?(date_or_time, T.cast(period, Symbol)) if %i[monday tuesday wednesday thursday friday saturday sunday].include?(period) return true if period == :indefinite return now_or_before?(date_or_time) if period == :now_or_before if period.is_a?(Array) period_name = T.cast(period.first, Symbol) args = period[1..] return compound_in_period?(date_or_time, period_name, *args) end raise "Teach me how to handle period #{period.inspect}" end |
#info ⇒ void
This method returns an undefined value.
@param message
764 |
# File 'sig/checkoff.rbs', line 764
def info: (?Object? message) -> void
|
#less_than_n_days_ago?(date_or_time, num_days) ⇒ Boolean
@param date_or_time
@param num_days
87 88 89 90 91 92 93 94 |
# File 'lib/checkoff/timing.rb', line 87 def less_than_n_days_ago?(date_or_time, num_days) return false if date_or_time.nil? date = date_or_time.to_date n_days_ago = @today_getter.today - num_days date < n_days_ago end |
#less_than_n_days_from_now?(date_or_time, num_days) ⇒ Boolean
@param date_or_time
@param num_days
98 99 100 101 102 |
# File 'lib/checkoff/timing.rb', line 98 def less_than_n_days_from_now?(date_or_time, num_days) return false if date_or_time.nil? date_or_time < n_days_from_now(num_days) end |
#log_level ⇒ Symbol
@sg-ignore return type could not be inferred — cascades from the ENV.fetch RBS gap below
773 |
# File 'sig/checkoff.rbs', line 773
def log_level: () -> Symbol
|
#logger ⇒ ::Logger
755 |
# File 'sig/checkoff.rbs', line 755
def logger: () -> ::Logger
|
#n_days_from_now(num_days) ⇒ ActiveSupport::TimeWithZone
@param num_days
171 172 173 |
# File 'lib/checkoff/timing.rb', line 171 def n_days_from_now(num_days) num_days.days.since(@now_getter.now) end |
#n_days_from_today(num_days) ⇒ Date
@sg-ignore Checkoff::Timing#n_days_from_today return type could not be inferred
@param num_days
179 180 181 |
# File 'lib/checkoff/timing.rb', line 179 def n_days_from_today(num_days) T.cast(@today_getter.today + num_days, Date) end |
#next_week?(date_or_time) ⇒ Boolean
@param date_or_time
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/checkoff/timing.rb', line 122 def next_week?(date_or_time) return false if date_or_time.nil? today = @today_getter.today # Beginning of next week (assuming week starts on Sunday) beginning_of_week = today - today.wday + 7 # End of this week (assuming week ends on Saturday) end_of_week = beginning_of_week + 6 date = date_or_time.to_date date.between?(beginning_of_week, end_of_week) end |
#now_or_before?(date_or_time) ⇒ Boolean
@param date_or_time
162 163 164 165 166 |
# File 'lib/checkoff/timing.rb', line 162 def now_or_before?(date_or_time) return true if date_or_time.nil? date_or_time.to_time < @now_getter.now end |
#this_week?(date_or_time) ⇒ Boolean
@param date_or_time
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/checkoff/timing.rb', line 105 def this_week?(date_or_time) return true if date_or_time.nil? today = @today_getter.today # Beginning of this week (assuming week starts on Sunday) beginning_of_week = today - today.wday # End of this week (assuming week ends on Saturday) end_of_week = beginning_of_week + 6 date = date_or_time.to_date date.between?(beginning_of_week, end_of_week) end |
#warn ⇒ void
This method returns an undefined value.
@param message
761 |
# File 'sig/checkoff.rbs', line 761
def warn: (?Object? message) -> void
|