Class: PerfectWorld::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/perfect_world/db.rb

Overview

Handles the password database.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(passwords = {}) ⇒ DB

Inits the db and sets the initial passwords.



39
40
41
42
# File 'lib/perfect_world/db.rb', line 39

def initialize(passwords = {})
  @passwords = passwords
  @changed = false
end

Class Method Details

.load(path) ⇒ Object

Loads the database.



22
23
24
25
26
27
28
# File 'lib/perfect_world/db.rb', line 22

def self.load(path)
  passwords = Storage.load(path)

  valid?(passwords) ? new(passwords) : raise(CorruptedDatabase, path)
rescue Error => e
  raise Error, "Couldn't load database: #{e.message}"
end

.open(path, owner = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/perfect_world/db.rb', line 10

def self.open(path, owner = nil)
  db = File.exist?(path) ? load(path) : new({})

  if block_given?
    yield(db)
    db.save(path, owner) if db.changed?
  else
    db
  end
end

.valid?(data) ⇒ Boolean

Checks if a password hash is valid. A valid object is a hash with string values.

Returns true if it is valid else false.

Returns:

  • (Boolean)


34
35
36
# File 'lib/perfect_world/db.rb', line 34

def self.valid?(data)
  data.is_a?(Hash) && data.values.all? { |v| v.is_a?(String) }
end

Instance Method Details

#[](id) ⇒ Object

Returns the password for the id or nil, if not found.



70
71
72
# File 'lib/perfect_world/db.rb', line 70

def [](id)
  @passwords[id]
end

#changed?Boolean

Returns true, when the database has changed, else false.

Returns:

  • (Boolean)


96
97
98
# File 'lib/perfect_world/db.rb', line 96

def changed?
  @changed
end

#delete(id) ⇒ Object

Deletes the password from the database.

store.delete(:google)
#=> "B6m/![)A%fqw,\\ti-d`4\"&0>gl+>$0$Z"

store[:google]
#=> nil

Returns nil if not found.



88
89
90
91
92
93
# File 'lib/perfect_world/db.rb', line 88

def delete(id)
  if (password = @passwords.delete(id))
    @changed = true
    password
  end
end

#each(&block) ⇒ Object

Iterates over all passwords.

store.each do |id, password|
  puts "#{password}   #{id}"
end

Returns an enumerator, if no block is given.



107
108
109
# File 'lib/perfect_world/db.rb', line 107

def each(&block)
  @passwords.each(&block)
end

#fetch(id, *default) ⇒ Object

Fetches a password from the database. It behaves like Hash#fetch.



75
76
77
# File 'lib/perfect_world/db.rb', line 75

def fetch(id, *default)
  @passwords.fetch(id, *default)
end

#generate(id, len = 64) ⇒ Object

Generates a new password and puts it in the database.

store.generate(:google, 32)
#=> "B6m/![)A%fqw,\\ti-d`4\"&0>gl+>$0$Z"

store[:google]
#=> "B6m/![)A%fqw,\\ti-d`4\"&0>gl+>$0$Z"

Returns the new password or raises an error if the password is already in the database.



54
55
56
57
58
59
60
# File 'lib/perfect_world/db.rb', line 54

def generate(id, len = 64)
  if ! @passwords.key?(id)
    generate!(id, len)
  else
    raise Error, "Your #{id} password is already in the database."
  end
end

#generate!(id, len = 64) ⇒ Object

Does the same as DB#generate, but overrides existing passwords.



63
64
65
66
67
# File 'lib/perfect_world/db.rb', line 63

def generate!(id, len = 64)
  @passwords[id] = Random.string(len)
  @changed = true
  @passwords[id]
end

#save(path, owner = nil) ⇒ Object

Encryptes the database and writes it to disk.



112
113
114
115
116
# File 'lib/perfect_world/db.rb', line 112

def save(path, owner = nil)
  Storage.dump(@passwords, owner, path)
rescue Error => e
  raise Error, "Couldn't save database: #{e.message}"
end