Class: Lutaml::Xsd::Validation::RuleRegistry
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::RuleRegistry
- Defined in:
- lib/lutaml/xsd/validation/rule_registry.rb
Overview
RuleRegistry manages validation rules
This class implements the Registry pattern to manage validation rules. It organizes rules by category and provides methods to register, retrieve, and filter rules based on configuration.
Instance Attribute Summary collapse
-
#config ⇒ ValidationConfiguration
readonly
The validation configuration.
Instance Method Summary collapse
-
#all_rules(enabled_only: true) ⇒ Array<ValidationRule>
Get all registered rules.
-
#applicable_rules ⇒ Array<ValidationRule>
Get applicable rules based on configuration.
-
#categories ⇒ Array<Symbol>
Get all categories with registered rules.
-
#clear ⇒ void
Clear all registered rules.
-
#constraint_rules ⇒ Array<ValidationRule>
Get constraint validation rules.
-
#disable_category(category) ⇒ void
Disable all rules in a category.
-
#enable_category(category) ⇒ void
Enable all rules in a category.
-
#facet_rules ⇒ Array<ValidationRule>
Get facet validation rules.
-
#has_rules_for?(category) ⇒ Boolean
Check if a category has any rules.
-
#identity_rules ⇒ Array<ValidationRule>
Get identity constraint validation rules.
-
#initialize(config) ⇒ RuleRegistry
constructor
Initialize a new RuleRegistry.
-
#inspect ⇒ String
Detailed string representation.
-
#register(rule) ⇒ void
Register a validation rule.
-
#register_all(rules) ⇒ void
Register multiple rules at once.
-
#rule_counts ⇒ Hash<Symbol, Integer>
Get count of rules per category.
-
#rules_for_category(category, enabled_only: true) ⇒ Array<ValidationRule>
Get rules for a specific category.
-
#structure_rules ⇒ Array<ValidationRule>
Get structure validation rules.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation.
-
#type_rules ⇒ Array<ValidationRule>
Get type validation rules.
-
#unregister(rule) ⇒ Boolean
Remove a specific rule.
Constructor Details
#initialize(config) ⇒ RuleRegistry
Initialize a new RuleRegistry
28 29 30 31 32 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 28 def initialize(config) @config = config @rules = [] @rules_by_category = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#config ⇒ ValidationConfiguration (readonly)
Returns The validation configuration.
23 24 25 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 23 def config @config end |
Instance Method Details
#all_rules(enabled_only: true) ⇒ Array<ValidationRule>
Get all registered rules
62 63 64 65 66 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 62 def all_rules(enabled_only: true) rules = @rules rules = rules.select(&:enabled?) if enabled_only rules end |
#applicable_rules ⇒ Array<ValidationRule>
Get applicable rules based on configuration
175 176 177 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 175 def applicable_rules all_rules.select { |rule| rule.applicable?(@config) } end |
#categories ⇒ Array<Symbol>
Get all categories with registered rules
117 118 119 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 117 def categories @rules_by_category.keys end |
#clear ⇒ void
This method returns an undefined value.
Clear all registered rules
141 142 143 144 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 141 def clear @rules.clear @rules_by_category.clear end |
#constraint_rules ⇒ Array<ValidationRule>
Get constraint validation rules
96 97 98 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 96 def constraint_rules rules_for_category(:constraint) end |
#disable_category(category) ⇒ void
This method returns an undefined value.
Disable all rules in a category
168 169 170 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 168 def disable_category(category) @rules_by_category[category].each(&:disable!) end |
#enable_category(category) ⇒ void
This method returns an undefined value.
Enable all rules in a category
160 161 162 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 160 def enable_category(category) @rules_by_category[category].each(&:enable!) end |
#facet_rules ⇒ Array<ValidationRule>
Get facet validation rules
103 104 105 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 103 def facet_rules rules_for_category(:facet) end |
#has_rules_for?(category) ⇒ Boolean
Check if a category has any rules
125 126 127 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 125 def has_rules_for?(category) rules_for_category(category).any? end |
#identity_rules ⇒ Array<ValidationRule>
Get identity constraint validation rules
110 111 112 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 110 def identity_rules rules_for_category(:identity) end |
#inspect ⇒ String
Detailed string representation
202 203 204 205 206 207 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 202 def inspect "#<#{self.class.name} " \ "total=#{@rules.size} " \ "enabled=#{all_rules.size} " \ "categories=#{categories.inspect}>" end |
#register(rule) ⇒ void
This method returns an undefined value.
Register a validation rule
40 41 42 43 44 45 46 47 48 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 40 def register(rule) validate_rule!(rule) @rules << rule @rules_by_category[rule.category] << rule # Sort by priority after adding @rules_by_category[rule.category].sort_by!(&:priority) end |
#register_all(rules) ⇒ void
This method returns an undefined value.
Register multiple rules at once
54 55 56 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 54 def register_all(rules) rules.each { |rule| register(rule) } end |
#rule_counts ⇒ Hash<Symbol, Integer>
Get count of rules per category
132 133 134 135 136 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 132 def rule_counts categories.each_with_object({}) do |category, counts| counts[category] = rules_for_category(category).size end end |
#rules_for_category(category, enabled_only: true) ⇒ Array<ValidationRule>
Get rules for a specific category
73 74 75 76 77 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 73 def rules_for_category(category, enabled_only: true) rules = @rules_by_category[category] rules = rules.select(&:enabled?) if enabled_only rules end |
#structure_rules ⇒ Array<ValidationRule>
Get structure validation rules
82 83 84 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 82 def structure_rules rules_for_category(:structure) end |
#to_h ⇒ Hash
Convert to hash representation
182 183 184 185 186 187 188 189 190 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 182 def to_h { total_rules: @rules.size, enabled_rules: all_rules.size, categories: categories, rule_counts: rule_counts, rules: @rules.map(&:to_h), } end |
#to_s ⇒ String
String representation
195 196 197 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 195 def to_s "RuleRegistry(#{@rules.size} rules, #{categories.size} categories)" end |
#type_rules ⇒ Array<ValidationRule>
Get type validation rules
89 90 91 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 89 def type_rules rules_for_category(:type) end |
#unregister(rule) ⇒ Boolean
Remove a specific rule
150 151 152 153 154 |
# File 'lib/lutaml/xsd/validation/rule_registry.rb', line 150 def unregister(rule) removed = @rules.delete(rule) @rules_by_category[rule.category].delete(rule) if removed !removed.nil? end |