Class: WebConsole::Session
- Inherits:
-
Object
- Object
- WebConsole::Session
- Defined in:
- lib/web_console/session.rb
Overview
A session lets you persist an Evaluator
instance in memory associated with multiple bindings.
Each newly created session is persisted into memory and you can find it later by its id
.
A session may be associated with multiple bindings. This is used by the error pages only, as currently, this is the only client that needs to do that.
Constant Summary collapse
- @@inmemory_storage =
{}
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
An unique identifier for every REPL.
Class Method Summary collapse
-
.find(id) ⇒ Object
Finds a persisted session in memory by its id.
-
.from(storage) ⇒ Object
Create a Session from an binding or exception in a storage.
Instance Method Summary collapse
-
#eval(input) ⇒ Object
Evaluate
input
on the current Evaluator associated binding. -
#initialize(bindings) ⇒ Session
constructor
A new instance of Session.
-
#switch_binding_to(index) ⇒ Object
Switches the current binding to the one at specified
index
.
Constructor Details
#initialize(bindings) ⇒ Session
Returns a new instance of Session.
43 44 45 46 47 48 49 |
# File 'lib/web_console/session.rb', line 43 def initialize(bindings) @id = SecureRandom.hex(16) @bindings = Array(bindings) @evaluator = Evaluator.new(application_binding || @bindings.first) store_into_memory end |
Instance Attribute Details
#id ⇒ Object (readonly)
An unique identifier for every REPL.
41 42 43 |
# File 'lib/web_console/session.rb', line 41 def id @id end |
Class Method Details
.find(id) ⇒ Object
Finds a persisted session in memory by its id.
Returns a persisted session if found in memory. Raises NotFound error unless found in memory.
20 21 22 |
# File 'lib/web_console/session.rb', line 20 def find(id) inmemory_storage[id] end |
.from(storage) ⇒ Object
Create a Session from an binding or exception in a storage.
The storage is expected to respond to #[]. The binding is expected in :__web_console_binding and the exception in :__web_console_exception.
Can return nil, if no binding or exception have been preserved in the storage.
31 32 33 34 35 36 37 |
# File 'lib/web_console/session.rb', line 31 def from(storage) if exc = storage[:__web_console_exception] new(exc.bindings) elsif binding = storage[:__web_console_binding] new(binding) end end |
Instance Method Details
#eval(input) ⇒ Object
Evaluate input
on the current Evaluator associated binding.
Returns a string of the Evaluator output.
54 55 56 |
# File 'lib/web_console/session.rb', line 54 def eval(input) @evaluator.eval(input) end |
#switch_binding_to(index) ⇒ Object
Switches the current binding to the one at specified index
.
Returns nothing.
61 62 63 |
# File 'lib/web_console/session.rb', line 61 def switch_binding_to(index) @evaluator = Evaluator.new(@bindings[index.to_i]) end |