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.
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 |