Class: RuboCop::Cop::OpenHAB::StatePredicate

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/openhab/state_predicate.rb

Overview

When comparing against named states and commands, prefer predicate methods.

Examples:

# bad
Switch == ON

# bad
event.command == UP

# good
Switch.on?

# good
event.command.up?

Constant Summary collapse

MSG =
"Use `%<predicate>s?` instead of `== %<constant>s`."
METHODS =
%i[
  REFRESH
  OPEN
  CLOSED
  ON
  OFF
  INCREASE
  DECREASE
  UP
  DOWN
  STOP
  MOVE
  PLAYING
  PAUSED
  REWINDING
  FASTFORWARDING
  PLAY
  PAUSE
  REWIND
  FASTFORWARD
  NEXT
  PREVIOUS
].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#message(constant) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/openhab/state_predicate.rb', line 51

def message(constant)
  format(MSG, predicate: constant.downcase, constant: constant)
end

#on_send(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/openhab/state_predicate.rb', line 55

def on_send(node)
  bad_comparison?(node) { |constant| add_offense(node, message: message(constant)) }
end