Class: Rworkflow::Lifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/rworkflow/lifecycle.rb

Direct Known Subclasses

SidekiqLifecycle

Constant Summary collapse

CARDINALITY_ALL_STARTED =

Indicates a cardinality equal to the jobs pushed at the start of the workflow

:all_started
DEFAULT_STATE_OPTIONS =
{
  cardinality: State::DEFAULT_CARDINALITY,
  priority: State::DEFAULT_PRIORITY,
  policy: State::STATE_POLICY_NO_WAIT
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_class: State, state_options: {}) {|_self| ... } ⇒ Lifecycle

Returns a new instance of Lifecycle.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
# File 'lib/rworkflow/lifecycle.rb', line 14

def initialize(state_class: State, state_options: {})
  @state_options = DEFAULT_STATE_OPTIONS.merge(state_options)
  @state_class = state_class
  @states = {}
  @default = nil
  yield(self) if block_given?
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



4
5
6
# File 'lib/rworkflow/lifecycle.rb', line 4

def default
  @default
end

#initialObject

Returns the value of attribute initial.



4
5
6
# File 'lib/rworkflow/lifecycle.rb', line 4

def initial
  @initial
end

#state_classObject

Returns the value of attribute state_class.



4
5
6
# File 'lib/rworkflow/lifecycle.rb', line 4

def state_class
  @state_class
end

#state_optionsObject

Returns the value of attribute state_options.



4
5
6
# File 'lib/rworkflow/lifecycle.rb', line 4

def state_options
  @state_options
end

#statesObject (readonly)

Returns the value of attribute states.



3
4
5
# File 'lib/rworkflow/lifecycle.rb', line 3

def states
  @states
end

Class Method Details

.serialize(lifecycle) ⇒ Object



84
85
86
# File 'lib/rworkflow/lifecycle.rb', line 84

def serialize(lifecycle)
  return lifecycle.to_h
end

.unserialize(hash) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rworkflow/lifecycle.rb', line 88

def unserialize(hash)
  return self.new do |lf|
    lf.initial = hash[:initial]
    lf.default = hash[:default]
    lf.state_options = hash[:state_options]
    lf.state_class = hash[:state_class]

    hash[:states].each do |name, state_hash|
      lf.states[name] = lf.state_class.unserialize(state_hash)
    end
  end
end

Instance Method Details

#concat!(from, name, lifecycle, &state_merge_handler) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rworkflow/lifecycle.rb', line 38

def concat!(from, name, lifecycle, &state_merge_handler)
  state_merge_handler ||= lambda do |_, original_state, concat_state|
    original_state.merge(concat_state)
  end

  @states.merge!(lifecycle.states, &state_merge_handler)

  next_state = lifecycle.initial
  @states[from].transition(name, next_state)
  return self
end

#rename_state(old_state_name, new_state_name) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rworkflow/lifecycle.rb', line 50

def rename_state(old_state_name, new_state_name)
  old_state = @states[old_state_name]
  @states[new_state_name] = old_state
  @states.delete(old_state)

  @initial = new_state_name if @initial == old_state_name
end

#serializeObject



79
80
81
# File 'lib/rworkflow/lifecycle.rb', line 79

def serialize
  return self.class.serialize(self)
end

#state(name, options = {}) {|new_state| ... } ⇒ Object

Yields:

  • (new_state)


22
23
24
25
26
27
28
29
# File 'lib/rworkflow/lifecycle.rb', line 22

def state(name, options = {})
  options = @state_options.merge(options)
  new_state = @state_class.new(**options)

  yield(new_state) if block_given?

  @states[name] = new_state
end

#to_graphObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/rworkflow/lifecycle.rb', line 68

def to_graph
  states = @states || []
  return digraph do
    states.each do |from, state|
      state.transitions.each do |transition, to|
        edge(from.to_s, to.to_s).label(transition.to_s)
      end
    end
  end
end

#to_hObject



58
59
60
61
62
63
64
65
66
# File 'lib/rworkflow/lifecycle.rb', line 58

def to_h
  return {
    initial: @initial,
    default: @default,
    state_class: @state_class,
    state_options: @state_options,
    states: Hash[@states.map { |name, state| [name, state.to_h] }]
  }
end

#transition(from, name) ⇒ Object



31
32
33
34
35
36
# File 'lib/rworkflow/lifecycle.rb', line 31

def transition(from, name)
  from_state = @states[from]
  fail(StateError, from) if from_state.nil?

  return from_state.perform(name, @default)
end