Sha256: 87fc74fdda1b6709fabc17fd5a4950f7b0fe56be8b8fee67829575019670c2a1

Contents?: true

Size: 929 Bytes

Versions: 9

Compression:

Stored size: 929 Bytes

Contents

module NxtStateMachine
  class ErrorCallbackRegistry
    include ::NxtRegistry

    def register(from, to, error, method = nil, block = nil)
      method_or_block = method || block
      return unless method_or_block

      Array(from).each do |from_state|
        Array(to).each do |to_state|
          callbacks.from(from_state).to(to_state).error(error, method_or_block)
        end
      end
    end

    def resolve(error, transition)
      candidate = callbacks.from(
        transition.from.enum
      ).to(
        transition.to.enum
      ).error.keys.find { |kind_of_error| error.is_a?(kind_of_error) }

      return unless candidate

      callbacks.from(transition.from.enum).to(transition.to.enum).error(candidate)
    end

    private

    def callbacks
      @callbacks ||= registry :from do
        nested :to do
          nested :error, transform_keys: false, call: false
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
nxt_state_machine-0.1.8 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.7 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.6 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.5 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.4 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.3 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.2 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.1 lib/nxt_state_machine/error_callback_registry.rb
nxt_state_machine-0.1.0 lib/nxt_state_machine/error_callback_registry.rb