Class: CDB

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(file, tmp, mode = 0644) ⇒ Object



18
19
20
21
22
23
# File 'lib/cdb.rb', line 18

def CDB.create(file, tmp, mode = 0644)
  CDBMake.open(tmp, mode) do |cm|
    yield cm
  end
  File.rename(tmp, file)
end

.dump(file) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/cdb.rb', line 49

def CDB.dump(file)
  CDB.open(file) do |c|
    str = %q()
    c.each do |k, d|
      str << %(+#{k.length},#{d.length}:#{k}->#{d}\n)
    end
    return str
  end
end

.each(file, key = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cdb.rb', line 4

def CDB.each(file, key = nil)
  CDB.open(file) do |db|
    if key
      db.each(key) do |d|
        yield d
      end
    else
      db.each do |k, d|
        yield k, d
      end
    end
  end
end

.edit(file, tmp, mode = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cdb.rb', line 25

def CDB.edit(file, tmp, mode = nil)
  mode ||= File.stat(file).mode
  CDB.open(file) do |c|
    ary = c.to_ary
    yield ary
    CDBMake.open(tmp, mode) do |cm|
      ary.each do |k, d|
        cm.add(k, d)
      end
    end
  end
  File.rename(tmp, file)
end

.update(file, tmp, mode = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/cdb.rb', line 39

def CDB.update(file, tmp, mode = nil)
  mode ||= File.stat(file).mode
  CDB.open(file) do |c|
    CDBMake.open(tmp, mode) do |cm|
      yield(c, cm)
    end
  end
  File.rename(tmp, file)
end

Instance Method Details

#to_aryObject



67
68
69
70
71
72
73
# File 'lib/cdb.rb', line 67

def to_ary
  ary = CDBEditable.new
  each do |k, d|
    ary.push [k, d]
  end
  return ary
end

#to_hashObject



59
60
61
62
63
64
65
# File 'lib/cdb.rb', line 59

def to_hash
  hash = Hash.new
  each do |k, d|
    hash[k] = d
  end
  return hash
end