Class: Credstore::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/credstore/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Storage

Returns a new instance of Storage.



17
18
19
20
21
22
23
24
# File 'lib/credstore/storage.rb', line 17

def initialize(opts={})
  opts[:keys_dir] ||= "./"
  opts[:database] ||= "credstore.db"
  opts[:public_key] ||= "id_rsa.pub"
  opts[:private_key] ||= "id_rsa"
  @crypt = Credstore::Crypt.new({:keys_dir=>opts[:keys_dir], :public_key=>opts[:public_key], :private_key=>opts[:private_key]})
  @store = PStore.new(File.join(opts[:keys_dir], opts[:database]), true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



42
43
44
45
# File 'lib/credstore/storage.rb', line 42

def method_missing(id, *args)
  return self.write_key(id.id2name.gsub("=", ""), args.first) if id.id2name =~ /=.*/
  return self.read_key(id.id2name) if id.id2name =~ /.*/
end

Instance Attribute Details

#storeObject

Returns the value of attribute store.



16
17
18
# File 'lib/credstore/storage.rb', line 16

def store
  @store
end

Instance Method Details

#read_key(key) ⇒ Object



36
37
38
39
40
# File 'lib/credstore/storage.rb', line 36

def read_key key
  @store.transaction do
    @crypt.decrypt_string @store[key.to_sym]
  end
end

#write_key(key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/credstore/storage.rb', line 26

def write_key key, value
  @store.transaction do
    if value.nil?
      @store[key.to_sym] = nil
    else
      @store[key.to_sym] = @crypt.encrypt_string value
    end
  end
end