Skip to content

Commit

Permalink
Prefer #call over #validate. Alias #validate to #call.
Browse files Browse the repository at this point in the history
  • Loading branch information
splattael committed Feb 2, 2010
1 parent 9dd17e0 commit 2ab4e01
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/valuedate.rb
Expand Up @@ -31,7 +31,7 @@ class Value < Valuedate
end

class OptionalValue < Valuedate
def validate(value = nil)
def call(value = nil)
value.nil? || super
end
end
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) }
Expand Down

0 comments on commit 2ab4e01

Please sign in to comment.