Sha256: 2df708fd7a39d973c8c5ecbbfbc75875ea00ca9a5b0428eb7ef51a26123860d3

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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

  # 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 transition_to(state)
    leave_old_state
    enter_new_state(state)
  end

  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

1 entries across 1 versions & 1 rubygems

Version Path
simplestate-2.0.9 lib/simplestate/state_holder.rb