Sha256: 0e79270ea905fb1349fa0553d545b108439e2d0855a0ebcc5f36ff0b72803651

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks for the use of the read_attribute or
      # write_attribute methods.
      #
      # @example
      #
      #   # bad
      #   x = read_attribute(:attr)
      #   write_attribute(:attr, val)
      #
      #   # good
      #   x = self[:attr]
      #   self[:attr] = val
      class ReadWriteAttribute < Cop
        MSG = 'Prefer `%s` over `%s`.'.freeze

        def_node_matcher :read_write_attribute?, <<-PATTERN
          {
            (send nil :read_attribute _)
            (send nil :write_attribute _ _)
          }
        PATTERN

        def on_send(node)
          read_write_attribute?(node) do
            add_offense(node, :selector)
          end
        end

        def message(node)
          _receiver, method_name, *_args = *node

          if method_name == :read_attribute
            format(MSG, 'self[:attr]', 'read_attribute(:attr)')
          else
            format(MSG, 'self[:attr] = val', 'write_attribute(:attr, val)')
          end
        end

        def autocorrect(node)
          _receiver, method_name, _body = *node

          case method_name
          when :read_attribute
            replacement = read_attribute_replacement(node)
          when :write_attribute
            replacement = write_attribute_replacement(node)
          end

          ->(corrector) { corrector.replace(node.source_range, replacement) }
        end

        private

        def read_attribute_replacement(node)
          _receiver, _method_name, body = *node

          "self[#{body.source}]"
        end

        def write_attribute_replacement(node)
          _receiver, _method_name, *args = *node
          name, value = *args

          "self[#{name.source}] = #{value.source}"
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.47.1 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.47.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.46.0 lib/rubocop/cop/rails/read_write_attribute.rb