Class: Validation::Rule::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/validation/rule/type.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Type

Returns a new instance of Type.



5
6
7
# File 'lib/validation/rule/type.rb', line 5

def initialize(params = {})
  @params = params
end

Instance Method Details

#error_keyObject



9
10
11
# File 'lib/validation/rule/type.rb', line 9

def error_key
  :type_rule
end

#paramsObject



36
37
38
# File 'lib/validation/rule/type.rb', line 36

def params
  @params
end

#valid_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/validation/rule/type.rb', line 13

def valid_value?(value)
  return true if value.nil? && !params[:required]
  
  valid = true
  if params[:array]
    valid = false unless value.all? {|v| validate_type(v) }
  else
    valid = false unless validate_type(value)
  end
  valid
end

#validate_type(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/validation/rule/type.rb', line 25

def validate_type(value)
  valid = true
  case params[:classes]
  when Array
    valid = false unless params[:classes].any? {|type| value.is_a?(type) }
  else
    valid = false unless value.is_a?(params[:classes])
  end
  valid
end