Class: RuboCop::Cop::Rails::DurationArithmetic
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::DurationArithmetic
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rails/duration_arithmetic.rb
Overview
Checks if a duration is added to or subtracted from ‘Time.current`.
Constant Summary collapse
- MSG =
'Do not add or subtract duration.'
- RESTRICT_ON_SEND =
%i[+ -].freeze
- DURATIONS =
Set[:second, :seconds, :minute, :minutes, :hour, :hours, :day, :days, :week, :weeks, :fortnight, :fortnights, :month, :months, :year, :years]
Instance Method Summary collapse
-
#duration?(node) ⇒ Boolean
Match a literal Duration.
-
#duration_arithmetic_argument?(node) { ... } ⇒ Object
Match duration subtraction or addition with current time.
- #on_send(node) ⇒ Object
-
#time_current?(node) ⇒ Boolean
Match Time.current.
Instance Method Details
#duration?(node) ⇒ Boolean
Match a literal Duration
58 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 58 def_node_matcher :duration?, '(send { int float (send nil _) } DURATIONS)' |
#duration_arithmetic_argument?(node) { ... } ⇒ Object
Match duration subtraction or addition with current time.
43 44 45 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 43 def_node_matcher :duration_arithmetic_argument?, <<~PATTERN (send #time_current? ${ :+ :- } $#duration?) PATTERN |
#on_send(node) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 78 def on_send(node) duration_arithmetic_argument?(node) do |*operation| add_offense(node) do |corrector| corrector.replace(node, corrected_source(*operation)) end end end |
#time_current?(node) ⇒ Boolean
Match Time.current
71 72 73 74 75 76 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 71 def_node_matcher :time_current?, <<~PATTERN { (send (const {nil? cbase} :Time) :current) (send (send (const {nil? cbase} :Time) :zone) :now) } PATTERN |