Sha256: b9ef27c5192d0642d393384adc566b633114ad9efad3fc7418f47cd2e6215650

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module StateFu
  module Hooks

    ALL_HOOKS = [[:event,  :before],   # good place to start a transaction, etc
                 [:origin, :exit],     # say goodbye!
                 [:event,  :execute],  # do stuff here, as a rule of thumb
                 [:target, :entry],    # last chance to halt!
                 [:event,  :after],    # clean up all the mess
                 [:target, :accepted]] # state changed. Quicksave!

    EVENT_HOOKS = ALL_HOOKS.select { |type, name| type == :event }
    STATE_HOOKS = ALL_HOOKS - EVENT_HOOKS
    HOOK_NAMES  = ALL_HOOKS.map {|a| a[1] }

    # just turn the above into what each class needs
    # and make it into a nice hash: { :name =>[ hook, ... ], ... }
    def self.for( me )
      x = if    me.is_a?( StateFu::State ); STATE_HOOKS
          elsif me.is_a?( StateFu::Event ); EVENT_HOOKS
          else  {}
          end.
        map { |_,name| [name, [].extend( StateFu::OrderedHash )] }
      hash = x.inject({}) {|h, a| h[a[0]] = a[1] ; h}
      hash.extend( StateFu::OrderedHash ).freeze
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
davidlee-state-fu-0.0.1 lib/state_fu/hooks.rb
davidlee-state-fu-0.0.2 lib/state_fu/hooks.rb
davidlee-state-fu-0.2.0 lib/state_fu/hooks.rb