class RuboCop::Cop::Rails::ReadWriteAttribute
self = val
x = self[:attr]
# good
write_attribute(:attr, val)
x = read_attribute(:attr)
# bad
@example
write_attribute methods.
This cop checks for the use of the read_attribute or
def autocorrect(node)
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
def message(node)
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 on_send(node)
def on_send(node) read_write_attribute?(node) do add_offense(node, :selector) end end
def read_attribute_replacement(node)
def read_attribute_replacement(node) _receiver, _method_name, body = *node "self[#{body.source}]" end
def write_attribute_replacement(node)
def write_attribute_replacement(node) _receiver, _method_name, *args = *node name, value = *args "self[#{name.source}] = #{value.source}" end