Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdickey/2e69047fc2722a86c138 to your computer and use it in GitHub Desktop.
Save jdickey/2e69047fc2722a86c138 to your computer and use it in GitHub Desktop.
Support code to demonstrate issue reported on troessner/reek

Tim and team,

This should be pretty straightforward, but here goes. We use module namespaces and nested classes (too?) extensively in our project, but very rarely make direct use of inheritance. One place we do is illustrated by the following two files.

Prolog::Core::User::Validators::Name::Whitespace is a class that, obviously, defines a protocol to check for impermissible whitespace in a string. It has two methods, #invalid_spacing? and #reason, that must be overridden by subclasses that implement the protocol. Such a class is Prolog::Core::User::Validators::Name::TrailingWs; it implements only those two methods, which are declared private as documentation that they'll never be called ordinarily by outside code.

Reek doesn't like that:

$ reek --version
reek 3.9.1
$ reek lib/prolog/core/user/validators/name/trailing_ws.rb
lib/prolog/core/user/validators/name/trailing_ws.rb -- 6 warnings:
  [15]:UnusedPrivateMethod: Prolog::Core::User has the unused private instance method `invalid_spacing?` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
  [19]:UnusedPrivateMethod: Prolog::Core::User has the unused private instance method `reason` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
  [15]:UnusedPrivateMethod: Prolog::Core::User::Validators::Name has the unused private instance method `invalid_spacing?` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
  [19]:UnusedPrivateMethod: Prolog::Core::User::Validators::Name has the unused private instance method `reason` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
  [15]:UnusedPrivateMethod: Prolog::Core::User::Validators::Name::TrailingWs has the unused private instance method `invalid_spacing?` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
  [19]:UnusedPrivateMethod: Prolog::Core::User::Validators::Name::TrailingWs has the unused private instance method `reason` [https://github.com/troessner/reek/blob/master/docs/Unused-Private-Method.md]
$

Almost beside the point is that tests using the code show it works properly, and that Reek is so deeply confused it reports warnings against the class and each containing class. What gives here?

require_relative 'whitespace'
module Prolog
module Core
# This is the base "User" domain logic in Meldd, whether a member or guest.
class User
module Validators
# Validations for a user name, wrapped up in a command-pattern class.
class Name
# Determines if name is invalid due to trailing whitespace.
class TrailingWs < Whitespace
private
def invalid_spacing?
name != name.rstrip
end
def reason
:trailing_whitespace
end
end # class Prolog::Core::User::Validators::Name::TrailingWs
end # class Prolog::Core::User::Validators::Name
end
end # class Prolog::Core::User
end
end
module Prolog
module Core
# This is the base "User" domain logic in Meldd, whether a member or guest.
class User
module Validators
# Validations for a user name, wrapped up in a command-pattern class.
class Name
# Class to DRY up similar whitespace-validation checks for name.
class Whitespace
def initialize(name:, publish:)
@name = name
@publish = publish
end
def call
invalid_spacing?.tap { |ret| report_invalid_spacing if ret }
end
private
attr_reader :name, :publish
def invalid_spacing?
message = 'You MUST override the #invalid_spacing? method!'
fail NoMethodError, message
end
def reason
fail NoMethodError, 'You MUST override the #reason method!'
end
def report_invalid_spacing
publish.call :user_field_invalid, :name, reason, name
end
end # class Prolog::Core::User::Validators::Name::Whitespace
end # class Prolog::Core::User::Validators::Name
end
end # class Prolog::Core::User
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment