Class: WebConsole::ConsoleSession

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON, Mutex_m
Defined in:
app/models/web_console/console_session.rb

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

INMEMORY_STORAGE =

In-memory storage for the console sessions. Session preservation is troubled on servers with multiple workers and threads.

{}
ATTRIBUTES =

Store and define the available attributes.

[ :id, :input, :output, :prompt ].each do |attr|
  attr_accessor attr
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ConsoleSession

Returns a new instance of ConsoleSession.



42
43
44
45
46
47
48
# File 'app/models/web_console/console_session.rb', line 42

def initialize(attributes = {})
  @repl = WebConsole::REPL.default.new

  super
  ensure_consequential_id!
  populate_repl_attributes!(initial: true)
end

Class Method Details

.createObject

Creates an already persisted consolse session.

Use this method if you need to persist a session, without providing it any input.



37
38
39
# File 'app/models/web_console/console_session.rb', line 37

def create
  INMEMORY_STORAGE[(model = new).id] = model
end

.find(id) ⇒ Object

Finds a session by its id.

Raises WebConsole::ConsoleSession::Expired if there is no such session.



29
30
31
# File 'app/models/web_console/console_session.rb', line 29

def find(id)
  INMEMORY_STORAGE[id.to_i] or raise NotFound.new('Session unavailable')
end

Instance Method Details

#persisted?Boolean

Returns true if the current session is persisted in the in-memory storage.

Returns:

  • (Boolean)


60
61
62
# File 'app/models/web_console/console_session.rb', line 60

def persisted?
  self == INMEMORY_STORAGE[id]
end

#save(attributes = {}) ⇒ Object

Saves the model into the in-memory storage.

Returns false if the model is not valid (e.g. its missing input).



53
54
55
56
57
# File 'app/models/web_console/console_session.rb', line 53

def save(attributes = {})
  self.attributes = attributes if attributes.present?
  populate_repl_attributes!
  store!
end

#to_keyObject

Returns an Enumerable of all key attributes if any is set, regardless if the object is persisted or not.



66
67
68
# File 'app/models/web_console/console_session.rb', line 66

def to_key
  super if persisted?
end