Class: Rack::State::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/state.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(store, token, key, options) ⇒ Manager

Instantiated in middleware-land.

store

Storage instance that implements CRUD methods for storing state.

token

Used to reference the state object in store. The token is stored as the cookie value on the client. If nil, token not set on client.

key

Cookie name.

options

Cookie options.



85
86
87
88
89
90
91
# File 'lib/rack/state.rb', line 85

def initialize(store, token, key, options) #:nodoc:
  @object = nil
  @key = key
  @store = store
  @token = token
  @options = options
end

Instance Method Details

#deleteObject

Remove object from store and delete associated token on client.



104
105
106
# File 'lib/rack/state.rb', line 104

def delete
  set false
end

#finish(resp) ⇒ Object

Create, update, or delete the state in the store and set or delete the client’s cookie accordingly.

Note: middleware method not intended for application use.

resp

Rack::Response used to set and delete the client cookie.



116
117
118
119
120
121
122
123
124
# File 'lib/rack/state.rb', line 116

def finish(resp) #:nodoc:
  if token? && object?
    update_state(resp)
  elsif object?
    create_state(resp)
  elsif token? && deleted?
    delete_state(resp)
  end
end

#getObject

Return object if it exists in store, otherwise nil.



94
95
96
# File 'lib/rack/state.rb', line 94

def get
  object
end

#set(object) ⇒ Object

Save object in store and set associated token on client.



99
100
101
# File 'lib/rack/state.rb', line 99

def set(object)
  @object = object
end