Class: Z4db::DB

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

Instance Method Summary collapse

Constructor Details

#initialize(*k) ⇒ DB

Returns a new instance of DB.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/z4db.rb', line 10

def initialize(*k);
  @constructor = [k].flatten
  @ev = {}
  @id = [@constructor].flatten.join('-');
  @type = @constructor.shift
  @index = @constructor.join('-')
  @db = PStore.new(%[db/#{@id}.pstore]);
  @cal = PStore.new(%[db/#{@id}.events.pstore])
  @sub = {}
  @current = %[]
  puts %[DB: #{@id}]
end

Instance Method Details

#[](k) ⇒ Object

getter



88
# File 'lib/z4db.rb', line 88

def [](k); @db.transaction { |db| db[k] }; end

#[]=(k, v) ⇒ Object

setter



90
# File 'lib/z4db.rb', line 90

def []=(k,v); @db.transaction { |db| db[k]=v }; end

#add(l, i, *u) ⇒ Object

push i to list



31
32
33
34
35
36
37
38
39
# File 'lib/z4db.rb', line 31

def add(l,i,*u);
  @db.transaction {|db|
    db[l] ||= [];
    [i].flatten.each { |e| db[l] << i; }
    if u[0] == :uniq;
      db[l].uniq!;
    end
  };
end

#agendaObject



72
73
74
75
76
77
78
79
80
# File 'lib/z4db.rb', line 72

def agenda
  o = []
  `remind -s reminders/#{@id}.rem`.chomp.gsub(' *', '').split("\n").each { |e|
    ee = e.split(" ");
    ee.slice!(1);
    o << ee.join(" ");
  }
  return o
end

#calendarObject



82
83
84
# File 'lib/z4db.rb', line 82

def calendar
  `remind -c reminders/#{@id}.rem`.chomp.gsub("\f","").split("\n")
end

#forget(k) ⇒ Object



65
66
67
68
69
70
# File 'lib/z4db.rb', line 65

def forget k
  kk = Z4.datetime(k).strftime('%Y/%m/%d AT %H:%m ');
  o = []
  @cal.transaction { |db| db.delete(kk); db.keys.each { |e| o << %[REM #{e} MSG #{db[e]}] } }
  File.open("reminders/#{@id}.rem", 'w') {|f| f.write(o.join("\n") + %[\n]); }
end

#idObject



23
# File 'lib/z4db.rb', line 23

def id; @id; end

#indexObject



25
# File 'lib/z4db.rb', line 25

def index; @index; end

#keysObject

defined keys



94
# File 'lib/z4db.rb', line 94

def keys; @db.transaction { |db| db.keys }; end

#log(*a) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/z4db.rb', line 56

def log *a
  kk = Time.now.strftime('%Y/%m/%d AT %H:%M');
  v = a.join(" ")
  o = []
  @cal.transaction { |db| db[kk] = %[:log: #{v}]; db.keys.each { |e| o << %[REM #{e} MSG #{db[e]}] } }
  File.open("reminders/#{@id}.rem", 'w') {|f| f.write(o.join("\n") + %[\n]); }
  return o.length
end

#on(k, h = {}, &b) ⇒ Object



86
# File 'lib/z4db.rb', line 86

def on(k, h={}, &b); if block_given?; @ev[k] = b; else; @db.transaction { |db| @ev[k.to_sym].call(db, h) }; end; end

#remind(k, *a) ⇒ Object

remind “date time string in some fashion.”, “event info”…



47
48
49
50
51
52
53
54
# File 'lib/z4db.rb', line 47

def remind k, *a
  kk = Z4.datetime(k).strftime('%Y/%m/%d AT %H:%M');
  v = a.join(" ")
  o = []
  @cal.transaction { |db| db[kk] = %[#{v}]; db.keys.each { |e| o << %[REM #{e} MSG #{db[e]}] } }
  File.open("reminders/#{@id}.rem", 'w') {|f| f.write(o.join("\n") + %[\n]); }
  return o.length
end

#sub(k) ⇒ Object



41
42
43
44
# File 'lib/z4db.rb', line 41

def sub(k)
  add(:subs, k, :uniq)
  Z4db[@id, k]
end

#tick(k, h = {num: 1}) ⇒ Object

incr / decr by :num



28
# File 'lib/z4db.rb', line 28

def tick(k,h={num: 1}); @db.transaction { |db| x = db[k.to_sym].to_i; db[k.to_sym] = x + h[:num] }; end

#to_hObject

bulk output



96
97
98
99
100
# File 'lib/z4db.rb', line 96

def to_h;
  h= {};
  @db.transaction { |db| db.keys.each { |e| h[e] = db[e]; } };
  return h;
end

#transaction(&b) ⇒ Object

transaction wrapper



92
# File 'lib/z4db.rb', line 92

def transaction(&b); @db.transaction { |db| b.call(db) }; end

#typeObject



24
# File 'lib/z4db.rb', line 24

def type; @type; end