Class: DeclarativePolicy::Rule::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/rule.rb

Overview

A Rule is the object that results from the `rule` declaration, usually built using the DSL in `RuleDsl`. It is a basic logical combination of building blocks, and is capable of deciding, given a context (instance of DeclarativePolicy::Base) whether it passes or not. Note that this decision doesn't by itself know how that affects the actual ability decision - for that, a `Step` is used.

Direct Known Subclasses

Ability, And, Condition, DelegatedCondition, Not, Or

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(*args) ⇒ Object


13
14
15
# File 'lib/declarative_policy/rule.rb', line 13

def self.make(*args)
  new(*args).simplify
end

Instance Method Details

#and(other) ⇒ Object Also known as: &


47
48
49
# File 'lib/declarative_policy/rule.rb', line 47

def and(other)
  And.make([self, other])
end

#cached_pass?(context) ⇒ Boolean

same as #pass? except refuses to do any I/O, returning nil if the result is not yet cached. used for accurately scoring And/Or

Returns:

  • (Boolean)

27
28
29
# File 'lib/declarative_policy/rule.rb', line 27

def cached_pass?(context)
  raise 'abstract'
end

#inspectObject


59
60
61
# File 'lib/declarative_policy/rule.rb', line 59

def inspect
  "#<Rule #{repr}>"
end

#negateObject Also known as: [email protected]


51
52
53
# File 'lib/declarative_policy/rule.rb', line 51

def negate
  Not.make(self)
end

#or(other) ⇒ Object Also known as: |

convenience combination methods


43
44
45
# File 'lib/declarative_policy/rule.rb', line 43

def or(other)
  Or.make([self, other])
end

#pass?(context) ⇒ Boolean

true or false whether this rule passes. `context` is a policy - an instance of DeclarativePolicy::Base.

Returns:

  • (Boolean)

20
21
22
# File 'lib/declarative_policy/rule.rb', line 20

def pass?(context)
  raise 'abstract'
end

#score(context) ⇒ Object

abstractly, how long would it take to compute this rule? lower-scored rules are tried first.


33
34
35
# File 'lib/declarative_policy/rule.rb', line 33

def score(context)
  raise 'abstract'
end

#simplifyObject

unwrap double negatives and nested and/or


38
39
40
# File 'lib/declarative_policy/rule.rb', line 38

def simplify
  self
end