Sha256: 2b82fe51604eda1d42796fabcbd146f7249630e0119df820f604306f46f0cc53

Contents?: true

Size: 810 Bytes

Versions: 2

Compression:

Stored size: 810 Bytes

Contents

class StateHolder < SimpleDelegator
  def initialize(opts={})
    # Set current_state to nil state within SimpleDelegator
    nil_state = NilState.new(nil,nil)
    super(nil_state)
    # Then transition to the initial state class
    initial_state_class = opts.fetch :start_in,
                         (opts.fetch :initial_state_class, nil)
    transition_to initial_state_class
  end

  def transition_to(new_state_class)
    current_state.send(:exit)
    set_new_state(new_state_class)
    current_state.send(:enter)
  end

  def current_state
    __getobj__
  end

  def set_new_state(new_state_class)
    new_state = new_state_class.new(self, current_state.class)
    self.current_state = new_state
  end

private
  def current_state=(state)
    __setobj__(state)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simplestate-0.3.1 lib/simplestate/state_holder.rb
simplestate-0.3.0 lib/simplestate/state_holder.rb