Class: HashValidator::Validator::SimpleValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/hash_validator/validators/simple_validator.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#presence_error_message, #should_validate?

Constructor Details

#initialize(name, lambda) ⇒ SimpleValidator

Returns a new instance of SimpleValidator.



5
6
7
8
9
10
11
12
13
# File 'lib/hash_validator/validators/simple_validator.rb', line 5

def initialize(name, lambda)
  # lambda must accept one argument (the value)
  if lambda.arity != 1
    raise StandardError.new("lambda should take only one argument - passed lambda takes #{lambda.arity}")
  end

  super(name)
  self.lambda = lambda
end

Instance Attribute Details

#lambdaObject

Returns the value of attribute lambda.



2
3
4
# File 'lib/hash_validator/validators/simple_validator.rb', line 2

def lambda
  @lambda
end

Instance Method Details

#validate(key, value, _validations, errors) ⇒ Object



15
16
17
18
19
# File 'lib/hash_validator/validators/simple_validator.rb', line 15

def validate(key, value, _validations, errors)
  unless lambda.call(value)
    errors[key] = presence_error_message
  end
end