Class: Rworkflow::State
- Inherits:
-
Object
- Object
- Rworkflow::State
- Defined in:
- lib/rworkflow/state.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_CARDINALITY =
1- DEFAULT_PRIORITY =
nil- STATE_POLICY_WAIT =
To be refactored into Policy objects
:wait- STATE_POLICY_NO_WAIT =
:no_wait
Instance Attribute Summary collapse
-
#cardinality ⇒ Object
Returns the value of attribute cardinality.
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Class Method Summary collapse
Instance Method Summary collapse
- #==(state) ⇒ Object
- #clone ⇒ Object
-
#initialize(cardinality: DEFAULT_CARDINALITY, priority: DEFAULT_PRIORITY, policy: STATE_POLICY_NO_WAIT, **_) ⇒ State
constructor
A new instance of State.
- #inspect ⇒ Object
- #merge(state) ⇒ Object
-
#merge!(state) ⇒ Object
Default rule: new state overwrites old state when applicable.
- #perform(name, default = nil) ⇒ Object
- #serialize ⇒ Object
- #to_graph ⇒ Object
- #to_h ⇒ Object
- #transition(name, to) ⇒ Object
Constructor Details
#initialize(cardinality: DEFAULT_CARDINALITY, priority: DEFAULT_PRIORITY, policy: STATE_POLICY_NO_WAIT, **_) ⇒ State
Returns a new instance of State.
13 14 15 16 17 18 19 |
# File 'lib/rworkflow/state.rb', line 13 def initialize(cardinality: DEFAULT_CARDINALITY, priority: DEFAULT_PRIORITY, policy: STATE_POLICY_NO_WAIT, **_) @cardinality = cardinality @priority = priority @policy = policy @transitions = {} end |
Instance Attribute Details
#cardinality ⇒ Object
Returns the value of attribute cardinality.
10 11 12 |
# File 'lib/rworkflow/state.rb', line 10 def cardinality @cardinality end |
#policy ⇒ Object
Returns the value of attribute policy.
10 11 12 |
# File 'lib/rworkflow/state.rb', line 10 def policy @policy end |
#priority ⇒ Object
Returns the value of attribute priority.
10 11 12 |
# File 'lib/rworkflow/state.rb', line 10 def priority @priority end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
11 12 13 |
# File 'lib/rworkflow/state.rb', line 11 def transitions @transitions end |
Class Method Details
.serialize(state) ⇒ Object
89 90 91 |
# File 'lib/rworkflow/state.rb', line 89 def serialize(state) return state.to_h end |
.unserialize(state_hash) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/rworkflow/state.rb', line 93 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
#==(state) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rworkflow/state.rb', line 54 def ==(state) return @cardinality == state.cardinality && @priority == state.priority && @policy == state.policy && @transitions == state.transitions end |
#clone ⇒ Object
48 49 50 51 52 |
# File 'lib/rworkflow/state.rb', line 48 def clone cloned = self.class.new(cardinality: @cardinality, priority: @priority, policy: @policy) @transitions.each { |from, to| cloned.transition(from, to) } return cloned end |
#inspect ⇒ Object
80 81 82 |
# File 'lib/rworkflow/state.rb', line 80 def inspect return "[ Cardinality: #{@cardinality} ; Policy: #{@policy} ; Priority: #{@priority} ] -> #{to_graph.to_s}" end |
#merge(state) ⇒ Object
44 45 46 |
# File 'lib/rworkflow/state.rb', line 44 def merge(state) return self.clone.merge!(state) end |
#merge!(state) ⇒ Object
Default rule: new state overwrites old state when applicable
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rworkflow/state.rb', line 32 def merge!(state) @cardinality = state.cardinality @priority = state.priority @policy = state.policy @transitions.merge!(state.transitions) do |_, _, transition| transition end return self end |
#perform(name, default = nil) ⇒ Object
25 26 27 28 29 |
# File 'lib/rworkflow/state.rb', line 25 def perform(name, default = nil) to_state = @transitions[name] || default raise TransitionError.new(name) if to_state.nil? return to_state end |
#serialize ⇒ Object
84 85 86 |
# File 'lib/rworkflow/state.rb', line 84 def serialize return self.class.serialize(self) end |
#to_graph ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rworkflow/state.rb', line 70 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 ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/rworkflow/state.rb', line 61 def to_h return { transitions: @transitions, cardinality: @cardinality, priority: @priority, policy: @policy } end |
#transition(name, to) ⇒ Object
21 22 23 |
# File 'lib/rworkflow/state.rb', line 21 def transition(name, to) @transitions[name] = to end |