Class: TokenPhrase::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/token_phrase/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separator = "-", options = {}) ⇒ Generator

Returns a new instance of Generator.



6
7
8
9
10
11
12
13
# File 'lib/token_phrase/generator.rb', line 6

def initialize separator = "-", options = {}
  separator, options = "-", separator if separator.is_a? Hash
  options[:numbers] = [] if options[:numbers] == false

  @separator = separator
  @dictionary = TokenPhrase.dictionary.merge options
  @order = dictionary.keys
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



4
5
6
# File 'lib/token_phrase/generator.rb', line 4

def dictionary
  @dictionary
end

#orderObject

Returns the value of attribute order.



4
5
6
# File 'lib/token_phrase/generator.rb', line 4

def order
  @order
end

#separatorObject

Returns the value of attribute separator.



4
5
6
# File 'lib/token_phrase/generator.rb', line 4

def separator
  @separator
end

Instance Method Details

#generateObject



15
16
17
# File 'lib/token_phrase/generator.rb', line 15

def generate
  lists.map(&:sample).join(separator).chomp(separator).gsub(/-/, separator)
end

#listsObject



29
30
31
# File 'lib/token_phrase/generator.rb', line 29

def lists
  dictionary.values_at(*order).compact
end

#permutationsObject



19
20
21
22
23
24
25
26
27
# File 'lib/token_phrase/generator.rb', line 19

def permutations
  lists.inject 1 do |p, list|
    if list.empty?
      p
    else
      p * list.uniq.count
    end
  end
end