Class: ValidationProfiler::Rules::Manager
- Inherits:
-
Object
- Object
- ValidationProfiler::Rules::Manager
- 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 ⇒ Object
Returns the value of attribute instance.
Instance Method Summary collapse
-
#add_rule(key, rule) ⇒ Object
This method is called to add a validation rule to the manager for use.
-
#get_rule(key) ⇒ ValidationRule
This method is called to get a validation rule by it’s registered key.
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
Constructor Details
#initialize ⇒ Manager
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
.instance ⇒ Object
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.
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.
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 |