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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, req) ⇒ SessionHash

Returns a new instance of SessionHash.



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

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

Instance Attribute Details

#idObject



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

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



21
22
23
# File 'lib/rack/session/abstract/id.rb', line 21

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

.set(req, session) ⇒ Object



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

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

.set_options(req, options) ⇒ Object



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

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

Instance Method Details

#[](key) ⇒ Object Also known as: fetch



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

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

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



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

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

#clearObject



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

def clear
  load_for_write!
  @data.clear
end

#delete(key) ⇒ Object



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

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

#destroyObject



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

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

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  load_for_read!
  @data.empty?
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/rack/session/abstract/id.rb', line 59

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

#inspectObject



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

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

#keysObject



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

def keys
  load_for_read!
  @data.keys
end

#loaded?Boolean

Returns:

  • (Boolean)


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

def loaded?
  @loaded
end

#optionsObject



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

def options
  @req.session_options
end

#replace(hash) ⇒ Object



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

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

#to_hashObject



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

def to_hash
  load_for_read!
  @data.dup
end

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



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

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

#valuesObject



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

def values
  load_for_read!
  @data.values
end