Class: Termtter::TypableIdGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/defaults/stdout.rb

Instance Method Summary collapse

Constructor Details

#initialize(ids) ⇒ TypableIdGenerator

Returns a new instance of TypableIdGenerator.



43
44
45
46
47
48
49
50
51
52
# File 'lib/plugins/defaults/stdout.rb', line 43

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



63
64
65
# File 'lib/plugins/defaults/stdout.rb', line 63

def get(id)
  @table[id]
end

#get_id(data) ⇒ Object



67
68
69
# File 'lib/plugins/defaults/stdout.rb', line 67

def get_id(data)
  @rtable[data] || self.next(data)
end

#next(data) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/plugins/defaults/stdout.rb', line 54

def next(data)
  id = @ids.shift
  @ids.push id
  @rtable.delete(@table[id])
  @table[id] = data
  @rtable[data] = id
  id
end