Sha256: 2034617773e88fa39183486a5d45f418ab2bff9ab2f8c7cbb75dd97c239078f6

Contents?: true

Size: 1011 Bytes

Versions: 3

Compression:

Stored size: 1011 Bytes

Contents

# encoding: utf-8

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

        def on_send(node)
          receiver, method_name, *_args = *node

          return if receiver

          if [:read_attribute, :write_attribute].include?(method_name)
            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, var)')
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.20.1 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.20.0 lib/rubocop/cop/rails/read_write_attribute.rb