Class: Rack::Session::Abstract::SessionHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rack/session/abstract/id.rb

Overview

SessionHash is responsible to lazily load the session from store.

Constant Summary collapse

Unspecified =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, req) ⇒ SessionHash


35
36
37
38
39
# File 'lib/rack/session/abstract/id.rb', line 35

def initialize(store, req)
  @store = store
  @req = req
  @loaded = false
end

Instance Attribute Details

#idObject


41
42
43
44
# File 'lib/rack/session/abstract/id.rb', line 41

def id
  return @id if @loaded or instance_variable_defined?(:@id)
  @id = @store.send(:extract_session_id, @req)
end

Class Method Details

.find(req) ⇒ Object


23
24
25
# File 'lib/rack/session/abstract/id.rb', line 23

def self.find(req)
  req.get_header RACK_SESSION
end

.set(req, session) ⇒ Object


27
28
29
# File 'lib/rack/session/abstract/id.rb', line 27

def self.set(req, session)
  req.set_header RACK_SESSION, session
end

.set_options(req, options) ⇒ Object


31
32
33
# File 'lib/rack/session/abstract/id.rb', line 31

def self.set_options(req, options)
  req.set_header RACK_SESSION_OPTIONS, options.dup
end

Instance Method Details

#[](key) ⇒ Object


55
56
57
58
# File 'lib/rack/session/abstract/id.rb', line 55

def [](key)
  load_for_read!
  @data[key.to_s]
end

#[]=(key, value) ⇒ Object Also known as: store


76
77
78
79
# File 'lib/rack/session/abstract/id.rb', line 76

def []=(key, value)
  load_for_write!
  @data[key.to_s] = value
end

#clearObject


82
83
84
85
# File 'lib/rack/session/abstract/id.rb', line 82

def clear
  load_for_write!
  @data.clear
end

#delete(key) ⇒ Object


108
109
110
111
# File 'lib/rack/session/abstract/id.rb', line 108

def delete(key)
  load_for_write!
  @data.delete(key.to_s)
end

#destroyObject


87
88
89
90
# File 'lib/rack/session/abstract/id.rb', line 87

def destroy
  clear
  @id = @store.send(:delete_session, @req, id, options)
end

#each(&block) ⇒ Object


50
51
52
53
# File 'lib/rack/session/abstract/id.rb', line 50

def each(&block)
  load_for_read!
  @data.each(&block)
end

#empty?Boolean


131
132
133
134
# File 'lib/rack/session/abstract/id.rb', line 131

def empty?
  load_for_read!
  @data.empty?
end

#exists?Boolean


121
122
123
124
125
# File 'lib/rack/session/abstract/id.rb', line 121

def exists?
  return @exists if instance_variable_defined?(:@exists)
  @data = {}
  @exists = @store.send(:session_exists?, @req)
end

#fetch(key, default = Unspecified, &block) ⇒ Object


60
61
62
63
64
65
66
67
# File 'lib/rack/session/abstract/id.rb', line 60

def fetch(key, default=Unspecified, &block)
  load_for_read!
  if default == Unspecified
    @data.fetch(key.to_s, &block)
  else
    @data.fetch(key.to_s, default, &block)
  end
end

#has_key?(key) ⇒ Boolean Also known as: key?, include?


69
70
71
72
# File 'lib/rack/session/abstract/id.rb', line 69

def has_key?(key)
  load_for_read!
  @data.has_key?(key.to_s)
end

#inspectObject


113
114
115
116
117
118
119
# File 'lib/rack/session/abstract/id.rb', line 113

def inspect
  if loaded?
    @data.inspect
  else
    "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>"
  end
end

#keysObject


136
137
138
139
# File 'lib/rack/session/abstract/id.rb', line 136

def keys
  load_for_read!
  @data.keys
end

#loaded?Boolean


127
128
129
# File 'lib/rack/session/abstract/id.rb', line 127

def loaded?
  @loaded
end

#optionsObject


46
47
48
# File 'lib/rack/session/abstract/id.rb', line 46

def options
  @req.session_options
end

#replace(hash) ⇒ Object


103
104
105
106
# File 'lib/rack/session/abstract/id.rb', line 103

def replace(hash)
  load_for_write!
  @data.replace(stringify_keys(hash))
end

#to_hashObject


92
93
94
95
# File 'lib/rack/session/abstract/id.rb', line 92

def to_hash
  load_for_read!
  @data.dup
end

#update(hash) ⇒ Object Also known as: merge!


97
98
99
100
# File 'lib/rack/session/abstract/id.rb', line 97

def update(hash)
  load_for_write!
  @data.update(stringify_keys(hash))
end

#valuesObject


141
142
143
144
# File 'lib/rack/session/abstract/id.rb', line 141

def values
  load_for_read!
  @data.values
end