Class: WebConsole::REPLSession

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON
Defined in:
lib/web_console/repl_session.rb

Defined Under Namespace

Classes: NotFound

Constant Summary collapse

INMEMORY_STORAGE =
{}
ATTRIBUTES =
[ :id, :input, :output, :prompt, :binding, :binding_stack ].each do |attr|
  attr_accessor attr
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ REPLSession

Returns a new instance of REPLSession.



32
33
34
35
36
# File 'lib/web_console/repl_session.rb', line 32

def initialize(attributes = {})
  self.attributes = attributes
  generate_secure_id!
  populate_repl_attributes!(initial: true)
end

Class Method Details

.create(attributes = {}) ⇒ Object

Creates an already persisted console session.

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



27
28
29
# File 'lib/web_console/repl_session.rb', line 27

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

.find(id) ⇒ Object

Finds a session by its id.



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

def find(id)
  INMEMORY_STORAGE[id] or raise NotFound, 'Session unavailable'
end

Instance Method Details

#binding=(binding) ⇒ Object



38
39
40
41
# File 'lib/web_console/repl_session.rb', line 38

def binding=(binding)
  @binding = binding
  @repl = WebConsole::REPL.new(binding)
end

#persisted?Boolean

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/web_console/repl_session.rb', line 53

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).



46
47
48
49
50
# File 'lib/web_console/repl_session.rb', line 46

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