Class: GEPUB::Package::IDPool

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

Instance Method Summary collapse

Constructor Details

#initializeIDPool

Returns a new instance of IDPool.


40
41
42
43
# File 'lib/gepub/package.rb', line 40

def initialize
  @pool = {}
  @counter = {}
end

Instance Method Details

#[](k) ⇒ Object


74
75
76
# File 'lib/gepub/package.rb', line 74

def [](k)
  @pool[k]
end

#[]=(k, v) ⇒ Object


77
78
79
# File 'lib/gepub/package.rb', line 77

def []=(k,v)
  @pool[k] = v
end

#counter(prefix, suffix) ⇒ Object


45
46
47
# File 'lib/gepub/package.rb', line 45

def counter(prefix,suffix)
  @counter[prefix + '////' + suffix]
end

#generate_key(param = {}) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gepub/package.rb', line 53

def generate_key(param = {})
  prefix = param[:prefix] || ''
  suffix = param[:suffix] || ''
  count = [ param[:start] || 1, counter(prefix,suffix) || 1].max
  while (true)
    if param[:without_count]
      k = prefix + suffix
      count -= 1
      param.delete(:without_count)
    else
      k = prefix + count.to_s + suffix
    end
    if @pool[k].nil?
      set_counter(prefix,suffix, count + 1)
      return k
    end
    count += 1
  end

end

#set_counter(prefix, suffix, val) ⇒ Object


49
50
51
# File 'lib/gepub/package.rb', line 49

def set_counter(prefix,suffix,val)
  @counter[prefix + '////' + suffix] = val
end