Class: Aequitas::RuleSet
- Inherits:
-
Object
- Object
- Aequitas::RuleSet
- Extended by:
- ValueObject, Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/aequitas/rule_set.rb
Instance Attribute Summary collapse
- #attribute_index ⇒ Object readonly private
- #rules ⇒ Object readonly
Attributes included from ValueObject
Instance Method Summary collapse
- #<<(rule) ⇒ Object
-
#concat(rules) ⇒ self
Assimilate all the rules from another RuleSet into the receiver.
-
#initialize ⇒ RuleSet
constructor
A new instance of RuleSet.
- #inspect ⇒ Object
- #rules_for_resource(resource) ⇒ Object
-
#validate(resource) ⇒ Array(Violation)
Execute all rules in this context against the resource.
Methods included from ValueObject
Constructor Details
#initialize ⇒ RuleSet
Returns a new instance of RuleSet.
30 31 32 33 |
# File 'lib/aequitas/rule_set.rb', line 30 def initialize @rules = Set.new @attribute_index = Hash.new { |h,k| h[k] = [] } end |
Instance Attribute Details
#attribute_index ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 |
# File 'lib/aequitas/rule_set.rb', line 28 def attribute_index @attribute_index end |
#rules ⇒ Object (readonly)
25 26 27 |
# File 'lib/aequitas/rule_set.rb', line 25 def rules @rules end |
Instance Method Details
#<<(rule) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/aequitas/rule_set.rb', line 35 def <<(rule) unless rules.include?(rule) rules << rule attribute_index[rule.attribute_name] << rule end self end |
#concat(rules) ⇒ self
Assimilate all the rules from another RuleSet into the receiver
65 66 67 68 69 |
# File 'lib/aequitas/rule_set.rb', line 65 def concat(rules) rules.each { |rule| self << rule.dup } self end |
#inspect ⇒ Object
71 72 73 |
# File 'lib/aequitas/rule_set.rb', line 71 def inspect "#<#{ self.class } {#{ rules.map { |e| e.inspect }.join( ', ' ) }}>" end |
#rules_for_resource(resource) ⇒ Object
75 76 77 |
# File 'lib/aequitas/rule_set.rb', line 75 def rules_for_resource(resource) rules.select { |r| r.execute?(resource) } end |
#validate(resource) ⇒ Array(Violation)
Execute all rules in this context against the resource.
51 52 53 54 55 56 57 |
# File 'lib/aequitas/rule_set.rb', line 51 def validate(resource) rules = rules_for_resource(resource) rules.map { |rule| rule.validate(resource) }.compact # TODO: # violations = rules.map { |rule| rule.validate(resource) }.compact # ViolationSet.new(resource).concat(violations) end |