Skip to content

Commit

Permalink
Move message composition into checker classes
Browse files Browse the repository at this point in the history
Having the MSG constant in the outer class, and the `#message` in the inner
class seemed like the class was split in the wrong location. Moving it into the
helper classes also means that we can check `RACK_LOADED` only once (in each
class) instead of twice.
  • Loading branch information
bquorning committed Feb 18, 2018
1 parent 8c6a474 commit 87b6b21
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions lib/rubocop/cop/rspec/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class HttpStatus < Cop

include ConfigurableEnforcedStyle

MSG = 'Prefer `%<prefer>s` over `%<current>s` '\
'to describe HTTP status code.'.freeze

def_node_matcher :http_status, <<-PATTERN
(send nil? :have_http_status ${int sym})
PATTERN
Expand Down Expand Up @@ -77,6 +74,11 @@ def checker_class

# :nodoc:
class SymbolicStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
'to describe HTTP status code.'.freeze

attr_reader :node
def initialize(node)
@node = node
Expand All @@ -87,19 +89,19 @@ def offensive?
end

def message
format(MSG, prefer: preferred_style, current: current_style)
if RACK_LOADED
format(MSG, prefer: preferred_style, current: number.to_s)
else
DEFAULT_MSG
end
end

def preferred_style
RACK_LOADED ? symbol.inspect : 'symbolic'
symbol.inspect
end

private

def current_style
RACK_LOADED ? number.to_s : 'numeric'
end

def symbol
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(number)
end
Expand All @@ -111,6 +113,11 @@ def number

# :nodoc:
class NumericStyleChecker
MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
'to describe HTTP status code.'.freeze
DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
'to describe HTTP status code.'.freeze

WHITELIST_STATUS = %i[error success missing redirect].freeze

attr_reader :node
Expand All @@ -123,19 +130,19 @@ def offensive?
end

def message
format(MSG, prefer: preferred_style, current: current_style)
if RACK_LOADED
format(MSG, prefer: preferred_style, current: symbol.inspect)
else
DEFAULT_MSG
end
end

def preferred_style
RACK_LOADED ? number.to_s : 'numeric'
number.to_s
end

private

def current_style
RACK_LOADED ? symbol.inspect : 'symbolic'
end

def number
::Rack::Utils::SYMBOL_TO_STATUS_CODE[symbol]
end
Expand Down

0 comments on commit 87b6b21

Please sign in to comment.