Class: Rworkflow::Lifecycle
- Inherits:
-
Object
- Object
- Rworkflow::Lifecycle
- Defined in:
- lib/rworkflow/lifecycle.rb
Direct Known Subclasses
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
-
#default ⇒ Object
Returns the value of attribute default.
-
#initial ⇒ Object
Returns the value of attribute initial.
-
#state_class ⇒ Object
Returns the value of attribute state_class.
-
#state_options ⇒ Object
Returns the value of attribute state_options.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
Class Method Summary collapse
Instance Method Summary collapse
- #concat!(from, name, lifecycle, &state_merge_handler) ⇒ Object
-
#initialize(state_class: State, state_options: {}) {|_self| ... } ⇒ Lifecycle
constructor
A new instance of Lifecycle.
- #rename_state(old_state_name, new_state_name) ⇒ Object
- #serialize ⇒ Object
- #state(name, options = {}) {|new_state| ... } ⇒ Object
- #to_graph ⇒ Object
- #to_h ⇒ Object
- #transition(from, name) ⇒ Object
Constructor Details
#initialize(state_class: State, state_options: {}) {|_self| ... } ⇒ Lifecycle
Returns a new instance of Lifecycle.
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_class = state_class @states = {}.with_indifferent_access @default = nil yield(self) if block_given? end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/rworkflow/lifecycle.rb', line 4 def default @default end |
#initial ⇒ Object
Returns the value of attribute initial.
4 5 6 |
# File 'lib/rworkflow/lifecycle.rb', line 4 def initial @initial end |
#state_class ⇒ Object
Returns the value of attribute state_class.
4 5 6 |
# File 'lib/rworkflow/lifecycle.rb', line 4 def state_class @state_class end |
#state_options ⇒ Object
Returns the value of attribute state_options.
4 5 6 |
# File 'lib/rworkflow/lifecycle.rb', line 4 def @state_options end |
#states ⇒ Object (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. = 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 |
#serialize ⇒ Object
84 85 86 |
# File 'lib/rworkflow/lifecycle.rb', line 84 def serialize return self.class.serialize(self) end |
#state(name, options = {}) {|new_state| ... } ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rworkflow/lifecycle.rb', line 24 def state(name, = {}) = @state_options.merge() new_state = @state_class.new(**) 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 ⇒ Object
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_h ⇒ Object
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
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 |