Sha256: 56d22c4b36f2641241f78a68838a8be46275958a9c48dd9b715b44ac8759d9c3

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module NxtStateMachine
  class Event
    include NxtRegistry

    def initialize(name, state_machine:, &block)
      @state_machine = state_machine
      @name = name
      @event_transitions = registry("#{name} event transitions")

      configure(&block)

      ensure_event_has_transitions
    end

    attr_reader :name, :state_machine, :event_transitions

    delegate :before_transition,
             :after_transition,
             :around_transition,
             :on_error,
             :on_error!,
             :any_state,
             :all_states,
             :all_states_except,
             to: :state_machine

    def transitions(from:, to:, &block)
      Array(from).each do |from_state|
        transition = Transition::Factory.new(name, from: from_state, to: to, state_machine: state_machine, &block)
        state_machine.transitions << transition
        event_transitions.register(from_state, transition)
      end
    end

    alias_method :transition, :transitions

    private

    def configure(&block)
      instance_exec(&block)
    end

    def ensure_event_has_transitions
      return if event_transitions.size > 0

      raise NxtStateMachine::Errors::EventWithoutTransitions, "No transitions for event :#{name} defined"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nxt_state_machine-0.1.2 lib/nxt_state_machine/event.rb
nxt_state_machine-0.1.1 lib/nxt_state_machine/event.rb