Class: GEPUB::Package::IDPool
- Inherits:
-
Object
- Object
- GEPUB::Package::IDPool
- Defined in:
- lib/gepub/package.rb
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #counter(prefix, suffix) ⇒ Object
- #generate_key(param = {}) ⇒ Object
-
#initialize ⇒ IDPool
constructor
A new instance of IDPool.
- #set_counter(prefix, suffix, val) ⇒ Object
Constructor Details
#initialize ⇒ IDPool
Returns a new instance of IDPool.
11 12 13 14 |
# File 'lib/gepub/package.rb', line 11 def initialize @pool = {} @counter = {} end |
Instance Method Details
#[](k) ⇒ Object
45 46 47 |
# File 'lib/gepub/package.rb', line 45 def [](k) @pool[k] end |
#[]=(k, v) ⇒ Object
48 49 50 |
# File 'lib/gepub/package.rb', line 48 def []=(k,v) @pool[k] = v end |
#counter(prefix, suffix) ⇒ Object
16 17 18 |
# File 'lib/gepub/package.rb', line 16 def counter(prefix,suffix) @counter[prefix + '////' + suffix] end |
#generate_key(param = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gepub/package.rb', line 24 def generate_key(param = {}) while (true) prefix = param[:prefix] || '' suffix = param[:suffix] || '' count = [ param[:start] || 1, counter(prefix,suffix) || 1].max 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
20 21 22 |
# File 'lib/gepub/package.rb', line 20 def set_counter(prefix,suffix,val) @counter[prefix + '////' + suffix] = val end |