Class: RuboCop::Cop::HGOOSTDD::NoTimeInDomain

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/hgoostdd/no_time_in_domain.rb

Overview

Domain code must read time via an injected ‘clock:` collaborator, not via `Time.now` / `Time.current`. See HGOOSTDD §6.

Autocorrect rewrites the call to ‘@clock.now`. This is marked unsafe (SafeAutoCorrect: false in .rubocop.yml) because it assumes the surrounding object has a `@clock` ivar wired by an injected collaborator. After autocorrect, the developer must ensure the constructor accepts `clock:` and assigns `@clock`.

Constant Summary collapse

MSG =
"Inject a `clock:` collaborator; do not call `%{call}` in domain code (§6)."
RESTRICT_ON_SEND =
%i[now current].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rubocop/cop/hgoostdd/no_time_in_domain.rb', line 23

def on_send(node)
  return unless time_send?(node)

  add_offense(node, message: format(MSG, call: node.source)) do |corrector|
    corrector.replace(node, "@clock.now")
  end
end