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
RESERVED_STATE_NAMES =
[Rworkflow::Flow::STATE_FAILED, Rworkflow::Flow::STATE_SUCCESSFUL].map(&:to_s).freeze
DEFAULT_CARDINALITY =
State::DEFAULT_CARDINALITY
STATE_POLICY_NO_WAIT =
State::STATE_POLICY_NO_WAIT
DEFAULT_STATE_OPTIONS =
{
  cardinality: self::DEFAULT_CARDINALITY,
  policy: self::STATE_POLICY_NO_WAIT
}.freeze

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:



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

def initialize(state_class: State, state_options: {})
  @state_options = DEFAULT_STATE_OPTIONS.merge(state_options)
  @state_class = state_class
  @states = {}.with_indifferent_access
  @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



89
90
91
# File 'lib/rworkflow/lifecycle.rb', line 89

def serialize(lifecycle)
  return lifecycle.to_h
end

.unserialize(hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rworkflow/lifecycle.rb', line 93

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



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rworkflow/lifecycle.rb', line 43

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



55
56
57
58
59
60
61
# File 'lib/rworkflow/lifecycle.rb', line 55

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



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

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

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

Yields:

  • (new_state)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rworkflow/lifecycle.rb', line 24

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

  raise ArgumentError, 'given state name is a reserved state name' if RESERVED_STATE_NAMES.include?(name.to_s)
  raise ArgumentError, 'no two states can have the same name in a lifecycle' if @states.key?(name)

  yield(new_state) if block_given?

  @states[name] = new_state
end

#to_graphObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/rworkflow/lifecycle.rb', line 73

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



63
64
65
66
67
68
69
70
71
# File 'lib/rworkflow/lifecycle.rb', line 63

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

Raises:



36
37
38
39
40
41
# File 'lib/rworkflow/lifecycle.rb', line 36

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

  return from_state.perform(name, @default)
end