class RuboCop::Cop::Rails::ReadWriteAttribute
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
Constants
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 40 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
message(node)
click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 30 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
on_send(node)
click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 21 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
Private Instance Methods
read_attribute_replacement(node)
click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 55 def read_attribute_replacement(node) _receiver, _method_name, body = *node "self[#{body.source}]" end
write_attribute_replacement(node)
click to toggle source
# File lib/rubocop/cop/rails/read_write_attribute.rb, line 61 def write_attribute_replacement(node) _receiver, _method_name, *args = *node name, value = *args "self[#{name.source}] = #{value.source}" end