class Rworkflow::State

Constants

DEFAULT_CARDINALITY
STATE_POLICY_NO_WAIT
STATE_POLICY_WAIT

To be refactored into Policy objects

Attributes

cardinality[RW]
policy[RW]
transitions[R]

Public Class Methods

new(cardinality: DEFAULT_CARDINALITY, policy: STATE_POLICY_NO_WAIT, **_) click to toggle source
# File lib/rworkflow/state.rb, line 12
def initialize(cardinality: DEFAULT_CARDINALITY, policy: STATE_POLICY_NO_WAIT, **_)
  @cardinality = cardinality
  @policy = policy
  @transitions = {}
end
serialize(state) click to toggle source
# File lib/rworkflow/state.rb, line 81
def serialize(state)
  return state.to_h
end
unserialize(state_hash) click to toggle source
# File lib/rworkflow/state.rb, line 85
def unserialize(state_hash)
  state = self.new(**state_hash)

  state_hash[:transitions].each do |from, to|
    state.transition(from, to)
  end

  return state
end

Public Instance Methods

==(other) click to toggle source
# File lib/rworkflow/state.rb, line 50
def ==(other)
  return @cardinality == other.cardinality && @policy == other.policy && @transitions == other.transitions
end
clone() click to toggle source
# File lib/rworkflow/state.rb, line 44
def clone
  cloned = self.class.new(cardinality: @cardinality, policy: @policy)
  @transitions.each { |from, to| cloned.transition(from, to) }
  return cloned
end
inspect() click to toggle source
# File lib/rworkflow/state.rb, line 72
def inspect
  return "[ Cardinality: #{@cardinality} ; Policy: #{@policy} ] -> #{to_graph}"
end
merge(state) click to toggle source
# File lib/rworkflow/state.rb, line 40
def merge(state)
  return self.clone.merge!(state)
end
merge!(state) click to toggle source

Default rule: new state overwrites old state when applicable

# File lib/rworkflow/state.rb, line 29
def merge!(state)
  @cardinality = state.cardinality
  @policy = state.policy

  @transitions.merge!(state.transitions) do |_, _, transition|
    transition
  end

  return self
end
perform(name, default = nil) click to toggle source
# File lib/rworkflow/state.rb, line 22
def perform(name, default = nil)
  to_state = @transitions[name] || default
  raise(TransitionError, name) if to_state.nil?
  return to_state
end
serialize() click to toggle source
# File lib/rworkflow/state.rb, line 76
def serialize
  return self.class.serialize(self)
end
to_graph() click to toggle source
# File lib/rworkflow/state.rb, line 62
def to_graph
  transitions = @transitions # need to capture for block, as digraph rebinds context

  return digraph do
    transitions.each do |transition, to|
      edge('self', to.to_s).label(transition.to_s)
    end
  end
end
to_h() click to toggle source
# File lib/rworkflow/state.rb, line 54
def to_h
  return {
    transitions: @transitions,
    cardinality: @cardinality,
    policy: @policy
  }
end
transition(name, to) click to toggle source
# File lib/rworkflow/state.rb, line 18
def transition(name, to)
  @transitions[name] = to
end