Class: Checkoff::Timing

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/checkoff/timing.rb,
sig/checkoff.rbs

Overview

Common vocabulary for managing time and time periods

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
DAY =

Returns:

  • (Object)
24 * HOUR
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
HOUR * 1
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE
WDAY_FROM_DAY_OF_WEEK =

Returns:

  • (Object)
{
  monday: 1,
  tuesday: 2,
  wednesday: 3,
  thursday: 4,
  friday: 5,
  saturday: 6,
  sunday: 0,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(today_getter: Date, now_getter: Time) ⇒ Timing

@param today_getter

@param now_getter

Parameters:

  • today_getter: (singleton(Date)) (defaults to: Date)
  • now_getter: (singleton(Time)) (defaults to: Time)


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

.runvoid

This method returns an undefined value.



236
237
238
239
240
# File 'lib/checkoff/timing.rb', line 236

def run
  time = Checkoff::Timing.new
  # time = time.time_or_raise(workspace_name, time_name)
  puts "Results: #{time}"
end

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

Parameters:

  • date_or_time (Date, Time, nil)
  • beginning_num_days_from_now (Integer, nil)
  • end_num_days_from_now (Integer, nil)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • period_name (Symbol)
  • args (Object)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • day_of_week (Symbol)

Returns:

  • (Boolean)


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

#debugvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


765
# File 'sig/checkoff.rbs', line 765

def debug: (?Object? message) -> void

#errorvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


756
# File 'sig/checkoff.rbs', line 756

def error: (?Object? message) -> void

#finervoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • num_days (Integer)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • num_days (Integer)

Returns:

  • (Boolean)


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]

Parameters:

  • date_or_time (Date, Time, nil)
  • period (Symbol, [Symbol, Integer])

Returns:

  • (Boolean)


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

#infovoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • num_days (Integer)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)
  • num_days (Integer)

Returns:

  • (Boolean)


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_levelSymbol

@sg-ignore

Returns:

  • (Symbol)


771
# File 'sig/checkoff.rbs', line 771

def log_level: () -> Symbol

#logger::Logger

Returns:

  • (::Logger)


753
# File 'sig/checkoff.rbs', line 753

def logger: () -> ::Logger

#n_days_from_now(num_days) ⇒ ActiveSupport::TimeWithZone

@param num_days

Parameters:

  • num_days (Integer)

Returns:

  • (ActiveSupport::TimeWithZone)


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

Parameters:

  • num_days (Integer)

Returns:

  • (Date)


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

Parameters:

  • date_or_time (Date, Time, nil)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)

Returns:

  • (Boolean)


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

Parameters:

  • date_or_time (Date, Time, nil)

Returns:

  • (Boolean)


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

#warnvoid

This method returns an undefined value.

@param message

Parameters:

  • message (Object, nil)


759
# File 'sig/checkoff.rbs', line 759

def warn: (?Object? message) -> void