Class: HashValidator::Validator::DynamicFuncValidator

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

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#should_validate?, #validate

Constructor Details

#initialize(name, func, error_message = nil) ⇒ DynamicFuncValidator

Returns a new instance of DynamicFuncValidator.



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

def initialize(name, func, error_message = nil)
  super(name)
  
  unless func.respond_to?(:call)
    raise ArgumentError, "Function must be callable (proc or lambda)"
  end
  
  @func = func
  @custom_error_message = error_message
end

Instance Attribute Details

#custom_error_messageObject

Returns the value of attribute custom_error_message.



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

def custom_error_message
  @custom_error_message
end

#funcObject

Returns the value of attribute func.



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

def func
  @func
end

Instance Method Details

#error_messageObject



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

def error_message
  @custom_error_message || super
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/hash_validator/validators/dynamic_func_validator.rb', line 19

def valid?(value)
  begin
    !!@func.call(value)
  rescue => e
    false
  end
end