lib/ultra_config/validator.rb in ultra_config-0.3.0 vs lib/ultra_config/validator.rb in ultra_config-0.4.0
- old
+ new
@@ -6,23 +6,30 @@
class ValidationError < StandardError; end
class TypeValidationError < ValidationError; end
def self.validate(old, new, &validation)
+ @old_value = old
@test_value = new
- type_safety(old) if Settings.type_safety == :strong
self.instance_eval(&validation) if validation
+
+ type_safety(Settings.type_safety) unless @explicit_type_safety
ensure
@test_value = nil
+ @old_value = nil
+ @explicit_type_safety = false
end
- def self.type_safety(old)
- return if old.nil?
- return if old.is_a?(Boolean) && @test_value.is_a?(Boolean)
+ def self.type_safety(type)
+ @explicit_type_safety = true
+ return unless type == :strong
- raise TypeValidationError if old.class != @test_value.class
+ return if @old_value.nil?
+ return if @old_value.is_a?(Boolean) && @test_value.is_a?(Boolean)
+
+ raise TypeValidationError if @old_value.class != @test_value.class
end
def self.one_of(list)
raise ValidationError unless list.include?(@test_value)
end
@@ -31,8 +38,12 @@
raise ValidationError unless regexp.match(@test_value)
end
def self.range(low, high)
raise ValidationError unless (@test_value >= low && @test_value <= high)
+ end
+
+ def self.custom(&block)
+ raise ValidationError unless block.call(@test_value)
end
end
end
\ No newline at end of file