class Rack::State::Manager

The Application Interface

Orchestrates the state management of an object. The API is very simple: you can get, set, and delete an object.

The application can access Manager via the Rack environment. See the key middleware option in State.new.

Attributes

key[R]
options[R]
store[R]

Public Instance Methods

delete() click to toggle source

Remove object from store and delete associated token on client.

# File lib/rack/state.rb, line 104
def delete
  set false
end
get() click to toggle source

Return object if it exists in store, otherwise nil.

# File lib/rack/state.rb, line 94
def get
  object
end
set(object) click to toggle source

Save object in store and set associated token on client.

# File lib/rack/state.rb, line 99
def set(object)
  @object = object
end

Private Instance Methods

create_state(resp) click to toggle source
# File lib/rack/state.rb, line 153
def create_state(resp)
  store.create(token(true), object)
  resp.set_cookie(key, options.merge(value: token))
rescue KeyError # token exists
  retry
end
delete_state(resp) click to toggle source
# File lib/rack/state.rb, line 167
def delete_state(resp)
  store.delete(token)
  resp.delete_cookie(key, options)
end
deleted?() click to toggle source

The object has been marked for deletion.

# File lib/rack/state.rb, line 149
def deleted?
  @object == false
end
object() click to toggle source

Load object from store on initial access only.

# File lib/rack/state.rb, line 139
def object
  @object ||= token? ? store.read(token) : nil
end
object?() click to toggle source

Returns true if object has been set directly or loaded from store.

# File lib/rack/state.rb, line 144
def object?
  !!@object
end
token(generate = false) click to toggle source
# File lib/rack/state.rb, line 130
def token(generate = false)
  @token = generate ? SecureRandom.urlsafe_base64 : @token
end
token?() click to toggle source
# File lib/rack/state.rb, line 134
def token?
  ! @token.nil?
end
update_state(resp) click to toggle source
# File lib/rack/state.rb, line 160
def update_state(resp)
  store.update(token, object)
  resp.set_cookie(key, options.merge(value: token)) # update timestamp
rescue KeyError # token nonexistent
  create_state(resp)
end