Class: RuboCop::Cop::Legion::Framework::CacheTimeCoercion

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/legion/framework/cache_time_coercion.rb

Overview

Detects arithmetic subtraction on the direct return value of ‘cache_get`. After a cache round-trip, Time objects are serialized as Strings; calling `-` on a String raises NoMethodError. Wrap the read with `Time.parse(val)` at cache boundaries.

Examples:

# bad
cache_get(:last_run) - Time.now

# good
Time.parse(cache_get(:last_run)) - Time.now

Constant Summary collapse

MSG =
'Time objects become Strings after cache round-trip. ' \
'Coerce with `Time.parse(val)` at read boundaries.'
SEVERITY =
:convention
RESTRICT_ON_SEND =
%i[-].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/legion/framework/cache_time_coercion.rb', line 24

def on_send(node)
  receiver = node.receiver
  return unless receiver&.send_type?
  return unless receiver.receiver.nil?
  return unless receiver.method_name == :cache_get

  add_offense(node, severity: SEVERITY)
end