Class: HashValidator::Validator::LambdaValidator
- Inherits:
-
Base
- Object
- Base
- HashValidator::Validator::LambdaValidator
show all
- Defined in:
- lib/hash_validator/validators/lambda_validator.rb
Defined Under Namespace
Classes: InvalidArgumentCount
Instance Attribute Summary
Attributes inherited from Base
#name
Instance Method Summary
collapse
Methods inherited from Base
#validate
Constructor Details
Returns a new instance of LambdaValidator.
2
3
4
|
# File 'lib/hash_validator/validators/lambda_validator.rb', line 2
def initialize
super('_lambda') end
|
Instance Method Details
#error_message ⇒ Object
18
19
20
|
# File 'lib/hash_validator/validators/lambda_validator.rb', line 18
def error_message
'is not valid'
end
|
#should_validate?(rhs) ⇒ Boolean
6
7
8
9
10
11
12
13
14
15
16
|
# 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
|
#valid?(value, lambda) ⇒ Boolean
22
23
24
25
26
|
# File 'lib/hash_validator/validators/lambda_validator.rb', line 22
def valid?(value, lambda)
lambda.call(value)
rescue
false
end
|