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.


34
35
36
37
# File 'lib/declarative_policy/runner.rb', line 34

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

Instance Attribute Details

#stepsObject (readonly)

a Runner contains a list of Steps to be run.


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

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)

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

def cached?
  !!@state
end

#debug(out = $stderr) ⇒ Object

see DeclarativePolicy::Base#debug


66
67
68
# File 'lib/declarative_policy/runner.rb', line 66

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

#merge_runner(other) ⇒ Object


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

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)

59
60
61
62
63
# File 'lib/declarative_policy/runner.rb', line 59

def pass?
  run unless cached?

  @state.pass?
end

#scoreObject

used by Rule::Ability. See #steps_by_score


47
48
49
50
51
# File 'lib/declarative_policy/runner.rb', line 47

def score
  return 0 if cached?

  steps.map(&:score).inject(0, :+)
end