Class: ValidationRuleManager

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

#initializeValidationRuleManager

Returns a new instance of ValidationRuleManager.



10
11
12
13
14
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 10

def initialize
  @rules = []
  ValidationRuleManager.instance = self
  load_rules
end

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



7
8
9
# File 'lib/validation_profiler/rules/validation_rule_manager.rb', line 7

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.



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

def add_rule(key, rule)

  instance = rule.new

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

  #verify the rule name has not already been registered
  if !@rules.select { |r| r[:key] == key }.empty?
    raise RuleAlreadyExists.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:



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

def get_rule(key)

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