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



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

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

#can?(action) ⇒ Boolean

Returns:

  • (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

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

#last?Boolean

Returns:

  • (Boolean)


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

def last?
  state == states.last
end

#restore_afterObject



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

#stateObject



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

def state
  states[index]
end

#statesObject



60
61
62
# File 'lib/linear_work_flow/work_flow.rb', line 60

def states
  self.class.states
end