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 * 1
- 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.
- #logger ⇒ ::Logger
-
#n_days_from_now(num_days) ⇒ ActiveSupport::TimeWithZone
@param
num_days. -
#n_days_from_today(num_days) ⇒ Date
@sg-ignore.
-
#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
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/checkoff/timing.rb', line 189 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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/checkoff/timing.rb', line 213 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 # @sg-ignore 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
155 156 157 158 159 160 161 162 |
# File 'lib/checkoff/timing.rb', line 155 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
765 |
# File 'sig/checkoff.rbs', line 765
def debug: (?Object? message) -> void
|
#error ⇒ void
This method returns an undefined value.
@param message
756 |
# File 'sig/checkoff.rbs', line 756
def error: (?Object? message) -> void
|
#finer ⇒ void
This method returns an undefined value.
@param message
768 |
# File 'sig/checkoff.rbs', line 768
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
82 83 84 85 86 |
# File 'lib/checkoff/timing.rb', line 82 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
71 72 73 74 75 76 77 78 |
# File 'lib/checkoff/timing.rb', line 71 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 63 64 65 |
# 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 # @sg-ignore return day_of_week?(date_or_time, period) 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) # @sg-ignore # @type [Symbol] period_name = period.first 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
762 |
# File 'sig/checkoff.rbs', line 762
def info: (?Object? message) -> void
|
#less_than_n_days_ago?(date_or_time, num_days) ⇒ Boolean
@param date_or_time
@param num_days
90 91 92 93 94 95 96 97 |
# File 'lib/checkoff/timing.rb', line 90 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
101 102 103 104 105 |
# File 'lib/checkoff/timing.rb', line 101 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
771 |
# File 'sig/checkoff.rbs', line 771
def log_level: () -> Symbol
|
#logger ⇒ ::Logger
753 |
# File 'sig/checkoff.rbs', line 753
def logger: () -> ::Logger
|
#n_days_from_now(num_days) ⇒ ActiveSupport::TimeWithZone
@param num_days
174 175 176 |
# File 'lib/checkoff/timing.rb', line 174 def n_days_from_now(num_days) num_days.days.since(@now_getter.now) end |
#n_days_from_today(num_days) ⇒ Date
@sg-ignore
@param num_days
182 183 184 |
# File 'lib/checkoff/timing.rb', line 182 def n_days_from_today(num_days) @today_getter.today + num_days end |
#next_week?(date_or_time) ⇒ Boolean
@param date_or_time
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/checkoff/timing.rb', line 125 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
165 166 167 168 169 |
# File 'lib/checkoff/timing.rb', line 165 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/checkoff/timing.rb', line 108 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
759 |
# File 'sig/checkoff.rbs', line 759
def warn: (?Object? message) -> void
|