class RuboCop::Cop::Rails::ReadWriteAttribute

self = val
x = self[:attr]
# good
write_attribute(:attr, val)
x = read_attribute(:attr)
# bad
@example
is why rubocop recommends using square brackets.
Explicitly raising an error in this situation is preferable, and that
an ‘ActiveModel::MissingAttributeError`.
will return nil, but square brackets will raise
initialized by a partial `select`) then `read_attribute`
If an attribute is missing from the instance (for example, when
methods and recommends square brackets instead.
This cop checks for the use of the `read_attribute` or `write_attribute`

def autocorrect(node)

def autocorrect(node)
  case node.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

def message(node)

def message(node)
  if node.method?(:read_attribute)
    format(MSG, prefer: 'self[:attr]', current: 'read_attribute(:attr)')
  else
    format(MSG, prefer: 'self[:attr] = val',
                current: 'write_attribute(:attr, val)')
  end
end

def on_send(node)

def on_send(node)
  return unless read_write_attribute?(node)
  add_offense(node, location: :selector)
end

def read_attribute_replacement(node)

def read_attribute_replacement(node)
  "self[#{node.first_argument.source}]"
end

def write_attribute_replacement(node)

def write_attribute_replacement(node)
  "self[#{node.first_argument.source}] = #{node.last_argument.source}"
end