Class: Booth::Requests::Session
- Inherits:
-
Object
- Object
- Booth::Requests::Session
- Includes:
- Logging
- Defined in:
- lib/booth/requests/session.rb
Overview
Convenience wrapper for ActionDispatch::Session::CookieStore.
Instance Attribute Summary collapse
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
-
#[](key) ⇒ Object
——————- Getters and Setters ——————-.
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(scope:, namespace:, session:) ⇒ Session
constructor
A new instance of Session.
-
#lifespan ⇒ Object
—– Timer —–.
- #reset ⇒ Object
- #seconds_until_auto_reset ⇒ Object
- #timed_out? ⇒ Boolean
Constructor Details
#initialize(scope:, namespace:, session:) ⇒ Session
Returns a new instance of Session.
11 12 13 14 15 |
# File 'lib/booth/requests/session.rb', line 11 def initialize(scope:, namespace:, session:) @scope = ::Booth::Syntaxes::Scope.call(scope).normalized_scope @namespace = namespace @session = session end |
Instance Attribute Details
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
9 10 11 |
# File 'lib/booth/requests/session.rb', line 9 def scope @scope end |
Instance Method Details
#[](key) ⇒ Object
Getters and Setters
21 22 23 24 25 26 27 |
# File 'lib/booth/requests/session.rb', line 21 def [](key) return if timed_out? raise ArgumentError if key.to_s == 'created_at' reset_if_too_old! session[path(key)] end |
#[]=(key, value) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/booth/requests/session.rb', line 29 def []=(key, value) return if timed_out? raise ArgumentError if key.to_s == 'created_at' ensure_ticking reset_if_too_old! session[path(key)] = value end |
#delete(key) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/booth/requests/session.rb', line 38 def delete(key) raise ArgumentError if key.to_s == 'created_at' reset_if_too_old! session.delete path(key) end |
#lifespan ⇒ Object
Timer
57 58 59 |
# File 'lib/booth/requests/session.rb', line 57 def lifespan ::Booth.config.interaction_timeout.to_i end |
#reset ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/booth/requests/session.rb', line 45 def reset log { "Resetting #{namespace.inspect} cookie data in scope #{scope.inspect}" } # There is no `#delete_if` in a `ActionDispatch::Request::Session`. keys_to_delete = session.keys.select { it.to_s.start_with?(path(nil)) } keys_to_delete.each { session.delete(it) } end |
#seconds_until_auto_reset ⇒ Object
61 62 63 64 65 |
# File 'lib/booth/requests/session.rb', line 61 def seconds_until_auto_reset return 0 if @reset_due_to_timeout lifespan - age end |
#timed_out? ⇒ Boolean
67 68 69 70 71 |
# File 'lib/booth/requests/session.rb', line 67 def timed_out? reset_if_too_old! !!@reset_due_to_timeout end |