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


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

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

Instance Attribute Details

#idObject


43
44
45
46
# File 'lib/rack/session/abstract/id.rb', line 43

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


25
26
27
# File 'lib/rack/session/abstract/id.rb', line 25

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

.set(req, session) ⇒ Object


29
30
31
# File 'lib/rack/session/abstract/id.rb', line 29

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

.set_options(req, options) ⇒ Object


33
34
35
# File 'lib/rack/session/abstract/id.rb', line 33

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

Instance Method Details

#[](key) ⇒ Object


57
58
59
60
# File 'lib/rack/session/abstract/id.rb', line 57

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

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


78
79
80
81
# File 'lib/rack/session/abstract/id.rb', line 78

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

#clearObject


84
85
86
87
# File 'lib/rack/session/abstract/id.rb', line 84

def clear
  load_for_write!
  @data.clear
end

#delete(key) ⇒ Object


110
111
112
113
# File 'lib/rack/session/abstract/id.rb', line 110

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

#destroyObject


89
90
91
92
# File 'lib/rack/session/abstract/id.rb', line 89

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

#each(&block) ⇒ Object


52
53
54
55
# File 'lib/rack/session/abstract/id.rb', line 52

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

#empty?Boolean


133
134
135
136
# File 'lib/rack/session/abstract/id.rb', line 133

def empty?
  load_for_read!
  @data.empty?
end

#exists?Boolean


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

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

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


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

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?


71
72
73
74
# File 'lib/rack/session/abstract/id.rb', line 71

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

#inspectObject


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

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

#keysObject


138
139
140
141
# File 'lib/rack/session/abstract/id.rb', line 138

def keys
  load_for_read!
  @data.keys
end

#loaded?Boolean


129
130
131
# File 'lib/rack/session/abstract/id.rb', line 129

def loaded?
  @loaded
end

#optionsObject


48
49
50
# File 'lib/rack/session/abstract/id.rb', line 48

def options
  @req.session_options
end

#replace(hash) ⇒ Object


105
106
107
108
# File 'lib/rack/session/abstract/id.rb', line 105

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

#to_hashObject


94
95
96
97
# File 'lib/rack/session/abstract/id.rb', line 94

def to_hash
  load_for_read!
  @data.dup
end

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


99
100
101
102
# File 'lib/rack/session/abstract/id.rb', line 99

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

#valuesObject


143
144
145
146
# File 'lib/rack/session/abstract/id.rb', line 143

def values
  load_for_read!
  @data.values
end