Commit e9476eb9 authored by Sean McGivern's avatar Sean McGivern

Speed up cached_pass? for composite rules

Both `Or` and `And` would evaluate whether each rule passed, then calculate a
value based on the results of all of those. We can actually return early in many
cases, without running the rule at all.
parent d7717192
......@@ -206,11 +206,13 @@ module DeclarativePolicy
end
def cached_pass?(context)
passes = @rules.map { |r| r.cached_pass?(context) }
return false if passes.any? { |p| p == false }
return true if passes.all? { |p| p == true }
@rules.each do |rule|
pass = rule.cached_pass?(context)
nil
return pass if pass.nil? || pass == false
end
true
end
def repr
......@@ -245,11 +247,13 @@ module DeclarativePolicy
end
def cached_pass?(context)
passes = @rules.map { |r| r.cached_pass?(context) }
return true if passes.any? { |p| p == true }
return false if passes.all? { |p| p == false }
@rules.each do |rule|
pass = rule.cached_pass?(context)
nil
return pass if pass.nil? || pass == true
end
false
end
def score(context)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment