Class: WebConsole::Session

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception_mappers) ⇒ Session

Returns a new instance of Session.



44
45
46
47
48
49
50
51
# File 'lib/web_console/session.rb', line 44

def initialize(exception_mappers)
  @id = SecureRandom.hex(16)

  @exception_mappers = exception_mappers
  @evaluator         = Evaluator.new(@current_binding = exception_mappers.first.first)

  store_into_memory
end

Instance Attribute Details

#idObject (readonly)

An unique identifier for every REPL.



42
43
44
# File 'lib/web_console/session.rb', line 42

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.



21
22
23
# File 'lib/web_console/session.rb', line 21

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.



32
33
34
35
36
37
38
# File 'lib/web_console/session.rb', line 32

def from(storage)
  if exc = storage[:__web_console_exception]
    new(ExceptionMapper.follow(exc))
  elsif binding = storage[:__web_console_binding]
    new([[binding]])
  end
end

Instance Method Details

#context(objpath) ⇒ Object

Returns context of the current binding



70
71
72
# File 'lib/web_console/session.rb', line 70

def context(objpath)
  Context.new(@current_binding).extract(objpath)
end

#eval(input) ⇒ Object

Evaluate input on the current Evaluator associated binding.

Returns a string of the Evaluator output.



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

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

#switch_binding_to(index, exception_object_id) ⇒ Object

Switches the current binding to the one at specified index.

Returns nothing.



63
64
65
66
67
# File 'lib/web_console/session.rb', line 63

def switch_binding_to(index, exception_object_id)
  bindings = ExceptionMapper.find_binding(@exception_mappers, exception_object_id)

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