Class: CircuitBreaker::WorkflowBuilder::DSL::Workflow

Inherits:
Object
  • Object
show all
Includes:
PrettyPrint
Defined in:
lib/circuit_breaker/workflow_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PrettyPrint

#pretty_print

Constructor Details

#initialize(states:, transitions:, before_flows:, rules:) ⇒ Workflow

Returns a new instance of Workflow.



310
311
312
313
314
315
316
# File 'lib/circuit_breaker/workflow_dsl.rb', line 310

def initialize(states:, transitions:, before_flows:, rules:)
  @states = states
  @transitions = transitions
  @before_flows = before_flows
  @rules = rules
  @tokens = []
end

Instance Attribute Details

#before_flowsObject (readonly)

Returns the value of attribute before_flows.



308
309
310
# File 'lib/circuit_breaker/workflow_dsl.rb', line 308

def before_flows
  @before_flows
end

#rulesObject (readonly)

Returns the value of attribute rules.



308
309
310
# File 'lib/circuit_breaker/workflow_dsl.rb', line 308

def rules
  @rules
end

#statesObject (readonly)

Returns the value of attribute states.



308
309
310
# File 'lib/circuit_breaker/workflow_dsl.rb', line 308

def states
  @states
end

#tokensObject (readonly)

Returns the value of attribute tokens.



308
309
310
# File 'lib/circuit_breaker/workflow_dsl.rb', line 308

def tokens
  @tokens
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



308
309
310
# File 'lib/circuit_breaker/workflow_dsl.rb', line 308

def transitions
  @transitions
end

Instance Method Details

#add_token(token) ⇒ Object



318
319
320
321
# File 'lib/circuit_breaker/workflow_dsl.rb', line 318

def add_token(token)
  token.state = @states.first if token.state.nil?
  @tokens << token
end

#transition!(token, transition_name) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/circuit_breaker/workflow_dsl.rb', line 323

def transition!(token, transition_name)
  transition = @transitions.find { |t| t.name == transition_name }
  raise "Invalid transition '#{transition_name}'" unless transition
  raise "Invalid from state '#{token.state}'" unless token.state == transition.from_state

  old_state = token.state
  @current_token = token  # Set current token

  # Then validate rules
  transition.validate_rules(token, @rules)

  # Execute actions first
  transition.execute_actions(token)

  # Finally update state
  token.state = transition.to_state
  token.record_transition(transition_name, old_state, token.state)

  @current_token = nil  # Clear current token
  token
end