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
This cop 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]
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
57 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 57 def_node_matcher :duration?, '(send { int float (send nil _) } DURATIONS)' |
#duration_arithmetic_argument?(node) { ... } ⇒ Object
Match duration subtraction or addition with current time.
42 43 44 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 42 def_node_matcher :duration_arithmetic_argument?, <<~PATTERN (send #time_current? ${ :+ :- } $#duration?) PATTERN |
#on_send(node) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 77 def on_send(node) duration_arithmetic_argument?(node) do |*operation| add_offense(node) do |corrector| corrector.replace(node.source_range, corrected_source(*operation)) end end end |
#time_current?(node) ⇒ Boolean
Match Time.current
70 71 72 73 74 75 |
# File 'lib/rubocop/cop/rails/duration_arithmetic.rb', line 70 def_node_matcher :time_current?, <<~PATTERN { (send (const _ :Time) :current) (send (send (const _ :Time) :zone) :now) } PATTERN |