Class: GemEnforcer::Setup::Helper::OnFailure

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_enforcer/setup/helper/on_failure.rb

Constant Summary collapse

ALLOWED_KEYS =
[LOG_LEVEL = :log_level, FAILURE_BEHAVIOR = :behavior, MESSAGE = :message]
ALLOWED_FAILURE_BEHAVIOR =
[:raise, :exit, DEFAULT_BEHAVIOR = :none]
DEFAULT_LOG_LEVEL =
:error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name:, on_failure:) ⇒ OnFailure

Returns a new instance of OnFailure.



13
14
15
16
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 13

def initialize(gem_name:, on_failure:)
  @gem_name = gem_name
  @on_failure = on_failure.transform_keys(&:to_sym) rescue on_failure
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



7
8
9
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 7

def gem_name
  @gem_name
end

#on_failureObject (readonly)

Returns the value of attribute on_failure.



7
8
9
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 7

def on_failure
  @on_failure
end

Instance Method Details

#errorsObject



22
23
24
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 22

def errors
  @errors ||= []
end

#run_on_failure!(message:, **params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 26

def run_on_failure!(message:, **params)
  unless valid_config?
    raise ConfigError, "Attempted to run on_failure with an invalid config."
  end

  send_message = (provided_message || message) % params
  GemEnforcer.logger.public_send(on_failure_log_level, send_message)

  case on_failure_behavior.to_sym
  when :raise
    raise ValidationError, send_message
  when :exit
    Kernel.exit(1)
  end

  true
end

#valid_config?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/gem_enforcer/setup/helper/on_failure.rb', line 18

def valid_config?
  @valid_config ||= validate_config
end