Sha256: 928c2027f0bcaa86cb4dc4d3f3d2a7e8e22bb76da6d1c0ab166607330b2a69a5

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

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
          return unless [:read_attribute,
                         :write_attribute].include?(method_name)

          add_offense(node, :selector)
        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

11 entries across 11 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/rails/read_write_attribute.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.28.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.27.1 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.27.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.26.1 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.26.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.25.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.24.1 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.24.0 lib/rubocop/cop/rails/read_write_attribute.rb
rubocop-0.23.0 lib/rubocop/cop/rails/read_write_attribute.rb