Class: TokenPhrase::Generator
- Inherits:
-
Object
- Object
- TokenPhrase::Generator
- Defined in:
- lib/token_phrase/generator.rb
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
Returns the value of attribute dictionary.
-
#order ⇒ Object
Returns the value of attribute order.
-
#separator ⇒ Object
Returns the value of attribute separator.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(separator = "-", options = {}) ⇒ Generator
constructor
A new instance of Generator.
- #lists ⇒ Object
- #permutations ⇒ Object
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 = "-", = {} separator, = "-", separator if separator.is_a? Hash [:numbers] = [] if [:numbers] == false @separator = separator @dictionary = TokenPhrase.dictionary.merge @order = dictionary.keys end |
Instance Attribute Details
#dictionary ⇒ Object
Returns the value of attribute dictionary.
4 5 6 |
# File 'lib/token_phrase/generator.rb', line 4 def dictionary @dictionary end |
#order ⇒ Object
Returns the value of attribute order.
4 5 6 |
# File 'lib/token_phrase/generator.rb', line 4 def order @order end |
#separator ⇒ Object
Returns the value of attribute separator.
4 5 6 |
# File 'lib/token_phrase/generator.rb', line 4 def separator @separator end |
Instance Method Details
#generate ⇒ Object
15 16 17 |
# File 'lib/token_phrase/generator.rb', line 15 def generate lists.map(&:sample).join(separator).chomp(separator).gsub(/-/, separator) end |
#lists ⇒ Object
29 30 31 |
# File 'lib/token_phrase/generator.rb', line 29 def lists dictionary.values_at(*order).compact end |
#permutations ⇒ Object
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 |