Sha256: 61e887d23de6f412b3e2a06f45cb0b1186bc5d58aff6042e2f8b5bf8874ddd97

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

module Apotomo::Transition
  
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    # Defines a transition for an implicit invoke.
    #
    # Usually when a container widget renders its kids there is no <tt>state</tt> passed to the
    # kid's #invoke ("implicit invoke") and thus the kid will enter its start state again.
    # You can customize that behaviour by setting a transition to let the widget jump to the 
    # defined <tt>:to</tt> state in place of the start state.
    #
    # Example:
    #   class Kid < MouseWidget
    #     transition :from => :sleep, :to => :snore
    #
    # Next time when mum renders and kid is in <tt>:sleep</tt> state kid will not return to its 
    # start state but invoke <tt>:snore</tt>.
    #
    #   class Kid < MouseWidget
    #     transition :in => :snore
    #
    # In subsequent render cycles from mum kid will keep snoring whereas other kids would go back
    # to the start state.
    def transition(options)
      if from = options[:from]
        class_transitions[from] = options[:to]
      elsif loop = options[:in]
        transition :from => loop, :to => loop
      end
    end
    
    def class_transitions
      @class_transitions ||= {}
    end
  end
  
  protected
    # Returns the next state for <tt>state</tt> or nil. A next state must have been defined 
    # with #transition.
    def next_state_for(state)
      self.class.class_transitions[state]
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apotomo-0.1.4 lib/apotomo/transition.rb
apotomo-0.1.3 lib/apotomo/transition.rb
apotomo-0.1.2 lib/apotomo/transition.rb
apotomo-0.1.1 lib/apotomo/transition.rb