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 ...@@ -206,11 +206,13 @@ module DeclarativePolicy
end end
def cached_pass?(context) def cached_pass?(context)
passes = @rules.map { |r| r.cached_pass?(context) } @rules.each do |rule|
return false if passes.any? { |p| p == false } pass = rule.cached_pass?(context)
return true if passes.all? { |p| p == true }
nil return pass if pass.nil? || pass == false
end
true
end end
def repr def repr
...@@ -245,11 +247,13 @@ module DeclarativePolicy ...@@ -245,11 +247,13 @@ module DeclarativePolicy
end end
def cached_pass?(context) def cached_pass?(context)
passes = @rules.map { |r| r.cached_pass?(context) } @rules.each do |rule|
return true if passes.any? { |p| p == true } pass = rule.cached_pass?(context)
return false if passes.all? { |p| p == false }
nil return pass if pass.nil? || pass == true
end
false
end end
def score(context) 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