Sha256: 614170ecb34e5a2fccbd405cbd87e75263ebafac6630e2fe160711cabfa6b872

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

require_relative 'utilities/boolean'

module UltraConfig
  module Validation
    include Boolean

    class ValidationError < StandardError
      attr_reader :config
      attr_reader :value

      def initialize(msg = nil, config = [], value = nil)
        @value = value
        @config = config.join('.')

        super(msg)
      end
    end

    class TypeValidationError < ValidationError; end

    def type_safety(type)
      @type_safety_checked = true

      return unless type == :strong
      return if @value.nil?
      return if @value.is_a?(Boolean) && @intermediate_value.is_a?(Boolean)

      raise TypeValidationError if @value.class != @intermediate_value.class
    end

    def one_of(list)
      raise ValidationError unless list.include?(@intermediate_value)
    end

    def match(regexp)
      raise ValidationError unless regexp.match(@intermediate_value)
    end

    def range(low, high)
      raise ValidationError unless (@intermediate_value >= low && @intermediate_value <= high)
    end

    def custom(&block)
      raise ValidationError unless block.call(@intermediate_value)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ultra_config-0.15.3 lib/ultra_config/validation.rb
ultra_config-0.15.1 lib/ultra_config/validation.rb
ultra_config-0.15.0 lib/ultra_config/validation.rb
ultra_config-0.14.0 lib/ultra_config/validation.rb
ultra_config-0.13.0 lib/ultra_config/validation.rb