Class: Himari::RuleProcessor
- Inherits:
-
Object
- Object
- Himari::RuleProcessor
- Defined in:
- lib/himari/rule_processor.rb
Defined Under Namespace
Classes: MissingDecisionError, Result
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#initial_decision ⇒ Object
readonly
Returns the value of attribute initial_decision.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #final? ⇒ Boolean
-
#initialize(context, initial_decision) ⇒ RuleProcessor
constructor
A new instance of RuleProcessor.
- #process(rule) ⇒ Object
- #run(rules) ⇒ Object
Constructor Details
#initialize(context, initial_decision) ⇒ RuleProcessor
Returns a new instance of RuleProcessor.
21 22 23 24 25 26 27 28 |
# File 'lib/himari/rule_processor.rb', line 21 def initialize(context, initial_decision) @context = context @initial_decision = initial_decision @result = Result.new(rule_name: nil, allowed: false, explicit_deny: false, decision: nil, decision_log: []) @decision = initial_decision @final = false end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
30 31 32 |
# File 'lib/himari/rule_processor.rb', line 30 def context @context end |
#initial_decision ⇒ Object (readonly)
Returns the value of attribute initial_decision.
30 31 32 |
# File 'lib/himari/rule_processor.rb', line 30 def initial_decision @initial_decision end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
31 32 33 |
# File 'lib/himari/rule_processor.rb', line 31 def result @result end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
30 31 32 |
# File 'lib/himari/rule_processor.rb', line 30 def rules @rules end |
Instance Method Details
#final? ⇒ Boolean
33 |
# File 'lib/himari/rule_processor.rb', line 33 def final?; @final; end |
#process(rule) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/himari/rule_processor.rb', line 36 def process(rule) raise "cannot process rule for finalized result [BUG]" if final? decision = @decision.evolve(rule.name) rule.call(context, decision) raise MissingDecisionError, "rule '#{rule.name}' returned no decision; rule must use one of decision.allow!, deny!, continue!, skip!" unless decision.effect result.decision_log.push(decision) case decision.effect when :allow @decision = decision result.rule_name ||= rule.name result.decision = decision result.allowed = true result.explicit_deny = false result. = decision. when :continue @decision = decision result.decision = decision when :skip # do nothing when :deny @final = true result.rule_name = rule.name result.decision = nil result.allowed = false result.explicit_deny = true result. = decision. result.suggestion = decision.effect_suggestion else raise "Unknown effect #{decision.effect} [BUG]" end end |
#run(rules) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/himari/rule_processor.rb', line 76 def run(rules) rules.each do |rule| process(rule) break if final? end @final = true result.decision ||= @initial_decision unless result.explicit_deny result end |