Sha256: f73211d5d068de21bf6978e4d1b3fee54951c2b0de9141b43fe655279f5ba7f1

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module StateFu

  # TODO document structure / sequence of hooks elsewhere

  module Hooks # :nodoc:

    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

1 entries across 1 versions & 1 rubygems

Version Path
davidlee-state-fu-0.3.1 lib/state_fu/hooks.rb