Class: LinearWorkFlow::WorkFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/linear_work_flow/work_flow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state = nil) ⇒ WorkFlow

Returns a new instance of WorkFlow.

Raises:



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

#indexObject

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

.statesObject



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

#actionsObject



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

Raises:



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_stateObject



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

Raises:



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_stateObject



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_statesObject



45
46
47
# File 'lib/linear_work_flow/work_flow.rb', line 45

def permissible_states
  []
end

#restore_afterObject



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

#stateObject



21
22
23
# File 'lib/linear_work_flow/work_flow.rb', line 21

def state
  states[index]
end

#statesObject



78
79
80
# File 'lib/linear_work_flow/work_flow.rb', line 78

def states
  self.class.states
end