Class: LinearWorkFlow::WorkFlow
- Inherits:
-
Object
- Object
- LinearWorkFlow::WorkFlow
- Defined in:
- lib/linear_work_flow/work_flow.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
Returns the value of attribute index.
Class Method Summary collapse
Instance Method Summary collapse
- #actions ⇒ Object
- #back! ⇒ Object
- #back_state ⇒ Object
- #can?(action) ⇒ Boolean
- #first? ⇒ Boolean
- #forward! ⇒ Object
- #forward_state ⇒ Object
-
#initialize(state = nil) ⇒ WorkFlow
constructor
A new instance of WorkFlow.
- #last? ⇒ Boolean
- #permissible_states ⇒ Object
- #restore_after ⇒ Object
- #state ⇒ Object
- #states ⇒ Object
Constructor Details
#initialize(state = nil) ⇒ WorkFlow
Returns a new instance of WorkFlow.
13 14 15 16 |
# File 'lib/linear_work_flow/work_flow.rb', line 13 def initialize(state=nil) self.index = state ? states.index(state) : 0 raise(InvalidStateError, "State must be in: #{states.inspect}") unless self.index end |
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
11 12 13 |
# File 'lib/linear_work_flow/work_flow.rb', line 11 def index @index end |
Class Method Details
.states ⇒ Object
4 5 6 7 8 9 |
# File 'lib/linear_work_flow/work_flow.rb', line 4 def self.states [ :first, :last ] end |
Instance Method Details
#actions ⇒ Object
71 72 73 74 75 76 |
# File 'lib/linear_work_flow/work_flow.rb', line 71 def actions { forward: :forward!, back: :back! } end |
#back! ⇒ Object
31 32 33 34 35 |
# File 'lib/linear_work_flow/work_flow.rb', line 31 def back! raise(ChangeStateError, "Cannot go back from first state") if first? self.index -= 1 state end |
#back_state ⇒ Object
53 54 55 |
# File 'lib/linear_work_flow/work_flow.rb', line 53 def back_state states[index - 1] unless first? end |
#can?(action) ⇒ Boolean
61 62 63 64 65 66 67 68 69 |
# File 'lib/linear_work_flow/work_flow.rb', line 61 def can?(action) !!restore_after do begin send(actions[action]) rescue ChangeStateError false end end end |
#first? ⇒ Boolean
41 42 43 |
# File 'lib/linear_work_flow/work_flow.rb', line 41 def first? index == 0 end |
#forward! ⇒ Object
25 26 27 28 29 |
# File 'lib/linear_work_flow/work_flow.rb', line 25 def forward! raise(ChangeStateError, "Cannot go forward from last state") if last? self.index += 1 state end |
#forward_state ⇒ Object
49 50 51 |
# File 'lib/linear_work_flow/work_flow.rb', line 49 def forward_state states[index + 1] unless last? end |
#last? ⇒ Boolean
37 38 39 |
# File 'lib/linear_work_flow/work_flow.rb', line 37 def last? state == states.last end |
#permissible_states ⇒ Object
45 46 47 |
# File 'lib/linear_work_flow/work_flow.rb', line 45 def permissible_states [] end |
#restore_after ⇒ Object
82 83 84 85 86 87 |
# File 'lib/linear_work_flow/work_flow.rb', line 82 def restore_after starting_index = index result = yield self.index = starting_index result end |
#state ⇒ Object
21 22 23 |
# File 'lib/linear_work_flow/work_flow.rb', line 21 def state states[index] end |
#states ⇒ Object
78 79 80 |
# File 'lib/linear_work_flow/work_flow.rb', line 78 def states self.class.states end |