Sha256: 52fd55b6fd19b3cc03f09e4ddd7149a27985e381f1ee900852600f66cf6f1b1e

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop makes sure that accessor methods are named properly.
      #
      # @example
      #   # bad
      #   def set_attribute(value) ...
      #
      #   # good
      #   def attribute=(value)
      #
      #   # bad
      #   def get_attribute ...
      #
      #   # good
      #   def attribute ...
      class AccessorMethodName < Cop
        include CheckMethods

        private

        def check(node, method_name, args, _body)
          if bad_reader_name?(method_name.to_s, args)
            add_offense(node, :name,
                        'Do not prefix reader method names with `get_`.')
          elsif bad_writer_name?(method_name.to_s, args)
            add_offense(node, :name,
                        'Do not prefix writer method names with `set_`.')
          end
        end

        def bad_reader_name?(method_name, args)
          method_name.start_with?('get_') && args.to_a.empty?
        end

        def bad_writer_name?(method_name, args)
          method_name.start_with?('set_') && args.to_a.one?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.21.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.20.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.20.0 lib/rubocop/cop/style/accessor_method_name.rb