Class: ValidationProfiler::Rules::ConditionValidationRule

Inherits:
ValidationRule
  • Object
show all
Defined in:
lib/validation_profiler/rules/condition_validation_rule.rb

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

#confirm_expression?(value_a, expression, value_b) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 72

def confirm_expression?(value_a, expression, value_b)
  a = value_a
  b = value_b

  if value_a.is_a?(String)
    a = "'#{value_a}'"
  elsif value_a == nil
    a = 'nil'
  end

  if value_b.is_a?(String)
    b = "'#{value_b}'"
  elsif value_b == nil
    b = 'nil'
  end

  eval("#{a} #{expression} #{b}")
end

#error_message(field, attributes = {}, parent = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 5

def error_message(field, attributes = {}, parent = nil)

  field_name = field.to_s
  if parent != nil
    field_name = "#{parent.to_s}.#{field.to_s}"
  end

  #check if a custom error message has been specified in the attributes
  if attributes[:message] == nil
    #no custom error message has been specified so create the default message.
    "#{field_name} is not valid"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes, parent = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/validation_profiler/rules/condition_validation_rule.rb', line 22

def validate(obj, field, attributes, parent = nil)

  #attempt to get the field value from the object
  value = get_field_value(obj, field)

  condition_field = attributes[:condition_field]
  if condition_field == nil
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  condition_value = attributes[:condition_value]
  if condition_value == nil
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  condition_expression = attributes[:condition_expression]
  if condition_expression == nil
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  field_expression = attributes[:field_expression]
  if field_expression == nil
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  field_value = attributes[:field_value]
  if field_value == nil
    raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
  end

  if !is_required?(value, attributes)
    return true
  end

  condition_field_value = get_field_value(obj, condition_field)

  #check if the condition is valid
  if confirm_expression?(condition_field_value, condition_expression, condition_value)
    #check if the field value is correct for the condition
    if confirm_expression?(value, field_expression, field_value)
      return true
    else
      return false
    end
  end

  return true

end