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.
32 33 34 35 |
# File 'lib/gepub/package.rb', line 32 def initialize @pool = {} @counter = {} end |
Instance Method Details
#[](k) ⇒ Object
66 67 68 |
# File 'lib/gepub/package.rb', line 66 def [](k) @pool[k] end |
#[]=(k, v) ⇒ Object
69 70 71 |
# File 'lib/gepub/package.rb', line 69 def []=(k,v) @pool[k] = v end |
#counter(prefix, suffix) ⇒ Object
37 38 39 |
# File 'lib/gepub/package.rb', line 37 def counter(prefix,suffix) @counter[prefix + '////' + suffix] end |
#generate_key(param = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gepub/package.rb', line 45 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
41 42 43 |
# File 'lib/gepub/package.rb', line 41 def set_counter(prefix,suffix,val) @counter[prefix + '////' + suffix] = val end |