Class: Aequitas::RuleSet

Inherits:
Object
  • Object
show all
Extended by:
ValueObject, Forwardable
Includes:
Enumerable
Defined in:
lib/aequitas/rule_set.rb

Instance Attribute Summary collapse

Attributes included from ValueObject

#equalizer

Instance Method Summary collapse

Methods included from ValueObject

equalize_on

Constructor Details

#initializeRuleSet

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_indexObject (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

#rulesObject (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

Parameters:

  • rules (RuleSet, Array)

    The other RuleSet (or Array) whose rules are to be assimilated

Returns:

  • (self)


65
66
67
68
69
# File 'lib/aequitas/rule_set.rb', line 65

def concat(rules)
  rules.each { |rule| self << rule.dup }

  self
end

#inspectObject



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.

Parameters:

  • resource (Object)

    the resource to be validated

Returns:

  • (Array(Violation))

    an Array of Violations



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