Class: ValidationProfiler::Manager

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

Instance Method Summary collapse

Instance Method Details

#validate(obj, profile, parent = nil) ⇒ ValidationManagerResult

Called to validate an object against a validation profile.

Parameters:

  • obj (Object)

    The object to validate

  • profile (ClassName)

    The class name of the validation profile to validate against

Returns:

  • (ValidationManagerResult)

    The result of the validation



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
# File 'lib/validation_profiler.rb', line 40

def validate(obj, profile, parent = nil)

  result = ValidationProfiler::ManagerResult.new

  validation_rules = profile.class_variable_get(:@@validation_rules)

  validation_rules.each do |r|

    if ValidationProfiler::Rules::Manager.instance == nil
      ValidationProfiler::Rules::Manager.new
    end

    rule = ValidationProfiler::Rules::Manager.instance.get_rule(r[:name])
    outcome = rule.validate(obj, r[:field], r[:attributes], parent)

    if outcome.is_a?(ValidationProfiler::ManagerResult) && !outcome.outcome
      result.errors = result.errors + outcome.errors
      result.outcome = false
    elsif !outcome
      result.outcome = false
      result.errors.push({ field: r[:field], message: rule.error_message(r[:field], r[:attributes], parent) })
    end

  end

  result

end