Module: DataShifter::Internal::SideEffectGuards

Defined in:
lib/data_shifter/internal/side_effect_guards.rb

Overview

Applies and restores side-effect guards during dry runs so that HTTP, mail, and job enqueues are blocked (or faked) unless explicitly allowed.

Production impact:

  • WebMock: required only when apply_webmock runs (i.e. during a dry run), so commit-only production runs never load WebMock. On restore we revert to the previous state (enable! or disable!) so e.g. specs that had WebMock enabled are not left with it disabled.

  • ActionMailer / ActiveJob / Sidekiq: no extra loading; we only toggle existing config for the duration of the block and restore in ensure (Sidekiq restores prior fake/inline/disable).

Class Method Summary collapse

Class Method Details

.with_guards(shift_class:, &block) ⇒ Object

Applies side-effect guards, yields, then restores. Call only when running in dry run.



19
20
21
22
23
24
25
26
27
28
# File 'lib/data_shifter/internal/side_effect_guards.rb', line 19

def with_guards(shift_class:, &block)
  saved = {}
  apply_guards(shift_class, saved)
  block.call
rescue webmock_net_connect_error => e
  host = extract_host_from_webmock_message(e.message)
  raise DataShifter::ExternalRequestNotAllowedError.new(attempted_host: host), cause: e
ensure
  restore_guards(saved) if saved.any?
end