Class: Termtter::TypableIdGenerator
- Inherits:
-
Object
- Object
- Termtter::TypableIdGenerator
- Defined in:
- lib/plugins/defaults/stdout.rb
Instance Method Summary collapse
- #get(id) ⇒ Object
- #get_id(data) ⇒ Object
-
#initialize(ids) ⇒ TypableIdGenerator
constructor
A new instance of TypableIdGenerator.
- #next(data) ⇒ Object
Constructor Details
#initialize(ids) ⇒ TypableIdGenerator
Returns a new instance of TypableIdGenerator.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/plugins/defaults/stdout.rb', line 41 def initialize(ids) if not ids.kind_of?(Array) raise ArgumentError, 'ids should be an Array' elsif ids.empty? raise ArgumentError, 'ids should not be empty' end @ids = ids @table = {} @rtable = {} end |
Instance Method Details
#get(id) ⇒ Object
61 62 63 |
# File 'lib/plugins/defaults/stdout.rb', line 61 def get(id) @table[id] end |
#get_id(data) ⇒ Object
65 66 67 |
# File 'lib/plugins/defaults/stdout.rb', line 65 def get_id(data) @rtable[data] || self.next(data) end |
#next(data) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/plugins/defaults/stdout.rb', line 52 def next(data) id = @ids.shift @ids.push id @rtable.delete(@table[id]) @table[id] = data @rtable[data] = id id end |