Class: DeclarativePolicy::Runner

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

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ Runner

Returns a new instance of Runner.



44
45
46
47
# File 'lib/declarative_policy/runner.rb', line 44

def initialize(steps)
  @steps = steps
  @state = nil
end

Instance Attribute Details

#stepsObject (readonly)

a Runner contains a list of Steps to be run.



42
43
44
# File 'lib/declarative_policy/runner.rb', line 42

def steps
  @steps
end

Instance Method Details

#cached?Boolean

We make sure only to run any given Runner once, and just continue to use the resulting @state that’s left behind.

Returns:

  • (Boolean)


52
53
54
# File 'lib/declarative_policy/runner.rb', line 52

def cached?
  !!@state
end

#debug(out = $stderr) ⇒ Object

see DeclarativePolicy::Base#debug



90
91
92
# File 'lib/declarative_policy/runner.rb', line 90

def debug(out = $stderr)
  run(out)
end

#dependenciesObject



72
73
74
75
76
# File 'lib/declarative_policy/runner.rb', line 72

def dependencies
  return Set.new unless @state

  @state.called_conditions
end

#merge_runner(other) ⇒ Object



68
69
70
# File 'lib/declarative_policy/runner.rb', line 68

def merge_runner(other)
  Runner.new(@steps + other.steps)
end

#pass?Boolean

The main entry point, called for making an ability decision. See #run and DeclarativePolicy::Base#can?

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/declarative_policy/runner.rb', line 80

def pass?
  run unless cached?

  parent_state = Thread.current[:declarative_policy_current_runner_state]
  parent_state&.called_conditions&.merge(@state.called_conditions)

  @state.pass?
end

#scoreObject

used by Rule::Ability. See #steps_by_score



62
63
64
65
66
# File 'lib/declarative_policy/runner.rb', line 62

def score
  return 0 if cached?

  steps.sum(&:score)
end

#uncache!Object

Delete the cached state - allowing this runner to be re-used if the facts have changed.



57
58
59
# File 'lib/declarative_policy/runner.rb', line 57

def uncache!
  @state = nil
end