Sha256: dbb108b7f7897acaffd50fff6b759c67b853f9ffcc53ccedf8fef75d6cafa55c

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require_relative 'transition_builder'

module FiniteMachine
  # A class responsible for merging choice options
  class ChoiceMerger
    # Initialize a ChoiceMerger
    #
    # @param [StateMachine] machine
    # @param [String] name
    # @param [Hash] transitions
    #   the transitions and attributes
    #
    # @api private
    def initialize(machine, name, **transitions)
      @machine     = machine
      @name        = name
      @transitions = transitions
    end

    # Create choice transition
    #
    # @example
    #   event :stop, from: :green do
    #     choice :yellow
    #   end
    #
    # @param [Symbol] to
    #   the to state
    # @param [Hash] conditions
    #   the conditions associated with this choice
    #
    # @return [FiniteMachine::Transition]
    #
    # @api public
    def choice(to, **conditions)
      transition_builder = TransitionBuilder.new(@machine, @name,
                                                 @transitions.merge(conditions))
      transition_builder.call(@transitions[:from] => to)
    end
    alias_method :default, :choice
  end # ChoiceMerger
end # FiniteMachine

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finite_machine-0.12.1 lib/finite_machine/choice_merger.rb
finite_machine-0.12.0 lib/finite_machine/choice_merger.rb