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, env) ⇒ SessionHash

Returns a new instance of SessionHash.



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

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

Instance Attribute Details

#idObject



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

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

Class Method Details

.find(env) ⇒ Object



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

def self.find(env)
  env[ENV_SESSION_KEY]
end

.set(env, session) ⇒ Object



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

def self.set(env, session)
  env[ENV_SESSION_KEY] = session
end

.set_options(env, options) ⇒ Object



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

def self.set_options(env, options)
  env[ENV_SESSION_OPTIONS_KEY] = options.dup
end

Instance Method Details

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



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

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

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



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

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

#clearObject



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

def clear
  load_for_write!
  @data.clear
end

#delete(key) ⇒ Object



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

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

#destroyObject



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

def destroy
  clear
  @id = @store.send(:destroy_session, @env, id, options)
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  load_for_read!
  @data.empty?
end

#exists?Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#keysObject



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

def keys
  @data.keys
end

#loaded?Boolean

Returns:

  • (Boolean)


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

def loaded?
  @loaded
end

#optionsObject



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

def options
  @env[ENV_SESSION_OPTIONS_KEY]
end

#replace(hash) ⇒ Object



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

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

#to_hashObject



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

def to_hash
  load_for_read!
  @data.dup
end

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



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

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

#valuesObject



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

def values
  @data.values
end