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
- #can?(action) ⇒ Boolean
- #first? ⇒ Boolean
- #forward! ⇒ Object
-
#initialize(state = nil) ⇒ WorkFlow
constructor
A new instance of WorkFlow.
- #last? ⇒ Boolean
- #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
53 54 55 56 57 58 |
# File 'lib/linear_work_flow/work_flow.rb', line 53 def actions { forward: :forward!, back: :back! } end |
#back! ⇒ Object
30 31 32 33 |
# File 'lib/linear_work_flow/work_flow.rb', line 30 def back! raise(ChangeStateError, "Cannot go back from first state") if first? self.index -= 1 end |
#can?(action) ⇒ Boolean
43 44 45 46 47 48 49 50 51 |
# File 'lib/linear_work_flow/work_flow.rb', line 43 def can?(action) !!restore_after do begin send(actions[action]) rescue ChangeStateError false end end end |
#first? ⇒ Boolean
39 40 41 |
# File 'lib/linear_work_flow/work_flow.rb', line 39 def first? index == 0 end |
#forward! ⇒ Object
25 26 27 28 |
# 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 end |
#last? ⇒ Boolean
35 36 37 |
# File 'lib/linear_work_flow/work_flow.rb', line 35 def last? state == states.last end |
#restore_after ⇒ Object
64 65 66 67 68 69 |
# File 'lib/linear_work_flow/work_flow.rb', line 64 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
60 61 62 |
# File 'lib/linear_work_flow/work_flow.rb', line 60 def states self.class.states end |