Class: Gonzui::IDCounter

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

Instance Method Summary collapse

Constructor Details

#initialize(dbm, id_name, counter, db, rev_db, alt_db) ⇒ IDCounter

Returns a new instance of IDCounter.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gonzui/dbm.rb', line 37

def initialize(dbm, id_name, counter, db, rev_db, alt_db)
  @dbm     = dbm
  @id_name = id_name
  @counter = counter
  @db      = dbm.send(db)
  @rev_db  = dbm.send(rev_db)
  @alt_db  = if alt_db then dbm.send(alt_db) else nil end

  @count = 0
  @cache = {}
  @last_id = (@dbm.seq[make_last_key] or -1)
end

Instance Method Details

#flushObject



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

def flush
  if @count > 0
    @dbm.increase_counter(@counter, @count)
    @dbm.seq[make_last_key] = @last_id if @last_id >= 0
    @count = 0
    @cache = {}
  end
end

#get_id(text) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gonzui/dbm.rb', line 69

def get_id(text)
  id = @cache[text]
  if id.nil?
    id = @db[text]
    if id.nil?
      id = make_new_id
      @db[text] = id
      @rev_db[id] = text
    end
    @cache[text] = id
  end
  return id
end

#get_id2(text, alt) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gonzui/dbm.rb', line 83

def get_id2(text, alt)
  id = @cache[text]
  if id.nil?
    id = @db[text]
    if id.nil?
      id = make_new_id
      @db[text] = id
      @rev_db[id] = text
      @alt_db[id] = alt
    end
    @cache[text] = id
  end
  return id
end

#make_last_keyObject



59
60
61
# File 'lib/gonzui/dbm.rb', line 59

def make_last_key
  "last_" + @id_name.to_s
end

#make_new_idObject



63
64
65
66
67
# File 'lib/gonzui/dbm.rb', line 63

def make_new_id
  @count += 1
  @last_id += 1
  return @last_id
end