class WebConsole::REPLSession

Constants

ATTRIBUTES
INMEMORY_STORAGE

Public Class Methods

create(attributes = {}) click to toggle source

Creates an already persisted console session.

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

# File lib/web_console/repl_session.rb, line 27
def create(attributes = {})
  INMEMORY_STORAGE[(model = new(attributes)).id] = model
end
find(id) click to toggle source

Finds a session by its id.

# File lib/web_console/repl_session.rb, line 19
def find(id)
  INMEMORY_STORAGE[id] or raise NotFound, 'Session unavailable'
end
new(attributes = {}) click to toggle source
# File lib/web_console/repl_session.rb, line 32
def initialize(attributes = {})
  self.attributes = attributes
  generate_secure_id!
  populate_repl_attributes!(initial: true)
end

Public Instance Methods

binding=(binding) click to toggle source
# File lib/web_console/repl_session.rb, line 38
def binding=(binding)
  @binding = binding
  @repl = WebConsole::REPL.new(binding)
end
persisted?() click to toggle source

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

# File lib/web_console/repl_session.rb, line 53
def persisted?
  self == INMEMORY_STORAGE[id]
end
save(attributes = {}) click to toggle source

Saves the model into the in-memory storage.

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

# File lib/web_console/repl_session.rb, line 46
def save(attributes = {})
  self.attributes = attributes if attributes.present?
  populate_repl_attributes!
  store!
end

Protected Instance Methods

attributes() click to toggle source

Returns a hash of the attributes and their values.

# File lib/web_console/repl_session.rb, line 60
def attributes
  return Hash[ATTRIBUTES.zip([nil])].except(:binding, :binding_stack)
end
attributes=(attributes) click to toggle source

Sets model attributes from a hash.

# File lib/web_console/repl_session.rb, line 65
def attributes=(attributes)
  attributes.each do |attr, value|
    next unless ATTRIBUTES.include?(attr.to_sym)
    public_send(:"#{attr}=", value)
  end
end

Private Instance Methods

generate_secure_id!() click to toggle source
# File lib/web_console/repl_session.rb, line 74
def generate_secure_id!
  self.id = SecureRandom.hex(16)
end
populate_repl_attributes!(options = {}) click to toggle source
# File lib/web_console/repl_session.rb, line 78
def populate_repl_attributes!(options = {})
  # Don't send any input on the initial population so we don't bump up
  # the numbers in the dynamic prompts.
  self.output = @repl.send_input(input) unless options[:initial]
  self.prompt = @repl.prompt
end
store!() click to toggle source
# File lib/web_console/repl_session.rb, line 85
def store!
  INMEMORY_STORAGE[id] = self
end