class Rworkflow::Lifecycle
Constants
- CARDINALITY_ALL_STARTED
- DEFAULT_CARDINALITY
- DEFAULT_STATE_OPTIONS
- RESERVED_STATE_NAMES
- STATE_POLICY_NO_WAIT
Attributes
default[RW]
initial[RW]
state_class[RW]
state_options[RW]
states[R]
Public Class Methods
new(state_class: State, state_options: {}) { |self| ... }
click to toggle source
# 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
serialize(lifecycle)
click to toggle source
# File lib/rworkflow/lifecycle.rb, line 89 def serialize(lifecycle) return lifecycle.to_h end
unserialize(hash)
click to toggle source
# 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
Public Instance Methods
concat!(from, name, lifecycle, &state_merge_handler)
click to toggle source
# 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)
click to toggle source
# 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
serialize()
click to toggle source
# File lib/rworkflow/lifecycle.rb, line 84 def serialize return self.class.serialize(self) end
state(name, options = {}) { |new_state| ... }
click to toggle source
# 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_graph()
click to toggle source
# 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_h()
click to toggle source
# 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)
click to toggle source
# 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