Class: Ducalis::MultipleTimes

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
RuboCop::Cop::DefNode
Defined in:
lib/ducalis/cops/multiple_times.rb

Constant Summary collapse

OFFENSE =
"  | You should avoid multiple time-related calls to prevent bugs during the period junctions (like Time.now.day called twice in the same scope could return different values if you called it near 23:59:59). You can pass it as default keyword argument or assign to a local variable.\n".gsub(/^ +\|\s/, '').strip
DETAILS =
"  | Compare:\n\n  | ```ruby\n  | def period\n  |   Date.today..(Date.today + 1.day)\n  | end\n  | # vs\n  | def period(today: Date.today)\n  |   today..(today + 1.day)\n  | end\n  | ```\n\n".gsub(/^ +\|\s/, '').strip
PARAMS_CALL =
s(:send, nil, :params)

Instance Method Summary collapse

Instance Method Details

#on_def(body) ⇒ Object Also known as: on_defs, on_send



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ducalis/cops/multiple_times.rb', line 30

def on_def(body)
  multiple = [
    date_today(body), date_current(body), date_yesterday(body),
    time_current(body), time_now(body)
  ].map(&:to_a).compact.flatten.to_a
  return if multiple.count < 2

  multiple.each do |time_node|
    add_offense(time_node, :expression, OFFENSE)
  end
end