Class: ValidationProfiler::Rules::Manager

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

Overview

This is the manager class that holds all registered validation rules.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



12
13
14
15
16
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 12

def initialize
  @rules = []
  ValidationProfiler::Rules::Manager.instance = self
  load_rules
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



9
10
11
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 9

def instance
  @instance
end

Instance Method Details

#add_rule(key, rule) ⇒ Object

This method is called to add a validation rule to the manager for use.

Parameters:

  • key (Symbol)

    This is the key to register the validation rule for.

  • rule (ClassName)

    This is the class name of the validation rule to register.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 37

def add_rule(key, rule)

  instance = rule.new

  #verify the rule instance inherits ValidationRule
  if instance == nil || !instance.is_a?(ValidationRule)
    raise ValidationProfiler::Exceptions::InvalidValidationRuleType.new(instance.class)
  end

  #verify the rule name has not already been registered
  if !@rules.select { |r| r[:key] == key }.empty?
    raise ValidationProfiler::Exceptions::ValidationRuleAlreadyExists.new(key)
  end

  @rules.push({ key: key, instance: instance})

end

#get_rule(key) ⇒ ValidationRule

This method is called to get a validation rule by it’s registered key.

Returns:



23
24
25
26
27
28
29
30
31
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 23

def get_rule(key)

  results = @rules.select { |r| r[:key] == key }
  if !results.empty?
    results[0][:instance]
  else
    raise ValidationProfiler::Exceptions::ValidationRuleNotFound.new(key)
  end
end