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



69
70
71
72
73
74
# File 'lib/linear_work_flow/work_flow.rb', line 69

def actions
  {
    forward: :forward!,
    back: :back!
  }
end

#back!Object

Raises:



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

#back_stateObject



51
52
53
# File 'lib/linear_work_flow/work_flow.rb', line 51

def back_state
  states[index - 1] unless first?
end

#can?(action) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
# File 'lib/linear_work_flow/work_flow.rb', line 59

def can?(action)
  !!restore_after do
    begin
      send(actions[action])
    rescue ChangeStateError
      false
    end
  end
end

#first?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/linear_work_flow/work_flow.rb', line 39

def first?
  index == 0
end

#forward!Object

Raises:



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

#forward_stateObject



47
48
49
# File 'lib/linear_work_flow/work_flow.rb', line 47

def forward_state
  states[index + 1] unless last?
end

#last?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/linear_work_flow/work_flow.rb', line 35

def last?
  state == states.last
end

#permissible_statesObject



43
44
45
# File 'lib/linear_work_flow/work_flow.rb', line 43

def permissible_states
  []
end

#restore_afterObject



80
81
82
83
84
85
# File 'lib/linear_work_flow/work_flow.rb', line 80

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



76
77
78
# File 'lib/linear_work_flow/work_flow.rb', line 76

def states
  self.class.states
end