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

#error_message, #should_validate?, #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

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(value)
  lambda.call(value)
end