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.
42 43 44 45 |
# File 'lib/gepub/package.rb', line 42 def initialize @pool = {} @counter = {} end |
Instance Method Details
#[](k) ⇒ Object
76 77 78 |
# File 'lib/gepub/package.rb', line 76 def [](k) @pool[k] end |
#[]=(k, v) ⇒ Object
79 80 81 |
# File 'lib/gepub/package.rb', line 79 def []=(k,v) @pool[k] = v end |
#counter(prefix, suffix) ⇒ Object
47 48 49 |
# File 'lib/gepub/package.rb', line 47 def counter(prefix,suffix) @counter[prefix + '////' + suffix] end |
#generate_key(param = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gepub/package.rb', line 55 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
51 52 53 |
# File 'lib/gepub/package.rb', line 51 def set_counter(prefix,suffix,val) @counter[prefix + '////' + suffix] = val end |