Class: TFA::Storage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tfa/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Storage

Returns a new instance of Storage.



6
7
8
9
10
11
12
13
14
# File 'lib/tfa/storage.rb', line 6

def initialize(path)
  @path = path
  @storage =
    if ".pstore" == File.extname(path)
      PStore.new(path)
    else
      YAML::Store.new(path)
    end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/tfa/storage.rb', line 4

def path
  @path
end

Instance Method Details

#allObject



22
23
24
25
26
# File 'lib/tfa/storage.rb', line 22

def all
  open_readonly do |storage|
    storage.roots.map { |key| { key => storage[key] } }
  end
end

#delete(key) ⇒ Object



40
41
42
43
44
# File 'lib/tfa/storage.rb', line 40

def delete(key)
  @storage.transaction do
    @storage.delete(key)
  end
end

#eachObject



16
17
18
19
20
# File 'lib/tfa/storage.rb', line 16

def each
  all.each do |each|
    yield each
  end
end

#save(key, value) ⇒ Object



34
35
36
37
38
# File 'lib/tfa/storage.rb', line 34

def save(key, value)
  @storage.transaction do
    @storage[key] = value
  end
end

#secret_for(key) ⇒ Object



28
29
30
31
32
# File 'lib/tfa/storage.rb', line 28

def secret_for(key)
  open_readonly do |storage|
    storage[key]
  end
end