Class: Himari::RuleProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/himari/rule_processor.rb

Defined Under Namespace

Classes: MissingDecisionError, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, initial_decision) ⇒ RuleProcessor

Returns a new instance of RuleProcessor.

Parameters:



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

#contextObject (readonly)

Returns the value of attribute context.



30
31
32
# File 'lib/himari/rule_processor.rb', line 30

def context
  @context
end

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

#resultObject (readonly)

Returns the value of attribute result.



31
32
33
# File 'lib/himari/rule_processor.rb', line 31

def result
  @result
end

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

Returns:

  • (Boolean)


33
# File 'lib/himari/rule_processor.rb', line 33

def final?; @final; end

#process(rule) ⇒ Object

Parameters:

Raises:



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.user_facing_message = decision.effect_user_facing_message

  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.user_facing_message = decision.effect_user_facing_message
    result.suggestion = decision.effect_suggestion

  else
    raise "Unknown effect #{decision.effect} [BUG]"
  end
end

#run(rules) ⇒ Object

Parameters:



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