Class: PromptScrub::Vault

Inherits:
Object
  • Object
show all
Defined in:
lib/promptscrub/vault.rb

Instance Method Summary collapse

Constructor Details

#initializeVault

Returns a new instance of Vault.



5
6
7
8
9
10
# File 'lib/promptscrub/vault.rb', line 5

def initialize
  @store    = {}
  @reverse  = {}
  @counters = Hash.new(0)
  @mutex    = Mutex.new
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/promptscrub/vault.rb', line 28

def empty?
  @mutex.synchronize { @store.empty? }
end

#rehydrate(token) ⇒ Object



24
25
26
# File 'lib/promptscrub/vault.rb', line 24

def rehydrate(token)
  @mutex.synchronize { @store[token] }
end

#sizeObject



32
33
34
# File 'lib/promptscrub/vault.rb', line 32

def size
  @mutex.synchronize { @store.size }
end

#tokenize(type, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/promptscrub/vault.rb', line 12

def tokenize(type, value)
  @mutex.synchronize do
    return @reverse[value] if @reverse.key?(value)

    @counters[type] += 1
    token = format('<%<type>s_%<num>03d>', type: type.upcase, num: @counters[type])
    @store[token]   = value
    @reverse[value] = token
    token
  end
end