Sha256: 59fb133a9bc88e899c52783af275f463a89f46e327dfe1df127c6ce413a12112
Contents?: true
Size: 1.49 KB
Versions: 5
Compression:
Stored size: 1.49 KB
Contents
module FSM # # A State has a name and a list of outgoing transitions. # class State include FSM::Options::InstanceMethods attr_reader(:name, :transitions) # name: a symbol which identifies this state # options # * :enter : a symbol or string or Proc # * :exit : a symbol or string or Proc def initialize(name, options = {}) raise ArgumentError.new('Name is required') unless name assert_options(options, [:enter, :exit]) @name = name @enter = Executable.new options[:enter] if options.has_key?(:enter) @exit = Executable.new options[:exit] if options.has_key?(:exit) @transitions = {} end # Called when this state is entered def enter(target) @enter.execute(target) if @enter nil end # Called when this state is exited def exit(target) @exit.execute(target) if @exit nil end def add_transition(transition) raise ArgumentError.new("#{self} already has a transition to '#{transition.name}'") if @transitions.has_key?(transition.name) raise ArgumentError.new("the transition '#{transition.name}' is already defined") if @transitions.detect() {|to_name, tr| transition.name == tr.name} @transitions[transition.to.name] = transition end # All states that are reachable form this state by one hop def to_states @transitions.map { |to_name, transition| transition.to} end def to_s "State '#{self.name}'" end end end
Version data entries
5 entries across 5 versions & 1 rubygems