Class: Wildling::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/wildling/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Token

Returns a new instance of Token.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/wildling/token.rb', line 5

def initialize(options)
  options = symbolize_keys(options)
  @src = options.fetch(:src, "")
  @start_length = default_integer(options[:startLength], 1)
  @end_length = default_integer(options[:endLength], 1)
  @variants = options[:variants] || []
  @count = 0
  (@start_length..@end_length).each do |length|
    @count += @variants.length**length
  end
end

Instance Method Details

#countObject



17
18
19
# File 'lib/wildling/token.rb', line 17

def count
  @count
end

#get(index) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wildling/token.rb', line 25

def get(index)
  return "" if index > @count - 1 || index < 0
  return "" if index.zero? && @start_length.zero?

  index_with_offset = index
  string_length = @start_length
  (@start_length..@end_length).each do |length|
    string_length = length
    offset_count = @variants.length**length
    break if index_with_offset < offset_count

    index_with_offset -= offset_count
  end

  string_array = []
  string_length.times do
    variant_index = index_with_offset % @variants.length
    index_with_offset /= @variants.length
    string_array << @variants[variant_index]
  end
  string_array.join
end

#srcObject



21
22
23
# File 'lib/wildling/token.rb', line 21

def src
  @src
end