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.



79
80
81
82
83
84
85
# File 'lib/rack/state.rb', line 79

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.



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

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.



110
111
112
113
114
115
116
117
118
# File 'lib/rack/state.rb', line 110

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.



88
89
90
# File 'lib/rack/state.rb', line 88

def get
  object
end

#set(object) ⇒ Object

Save object in store and set associated token on client.



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

def set(object)
  @object = object
end