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.
This commit is contained in:
Sean McGivern 2017-10-04 14:13:22 +01:00
parent d771719278
commit e9476eb97a

View file

@ -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)