Sha256: 69ec23322f8be59d3b6a1bbb414da13c441f7364be1cb1ad792467328e80db4a

Contents?: true

Size: 947 Bytes

Versions: 14

Compression:

Stored size: 947 Bytes

Contents

module Stateflow
  class IncorrectTransition < Exception; end
  
  class Transition
    attr_reader :from, :to, :if, :decide
    
    def initialize(args)
      @from = [args[:from]].flatten
      @to = args[:to]
      @if = args[:if]
      @decide = args[:decide]
    end 
    
    def can_transition?(base)
      return true unless @if
      execute_action(@if, base)
    end
    
    def find_to_state(base)
      raise IncorrectTransition.new("Array of destinations and no decision") if @to.is_a?(Array) && @decide.nil?
      return @to unless @to.is_a?(Array)
      
      to = execute_action(@decide, base)
      
      @to.include?(to) ? to : (raise NoStateFound.new("Decision did not return a state that was set in the 'to' argument"))
    end
      
    private
    def execute_action(action, base)
      case action
      when Symbol, String
        base.send(action)
      when Proc
        action.call(base)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
stateflow-0.4.1 lib/stateflow/transition.rb
stateflow-0.4.0 lib/stateflow/transition.rb
stateflow-0.3.0 lib/stateflow/transition.rb
stateflow-0.2.3 lib/stateflow/transition.rb
stateflow-0.2.2 lib/stateflow/transition.rb
stateflow-0.2.1 lib/stateflow/transition.rb
stateflow-0.2.0 lib/stateflow/transition.rb
stateflow-0.1.2 lib/stateflow/transition.rb
stateflow-0.1.1 lib/stateflow/transition.rb
stateflow-0.1.0 lib/stateflow/transition.rb
stateflow-0.0.4 lib/stateflow/transition.rb
stateflow-0.0.3 lib/stateflow/transition.rb
stateflow-0.0.2 lib/stateflow/transition.rb
stateflow-0.0.1 lib/stateflow/transition.rb