Sha256: 68082829cf22dcab78ea28c9569b8ebfc9b3e82c9df186182fe0291fcfb93a51

Contents?: true

Size: 813 Bytes

Versions: 2

Compression:

Stored size: 813 Bytes

Contents

module UltraConfig
  class Validator
    class ValidationError < StandardError; end
    class TypeValidationError < ValidationError; end

    def self.validate(old, new, &validation)
      @test_value = new
      type_safety(old) if Settings.type_safety == :strong

      self.instance_eval(&validation) if validation
    ensure
      @test_value = nil
    end

    def self.type_safety(old)
      return if old.nil?

      raise TypeValidationError if old.class != @test_value.class
    end

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

    def self.match(regexp)
      raise ValidationError unless regexp.match(@test_value)
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ultra_config-0.2.0 lib/ultra_config/validator.rb
ultra_config-0.1.0 lib/ultra_config/validator.rb