Sha256: 50aa12af73725698113a77a6b83a271992a21e6fd33876d5706a95bd762230bc

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

class StateHolder < SimpleDelegator
  attr_reader :initial_state

  def initialize( initial_state: nil,
                  state_history: StateHistory.new,
                  state_list: StateList.new,
                  opts: {})
    @state_history = state_history
    @state_list    = state_list
    super
  end

  def start(init_state=initial_state)
    enter_new_state(init_state)
  end

  def transition_to(state)
    leave_old_state
    enter_new_state(state)
  end

  # Convenience methods
  def current_state
    __getobj__
  end

  def history
    state_history.list
  end

  def hx_size_limit
    state_history.hx_size_limit
  end

  private
    attr_reader :state_list, :state_history
    def leave_old_state
      current_state.__send__(:exit)
    end

    def enter_new_state(state)
      self.current_state = state
      state_history << current_state.symbol
      current_state.__send__(:enter)
    end

    def current_state=(state)
      state_obj = state_list[state]
      __setobj__(state_obj)
    end

    def add_state(state_instance)
      state_list.add state_instance
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simplestate-2.0.8 lib/simplestate/state_holder.rb
simplestate-2.0.7 lib/simplestate/state_holder.rb