class HashValidator::Validator::LambdaValidator

Public Class Methods

new() click to toggle source
Calls superclass method HashValidator::Validator::Base::new
# File lib/hash_validator/validators/lambda_validator.rb, line 2
def initialize
  super('_lambda')  # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
end

Public Instance Methods

presence_error_message() click to toggle source
# File lib/hash_validator/validators/lambda_validator.rb, line 18
def presence_error_message
  'is not valid'
end
should_validate?(rhs) click to toggle source
# File lib/hash_validator/validators/lambda_validator.rb, line 6
def should_validate?(rhs)
  if rhs.is_a?(Proc)
    if rhs.arity == 1
      true
    else
      raise HashValidator::Validator::LambdaValidator::InvalidArgumentCount.new("Lambda validator should only accept one argument, supplied lambda accepts #{rhs.arity}.")
    end
  else
    false
  end
end
validate(key, value, lambda, errors) click to toggle source
# File lib/hash_validator/validators/lambda_validator.rb, line 22
def validate(key, value, lambda, errors)
  unless lambda.call(value)
    errors[key] = presence_error_message
  end

rescue
  errors[key] = presence_error_message
end