Class: Longleaf::ConfigurationValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/services/configuration_validator.rb

Overview

Abstract configuration validator class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ConfigurationValidator

Returns a new instance of ConfigurationValidator.



6
7
8
9
# File 'lib/longleaf/services/configuration_validator.rb', line 6

def initialize(config)
  @result = ConfigurationValidationResult.new
  @config = config
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/longleaf/services/configuration_validator.rb', line 4

def result
  @result
end

Instance Method Details

#assert(fail_message, assertion_passed) ⇒ Object

Asserts that the given conditional is true, raising a ConfigurationError if it is not.



20
21
22
# File 'lib/longleaf/services/configuration_validator.rb', line 20

def assert(fail_message, assertion_passed)
  fail(fail_message) unless assertion_passed
end

#fail(fail_message) ⇒ Object

Indicate that validation has failed, throwing a Configuration error with the given message

Raises:



25
26
27
# File 'lib/longleaf/services/configuration_validator.rb', line 25

def fail(fail_message)
  raise ConfigurationError.new(fail_message)
end

#register_error(error) ⇒ Object

Registers an error to the result for this validator



30
31
32
33
34
35
36
# File 'lib/longleaf/services/configuration_validator.rb', line 30

def register_error(error)
  if error.is_a?(StandardError)
    @result.register_error(error.msg)
  else
    @result.register_error(error)
  end
end

#register_on_failureObject

Performs the provided block. If the block produces a ConfigurationError, the error is swallowed and registered to the result



40
41
42
43
44
45
46
# File 'lib/longleaf/services/configuration_validator.rb', line 40

def register_on_failure
  begin
    yield
  rescue ConfigurationError => err
    register_error(err.message)
  end
end

#validate_configConfigurationValidationResult

Verify that the provided configuration is valid

Returns:



13
14
15
16
17
# File 'lib/longleaf/services/configuration_validator.rb', line 13

def validate_config
  register_on_failure { validate }

  @result
end