class Concode::Generator

Attributes

capitalize[R]
chars[R]
glue[R]
words[R]

Public Class Methods

new(words: 2, chars: 0, glue: '-', capitalize: false) click to toggle source
# File lib/concode/generator.rb, line 7
def initialize(words: 2, chars: 0, glue: '-', capitalize: false)
  @words = words
  @glue = glue
  @capitalize = capitalize
  @chars = chars

  @chars = 3 if @chars.between? 1, 3
  @chars = 0 if @chars > 9
end

Public Instance Methods

generate(text) click to toggle source
# File lib/concode/generator.rb, line 17
def generate(text)
  result = generate_particles text
  result.map!(&:capitalize) if capitalize
  result.join glue
end
word_count() click to toggle source
# File lib/concode/generator.rb, line 23
def word_count
  @word_count ||= particles.map(&:size).reduce(:*)
end

Private Instance Methods

adjective_count() click to toggle source
# File lib/concode/generator.rb, line 62
def adjective_count
  words - 1
end
adjectives() click to toggle source
# File lib/concode/generator.rb, line 78
def adjectives
  Dictionary.adjectives
end
adjectives_length() click to toggle source
# File lib/concode/generator.rb, line 70
def adjectives_length
  Dictionary.adjective_lengths[chars]
end
generate_particles(text) click to toggle source
# File lib/concode/generator.rb, line 45
def generate_particles(text)
  index = text_hash(text) % word_count

  result = []
  particles.each do |p|
    result.push p[index % p.size]
    index = (index / p.size).to_i
  end

  result.reverse
end
nouns() click to toggle source
# File lib/concode/generator.rb, line 74
def nouns
  Dictionary.nouns
end
nouns_length() click to toggle source
# File lib/concode/generator.rb, line 66
def nouns_length
  Dictionary.noun_lengths[chars]
end
particles() click to toggle source
# File lib/concode/generator.rb, line 29
def particles
  @particles ||= particles!
end
particles!() click to toggle source
# File lib/concode/generator.rb, line 33
def particles!
  if chars == 0
    result = [ nouns ]
    adjective_count.times { result.push adjectives }
  else
    result = [ nouns[0...nouns_length] ]
    adjective_count.times { result.push adjectives[0...adjectives_length] }
  end

  result
end
text_hash(text) click to toggle source
# File lib/concode/generator.rb, line 57
def text_hash(text)
  text = text.to_s
  Digest::MD5.hexdigest(text).to_i(16) * 36413321723440003717
end