Sha256: 48c0709678155303c5c6c1dc2ffca6e354170b66cabd84d92371af6222ee38aa

Contents?: true

Size: 1.13 KB

Versions: 16

Compression:

Stored size: 1.13 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 OnMethodDef

        private

        def on_method_def(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

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.35.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.34.2 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.34.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.34.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.33.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.32.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.32.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.31.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.30.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.30.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.29.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.29.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.28.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.27.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.27.0 lib/rubocop/cop/style/accessor_method_name.rb