Module: Twilic::Core::Session::InternTableHelpers

Defined in:
lib/twilic/core/session.rb

Class Method Summary collapse

Class Method Details

.get_id(table, value) ⇒ Object



325
326
327
328
# File 'lib/twilic/core/session.rb', line 325

def get_id(table, value)
  id = table.by_value[value]
  id ? [id, true] : [0, false]
end

.get_value(table, id) ⇒ Object



330
331
332
333
334
# File 'lib/twilic/core/session.rb', line 330

def get_value(table, id)
  return ["", false] if id >= table.by_id.length

  [table.by_id[id], true]
end

.register(table, value) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/twilic/core/session.rb', line 336

def register(table, value)
  id, ok = get_id(table, value)
  return [table, id] if ok

  id = table.by_id.length
  new_table = InternTable.new(
    by_value: table.by_value.merge(value => id),
    by_id: table.by_id + [value]
  )
  [new_table, id]
end

.register_mut(table, value) ⇒ Object



348
349
350
351
352
353
354
355
356
# File 'lib/twilic/core/session.rb', line 348

def register_mut(table, value)
  id = table.by_value[value]
  return id if id

  id = table.by_id.length
  table.by_value[value] = id
  table.by_id << value
  id
end