Sha256: 68ab5736581723cad5f7b7a60ce50a88319a6cf2f7a92fb9d0923900adf489ca

Contents?: true

Size: 875 Bytes

Versions: 5

Compression:

Stored size: 875 Bytes

Contents

module Tr3llo
  class Registry
    attr_reader :counter, :store

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

    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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
3llo-1.3.1 lib/3llo/registry.rb
3llo-1.3.1.pre.rc.0 lib/3llo/registry.rb
3llo-1.2.0 lib/3llo/registry.rb
3llo-1.1.0 lib/3llo/registry.rb
3llo-1.0.0 lib/3llo/registry.rb