Class: DeclarativePolicy::Rule::Ability

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

Overview

A rule constructed from RuleDsl#can?. Computes a different ability on the same subject.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#and, #inspect, make, #negate, #or, #simplify

Constructor Details

#initialize(ability) ⇒ Ability

Returns a new instance of Ability.



150
151
152
# File 'lib/declarative_policy/rule.rb', line 150

def initialize(ability)
  @ability = ability
end

Instance Attribute Details

#abilityObject (readonly)

Returns the value of attribute ability.



148
149
150
# File 'lib/declarative_policy/rule.rb', line 148

def ability
  @ability
end

Instance Method Details

#cached_pass?(context) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'lib/declarative_policy/rule.rb', line 163

def cached_pass?(context)
  runner = context.runner(@ability)
  return unless runner.cached?

  runner.pass?
end

#description(_context) ⇒ Object



170
171
172
# File 'lib/declarative_policy/rule.rb', line 170

def description(_context)
  "User can #{@ability.inspect}"
end

#pass?(context) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/declarative_policy/rule.rb', line 159

def pass?(context)
  context.allowed?(@ability)
end

#reprObject



174
175
176
# File 'lib/declarative_policy/rule.rb', line 174

def repr
  "can?(#{@ability.inspect})"
end

#score(context) ⇒ Object

We ask the ability’s runner for a score



155
156
157
# File 'lib/declarative_policy/rule.rb', line 155

def score(context)
  context.runner(@ability).score
end