Sha256: bb21ca7370e1991a8da728fff8403c3b53f3281cdc2644db3d78789a5e47a6b1

Contents?: true

Size: 827 Bytes

Versions: 9

Compression:

Stored size: 827 Bytes

Contents

module NxtStateMachine
  class CallbackRegistry
    include ::NxtRegistry

    def register(from, to, kind, 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).kind(kind) << method_or_block
        end
      end
    end

    def resolve(transition, kind = nil)
      all_callbacks = callbacks.from(transition.from.enum).to(transition.to.enum)
      return all_callbacks unless kind

      all_callbacks.kind(kind)
    end

    private

    def callbacks
      @callbacks ||= registry :from do
        nested :to do
          nested :kind, default: -> { [] } do
            attrs :before, :after
          end
        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/callback_registry.rb
nxt_state_machine-0.1.7 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.6 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.5 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.4 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.3 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.2 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.1 lib/nxt_state_machine/callback_registry.rb
nxt_state_machine-0.1.0 lib/nxt_state_machine/callback_registry.rb