Class: WebConsole::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/web_console/session.rb

Overview

A session lets you persist wrap an Evaluator instance in memory associated with multiple bindings.

Each newly created session is persisted into memory and you can find it later 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bindings) ⇒ Session

Returns a new instance of Session.



37
38
39
40
41
42
43
# File 'lib/web_console/session.rb', line 37

def initialize(bindings)
  @id = SecureRandom.hex(16)
  @bindings = Array(bindings)
  @evaluator = Evaluator.new(@bindings[0])

  store_into_memory
end

Instance Attribute Details

#idObject (readonly)

An unique identifier for every REPL.



35
36
37
# File 'lib/web_console/session.rb', line 35

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.



19
20
21
# File 'lib/web_console/session.rb', line 19

def find(id)
  INMEMORY_STORAGE[id]
end

.from_binding(binding) ⇒ Object

Create a Session from a single binding.



29
30
31
# File 'lib/web_console/session.rb', line 29

def from_binding(binding)
  new(binding)
end

.from_exception(exc) ⇒ Object

Create a Session from an exception.



24
25
26
# File 'lib/web_console/session.rb', line 24

def from_exception(exc)
  new(exc.bindings)
end

Instance Method Details

#eval(input) ⇒ Object

Evaluate input on the current Evaluator associated binding.

Returns a string of the Evaluator output.



48
49
50
# File 'lib/web_console/session.rb', line 48

def eval(input)
  @evaluator.eval(input)
end

#switch_binding_to(index) ⇒ Object

Switches the current binding to the one at specified index.

Returns nothing.



55
56
57
# File 'lib/web_console/session.rb', line 55

def switch_binding_to(index)
  @evaluator = Evaluator.new(@bindings[index.to_i])
end