Class: RuboCop::Cop::Rails::RelativeDateConstant
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::RelativeDateConstant
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rails/relative_date_constant.rb
Overview
Checks whether constant value isn’t relative date. Because the relative date will be evaluated only once.
Constant Summary collapse
- MSG =
'Do not assign `%<method_name>s` to constants as it will be evaluated only once.'
- RELATIVE_DATE_METHODS =
%i[since from_now after ago until before yesterday tomorrow].to_set.freeze
Instance Method Summary collapse
Instance Method Details
#on_casgn(node) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rubocop/cop/rails/relative_date_constant.rb', line 40 def on_casgn(node) nested_relative_date(node) do |method_name| add_offense(node, message: (method_name)) do |corrector| autocorrect(corrector, node) end end end |
#on_masgn(node) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rubocop/cop/rails/relative_date_constant.rb', line 48 def on_masgn(node) lhs, rhs = *node return unless rhs&.array_type? lhs.children.zip(rhs.children).each do |(name, value)| next unless name.casgn_type? nested_relative_date(value) do |method_name| add_offense(offense_range(name, value), message: (method_name)) do |corrector| autocorrect(corrector, node) end end end end |
#on_or_asgn(node) ⇒ Object
64 65 66 67 68 |
# File 'lib/rubocop/cop/rails/relative_date_constant.rb', line 64 def on_or_asgn(node) relative_date_or_assignment(node) do |method_name| add_offense(node, message: format(MSG, method_name: method_name)) end end |