class DeclarativePolicy::Rule::And

Logical `and`, containing a list of rules. Only passes if all of them do.

Attributes

rules[R]

Public Class Methods

new(rules) click to toggle source
# File lib/declarative_policy/rule.rb, line 184
def initialize(rules)
  @rules = rules
end

Public Instance Methods

cached_pass?(context) click to toggle source
# File lib/declarative_policy/rule.rb, line 216
def cached_pass?(context)
  @rules.each do |rule|
    pass = rule.cached_pass?(context)

    return pass if pass.nil? || pass == false
  end

  true
end
pass?(context) click to toggle source
# File lib/declarative_policy/rule.rb, line 207
def pass?(context)
  # try to find a cached answer before
  # checking in order
  cached = cached_pass?(context)
  return cached unless cached.nil?

  @rules.all? { |r| r.pass?(context) }
end
repr() click to toggle source
# File lib/declarative_policy/rule.rb, line 226
def repr
  "all?(#{rules.map(&:repr).join(', ')})"
end
score(context) click to toggle source
# File lib/declarative_policy/rule.rb, line 200
def score(context)
  return 0 unless cached_pass?(context).nil?

  # note that cached rules will have score 0 anyways.
  @rules.sum { |r| r.score(context) }
end
simplify() click to toggle source
# File lib/declarative_policy/rule.rb, line 188
def simplify
  simplified_rules = @rules.flat_map do |rule|
    simplified = rule.simplify
    case simplified
    when And then simplified.rules
    else [simplified]
    end
  end

  And.new(simplified_rules)
end