Class: Rworkflow::State

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

Direct Known Subclasses

SidekiqState

Constant Summary collapse

DEFAULT_CARDINALITY =
1
STATE_POLICY_WAIT =

To be refactored into Policy objects

:wait
STATE_POLICY_NO_WAIT =
:no_wait

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cardinality: DEFAULT_CARDINALITY, policy: STATE_POLICY_NO_WAIT, **_) ⇒ State



12
13
14
15
16
# File 'lib/rworkflow/state.rb', line 12

def initialize(cardinality: DEFAULT_CARDINALITY, policy: STATE_POLICY_NO_WAIT, **_)
  @cardinality = cardinality
  @policy = policy
  @transitions = {}
end

Instance Attribute Details

#cardinalityObject

Returns the value of attribute cardinality.



9
10
11
# File 'lib/rworkflow/state.rb', line 9

def cardinality
  @cardinality
end

#policyObject

Returns the value of attribute policy.



9
10
11
# File 'lib/rworkflow/state.rb', line 9

def policy
  @policy
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



10
11
12
# File 'lib/rworkflow/state.rb', line 10

def transitions
  @transitions
end

Class Method Details

.serialize(state) ⇒ Object



81
82
83
# File 'lib/rworkflow/state.rb', line 81

def serialize(state)
  return state.to_h
end

.unserialize(state_hash) ⇒ Object



85
86
87
88
89
90
91
92
93
# 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

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/rworkflow/state.rb', line 50

def ==(other)
  return @cardinality == other.cardinality && @policy == other.policy && @transitions == other.transitions
end

#cloneObject



44
45
46
47
48
# 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

#inspectObject



72
73
74
# File 'lib/rworkflow/state.rb', line 72

def inspect
  return "[ Cardinality: #{@cardinality} ; Policy: #{@policy} ] -> #{to_graph}"
end

#merge(state) ⇒ Object



40
41
42
# File 'lib/rworkflow/state.rb', line 40

def merge(state)
  return self.clone.merge!(state)
end

#merge!(state) ⇒ Object

Default rule: new state overwrites old state when applicable



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

Raises:



22
23
24
25
26
# 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

#serializeObject



76
77
78
# File 'lib/rworkflow/state.rb', line 76

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

#to_graphObject



62
63
64
65
66
67
68
69
70
# 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_hObject



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

def to_h
  return {
    transitions: @transitions,
    cardinality: @cardinality,
    policy: @policy
  }
end

#transition(name, to) ⇒ Object



18
19
20
# File 'lib/rworkflow/state.rb', line 18

def transition(name, to)
  @transitions[name] = to
end