Class: Rubopolis::Cop::TimeUsage

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubopolis/cop/time_usage.rb

Overview

Examples:

# bad
Time.zone.today
Time.zone.now
Time.current + 2.weeks
Time.current - 2.weeks

# good
Date.current
Time.current
2.weeks.ago
2.weeks.since or 2.weeks.from_now

Constant Summary collapse

USAGE_MSG =
'`Time.zone.now` or `Time.zone.today` should not be used, to consider Time.current or ' \
'Date.current instead: see lib/custom_cops/time_usage'
ARITHMETIC_MSG =
'Avoid subtracting or adding for Time, to use methods like `ago` and `since/from_now` e.g. ' \
'`2.weeks.ago`, `5.minutes.since`. Refer to https://api.rubyonrails.org/classes/Time.html ' \
'for more info'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubopolis/cop/time_usage.rb', line 45

def on_send(node)
  if time_now?(node) || time_today?(node)
    message = USAGE_MSG
  elsif time_addition?(node) || time_subtract?(node)
    message = ARITHMETIC_MSG
  else
    return
  end

  add_offense(node, message: message)
end