Class: CircuitBreaker::WorkflowBuilder::DSL::Transition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, from_state, to_state, workflow_builder) ⇒ Transition

Returns a new instance of Transition.



233
234
235
236
237
238
239
240
241
# File 'lib/circuit_breaker/workflow_dsl.rb', line 233

def initialize(name, from_state, to_state, workflow_builder)
  @name = name
  @from_state = from_state
  @to_state = to_state
  @rules = []
  @action_context = nil
  @action_builder = nil
  @workflow_builder = workflow_builder
end

Instance Attribute Details

#action_builderObject

Returns the value of attribute action_builder.



231
232
233
# File 'lib/circuit_breaker/workflow_dsl.rb', line 231

def action_builder
  @action_builder
end

#action_contextObject

Returns the value of attribute action_context.



231
232
233
# File 'lib/circuit_breaker/workflow_dsl.rb', line 231

def action_context
  @action_context
end

#from_stateObject (readonly)

Returns the value of attribute from_state.



230
231
232
# File 'lib/circuit_breaker/workflow_dsl.rb', line 230

def from_state
  @from_state
end

#nameObject (readonly)

Returns the value of attribute name.



230
231
232
# File 'lib/circuit_breaker/workflow_dsl.rb', line 230

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



230
231
232
# File 'lib/circuit_breaker/workflow_dsl.rb', line 230

def rules
  @rules
end

#to_stateObject (readonly)

Returns the value of attribute to_state.



230
231
232
# File 'lib/circuit_breaker/workflow_dsl.rb', line 230

def to_state
  @to_state
end

#workflow_builderObject (readonly)

Returns the value of attribute workflow_builder.



230
231
232
# File 'lib/circuit_breaker/workflow_dsl.rb', line 230

def workflow_builder
  @workflow_builder
end

Instance Method Details

#actions(&block) ⇒ Object



243
244
245
246
247
# File 'lib/circuit_breaker/workflow_dsl.rb', line 243

def actions(&block)
  @action_builder = ActionBuilder.new(@workflow_builder)
  @action_builder.instance_eval(&block) if block_given?
  @action_context = @action_builder.context
end

#execute_actions(token) ⇒ Object



253
254
255
# File 'lib/circuit_breaker/workflow_dsl.rb', line 253

def execute_actions(token)
  @action_builder&.execute_actions(token)
end

#policy(rules) ⇒ Object



249
250
251
# File 'lib/circuit_breaker/workflow_dsl.rb', line 249

def policy(rules)
  @rules = rules
end

#validate_rules(token, rules_dsl) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/circuit_breaker/workflow_dsl.rb', line 257

def validate_rules(token, rules_dsl)
  return unless @rules
  
  rules_dsl.with_context(@action_context) do
    # Handle :all rules
    if @rules[:all]
      @rules[:all].each do |rule|
        raise "Rule '#{rule}' failed for transition '#{name}'" unless rules_dsl.evaluate(rule, token)
      end
    end

    # Handle :any rules
    if @rules[:any]
      any_passed = @rules[:any].any? { |rule| rules_dsl.evaluate(rule, token) }
      raise "None of the rules #{@rules[:any]} passed for transition '#{name}'" unless any_passed
    end
  end
end