Class: Tr3llo::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/3llo/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



5
6
7
8
# File 'lib/3llo/registry.rb', line 5

def initialize
  @store = {}
  @semaphore = Mutex.new()
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



3
4
5
# File 'lib/3llo/registry.rb', line 3

def counter
  @counter
end

#storeObject (readonly)

Returns the value of attribute store.



3
4
5
# File 'lib/3llo/registry.rb', line 3

def store
  @store
end

Instance Method Details

#register(type, id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/3llo/registry.rb', line 10

def register(type, id)
  @semaphore.synchronize do
    data =
      @store.fetch(type, {
        counter: 0,
        id_to_shortcut: {},
        shortcut_to_id: {}
      })

    id_to_shortcut = data[:id_to_shortcut]

    if id_to_shortcut.has_key?(id)
      id_to_shortcut.fetch(id)
    else
      counter = data[:counter] + 1
      shortcut = counter.to_s

      data[:counter] = counter
      data[:id_to_shortcut][id] = shortcut
      data[:shortcut_to_id][shortcut] = id

      @store[type] = data

      shortcut
    end
  end
end

#reverse_lookup(type, shortcut) ⇒ Object



38
39
40
# File 'lib/3llo/registry.rb', line 38

def reverse_lookup(type, shortcut)
  @store.dig(type, :shortcut_to_id, shortcut)
end