From 2ab4e0176ac3adf5e1dddd415450f0da1d7abc93 Mon Sep 17 00:00:00 2001 From: Peter Suschlik Date: Tue, 2 Feb 2010 13:22:58 +0100 Subject: [PATCH] Prefer #call over #validate. Alias #validate to #call. --- lib/valuedate.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/valuedate.rb b/lib/valuedate.rb index a6dc600..5994f96 100644 --- a/lib/valuedate.rb +++ b/lib/valuedate.rb @@ -31,7 +31,7 @@ class Value < Valuedate end class OptionalValue < Valuedate - def validate(value = nil) + def call(value = nil) value.nil? || super end end @@ -56,18 +56,16 @@ def hash(schema={}) end end - def validate(value = nil) + def call(value = nil) @errors.clear @validators.all? { |validator| validator.call(value) || collect_errors!(validator) } end + alias :validate :call - def validate!(value = nil) - validate(value) or raise ValidationFailed.new(@errors) - end - - def call(value) - validate(value) + def call!(value = nil) + call(value) or raise ValidationFailed.new(@errors) end + alias :validate! :call! def valid?(&block) @validators << block @@ -106,7 +104,11 @@ class << self attr_reader :matchers def validate(value, &block) - schema(&block).validate(value) + schema(&block).call(value) + end + + def validate!(value, &block) + schema(&block).call!(value) end def schema(&block) @@ -124,7 +126,7 @@ def matcher(name, &block) Valuedate.matcher(:equals) { |value, expected| value == expected } Valuedate.matcher(:is_a) { |value, expected| value.is_a?(expected) } Valuedate.matcher(:any) do |value, *validators| - validators.any? { |validator| validator.validate(value) } + validators.any? { |validator| validator.call(value) } end Valuedate.matcher(:in) { |value, expected| expected.include?(value) } Valuedate.matcher(:is) { |*value, &block| block.call(*value) }