Sha256: 237272170a43c4cf52187859864b2e02efbeddcbe48470b22050b23d55111455

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

module NxtStateMachine
  class Event
    include NxtRegistry

    def initialize(name, state_machine, **options, &block)
      @state_machine = state_machine
      @name = name
      @event_transitions = registry("#{name} event transitions")
      @names = Event::Names.build(name)
      @options = options.with_indifferent_access

      configure(&block)

      ensure_event_has_transitions
    end

    attr_reader :name, :state_machine, :event_transitions, :names, :options

    delegate :before_transition,
             :after_transition,
             :around_transition,
             :on_success,
             :on_error,
             :on_error!,
             :any_state,
             :all_states,
             :all_states_except,
             :defuse,
             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

    def transitions_from?(state)
      event_transitions.resolve(state).present?
    end

    def to_s
      "#{self.class.name}[:#{name}]"
    end

    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

4 entries across 4 versions & 1 rubygems

Version Path
nxt_state_machine-0.1.12 lib/nxt_state_machine/event.rb
nxt_state_machine-0.1.11 lib/nxt_state_machine/event.rb
nxt_state_machine-0.1.10 lib/nxt_state_machine/event.rb
nxt_state_machine-0.1.9 lib/nxt_state_machine/event.rb