Class: Tina4::Session
- Inherits:
-
Object
- Object
- Tina4::Session
- Defined in:
- lib/tina4/session.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ cookie_name: "tina4_session", secret: nil, max_age: 86400, handler: :file, handler_options: {} }.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#all ⇒ Object
Return all session data.
- #clear ⇒ Object
- #cookie_header ⇒ Object
- #delete(key) ⇒ Object
- #destroy ⇒ Object
-
#flash(key, value = nil) ⇒ Object
Flash data: set a value that is removed after next read.
-
#gc(max_age = nil) ⇒ Object
Garbage collection: remove expired sessions from the handler.
-
#get(key, default = nil) ⇒ Object
Get a session value with optional default.
-
#get_flash(key, default = nil) ⇒ Object
Get flash data by key (alias for flash(key) without value).
-
#has?(key) ⇒ Boolean
Check if a key exists in the session.
-
#initialize(env, options = {}) ⇒ Session
constructor
A new instance of Session.
-
#regenerate ⇒ Object
Regenerate the session ID while preserving data — returns new ID.
- #save ⇒ Object
-
#set(key, value) ⇒ Object
Set a session value.
- #to_hash ⇒ Object
Constructor Details
#initialize(env, options = {}) ⇒ Session
Returns a new instance of Session.
17 18 19 20 21 22 23 24 |
# File 'lib/tina4/session.rb', line 17 def initialize(env, = {}) @options = DEFAULT_OPTIONS.merge() @options[:secret] ||= ENV["SECRET"] || "tina4-default-secret" @handler = create_handler @id = extract_session_id(env) || SecureRandom.hex(32) @data = load_session @modified = false end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/tina4/session.rb', line 15 def data @data end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/tina4/session.rb', line 15 def id @id end |
Instance Method Details
#[](key) ⇒ Object
26 27 28 |
# File 'lib/tina4/session.rb', line 26 def [](key) @data[key.to_s] end |
#[]=(key, value) ⇒ Object
30 31 32 33 |
# File 'lib/tina4/session.rb', line 30 def []=(key, value) @data[key.to_s] = value @modified = true end |
#all ⇒ Object
Return all session data
77 78 79 |
# File 'lib/tina4/session.rb', line 77 def all @data.dup end |
#clear ⇒ Object
40 41 42 43 |
# File 'lib/tina4/session.rb', line 40 def clear @data = {} @modified = true end |
#cookie_header ⇒ Object
117 118 119 120 |
# File 'lib/tina4/session.rb', line 117 def samesite = ENV["TINA4_SESSION_SAMESITE"] || "Lax" "#{@options[:cookie_name]}=#{@id}; Path=/; HttpOnly; SameSite=#{samesite}; Max-Age=#{@options[:max_age]}" end |
#delete(key) ⇒ Object
35 36 37 38 |
# File 'lib/tina4/session.rb', line 35 def delete(key) @data.delete(key.to_s) @modified = true end |
#destroy ⇒ Object
55 56 57 58 |
# File 'lib/tina4/session.rb', line 55 def destroy @handler.destroy(@id) @data = {} end |
#flash(key, value = nil) ⇒ Object
Flash data: set a value that is removed after next read. Call with value to set, call without value to get (and remove).
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tina4/session.rb', line 83 def flash(key, value = nil) flash_key = "_flash_#{key}" if value.nil? val = @data.delete(flash_key.to_s) @modified = true if val val else @data[flash_key.to_s] = value @modified = true value end end |
#gc(max_age = nil) ⇒ Object
Garbage collection: remove expired sessions from the handler
112 113 114 115 |
# File 'lib/tina4/session.rb', line 112 def gc(max_age = nil) max_age ||= @options[:max_age] @handler.gc(max_age) if @handler.respond_to?(:gc) end |
#get(key, default = nil) ⇒ Object
Get a session value with optional default
61 62 63 |
# File 'lib/tina4/session.rb', line 61 def get(key, default = nil) @data[key.to_s] || default end |
#get_flash(key, default = nil) ⇒ Object
Get flash data by key (alias for flash(key) without value)
97 98 99 100 |
# File 'lib/tina4/session.rb', line 97 def get_flash(key, default = nil) result = flash(key) result.nil? ? default : result end |
#has?(key) ⇒ Boolean
Check if a key exists in the session
72 73 74 |
# File 'lib/tina4/session.rb', line 72 def has?(key) @data.key?(key.to_s) end |
#regenerate ⇒ Object
Regenerate the session ID while preserving data — returns new ID
103 104 105 106 107 108 109 |
# File 'lib/tina4/session.rb', line 103 def regenerate old_id = @id @id = SecureRandom.hex(32) @handler.destroy(old_id) @modified = true @id end |
#save ⇒ Object
49 50 51 52 53 |
# File 'lib/tina4/session.rb', line 49 def save return unless @modified @handler.write(@id, @data) @modified = false end |
#set(key, value) ⇒ Object
Set a session value
66 67 68 69 |
# File 'lib/tina4/session.rb', line 66 def set(key, value) @data[key.to_s] = value @modified = true end |
#to_hash ⇒ Object
45 46 47 |
# File 'lib/tina4/session.rb', line 45 def to_hash @data.dup end |