Skip to content

Commit

Permalink
refactor generation to a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
benolee committed Jan 21, 2013
1 parent 17c4986 commit 7e5a087
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 43 deletions.
51 changes: 8 additions & 43 deletions lib/token_phrase.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,14 @@
require "token_phrase/dictionary"
require "token_phrase/generator"
require "token_phrase/version"

module TokenPhrase
Adjectives = %w(splendid superior spectacular amazing ultimate ferocious exciting lovely old-fashioned home-made grass-fed free-range grandmas grandpas governing prickly strong stellar awesome wonderful bodacious excellent stupendous groovy dancing energetic sweet sour sugarfilled glazed vegan letterman thunderous established magnetic better windy wind-up american soft genetically-modified tailored liberal conservative bluetooth)
Colors = %w(red yellow blue green violet taupe mauve lime golden silver grey black white tangello sunshine brown tan infrared ultraviolet pink beige almond aquamarine burnt-orange cerulean cornflower-blue denim forest-green midnight-blue peach plum sea-green ruby emerald jade rose topaz onyx pearl coral crimson cyan chocolate aqua azure lavendar chiffon khaki ivory magenta navy-blue olive salmon turquoise)
Patterns = %w(striped checked spotted polka-dotted plaid wavy houndstooth argyle glossy matte pinstriped tartan paisley satin honeycomb fractal waved cracked )
Nouns = %w(floutist carpenter jacket president address machine computer mug lamp phone wall bicycle river lake fountain building book hat pants shirt cape soup gloves pen suit photograph sand profit energy fork compact-disk floppy-disk chandelier door window laboratory people tapir wolverine wolf spider wolf-spider spider-wolf banana-slug giraffe deer-mouse capybara dingo dragon cardinal owl octopus elk moose weasel elephant rhino iguana bullfrog greyhound stickbug ladybug ant rat coyote chimpanzee housecat barracuda raven crane fox panda racoon nessie whale dolphin shark viper frog toad flounder skunk wookie dishwasher bat space-heater bobble-head lego-set pinboard flag tv video-game envelope headphones mousepad jukebox)
Numbers = [*1..100]

def self.adjectives(more_adjectives = nil)
more_adjectives.nil? ? TokenPhrase::Adjectives : TokenPhrase::Adjectives | more_adjectives
def self.generate *args
Generator.new(*args).generate
end

def self.colors(more_colors = nil)
more_colors.nil? ? TokenPhrase::Colors : TokenPhrase::Colors | more_colors
end

def self.patterns(more_patterns = nil)
more_patterns.nil? ? TokenPhrase::Patterns : TokenPhrase::Patterns | more_patterns
end

def self.nouns(more_nouns = nil)
more_nouns.nil? ? TokenPhrase::Nouns : TokenPhrase::Nouns | more_nouns
end

def self.generate(separator = nil, dictionaries = {})
if separator.is_a?(Hash)
dictionaries = separator
separator = nil
end
dictionaries[:adjectives] ||= TokenPhrase::Adjectives
dictionaries[:colors] ||= TokenPhrase::Colors
dictionaries[:patterns] ||= TokenPhrase::Patterns
dictionaries[:nouns] ||= TokenPhrase::Nouns
phrase = [dictionaries[:adjectives].sample, dictionaries[:colors].sample, dictionaries[:patterns].sample, dictionaries[:nouns].sample].join('-')
phrase << Random.new.rand(1..1000000).to_s unless dictionaries[:numbers] == false
phrase.gsub!(/-/, separator) unless separator.nil?
return phrase
end

def self.permutations(dictionaries = {})
dictionaries[:adjectives] ||= TokenPhrase::Adjectives
dictionaries[:colors] ||= TokenPhrase::Colors
dictionaries[:patterns] ||= TokenPhrase::Patterns
dictionaries[:nouns] ||= TokenPhrase::Nouns
permutations = [:adjectives, :colors, :patterns, :nouns].map{ |key| dictionaries[key].uniq.count }.inject{ |product, total| product * total }
dictionaries[:numbers] == false ? permutations : permutations * 1000000

def self.permutations *args
Generator.new(*args).permutations
end
end

32 changes: 32 additions & 0 deletions lib/token_phrase/dictionary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module TokenPhrase
def self.dictionary
{
adjectives: %w(splendid superior spectacular amazing ultimate ferocious exciting lovely old-fashioned home-made grass-fed free-range grandmas grandpas governing prickly strong stellar awesome wonderful bodacious excellent stupendous groovy dancing energetic sweet sour sugarfilled glazed vegan letterman thunderous established magnetic better windy wind-up american soft genetically-modified tailored liberal conservative bluetooth),
colors: %w(red yellow blue green violet taupe mauve lime golden silver grey black white tangello sunshine brown tan infrared ultraviolet pink beige almond aquamarine burnt-orange cerulean cornflower-blue denim forest-green midnight-blue peach plum sea-green ruby emerald jade rose topaz onyx pearl coral crimson cyan chocolate aqua azure lavendar chiffon khaki ivory magenta navy-blue olive salmon turquoise),
patterns: %w(striped checked spotted polka-dotted plaid wavy houndstooth argyle glossy matte pinstriped tartan paisley satin honeycomb fractal waved cracked ),
nouns: %w(floutist carpenter jacket president address machine computer mug lamp phone wall bicycle river lake fountain building book hat pants shirt cape soup gloves pen suit photograph sand profit energy fork compact-disk floppy-disk chandelier door window laboratory people tapir wolverine wolf spider wolf-spider spider-wolf banana-slug giraffe deer-mouse capybara dingo dragon cardinal owl octopus elk moose weasel elephant rhino iguana bullfrog greyhound stickbug ladybug ant rat coyote chimpanzee housecat barracuda raven crane fox panda racoon nessie whale dolphin shark viper frog toad flounder skunk wookie dishwasher bat space-heater bobble-head lego-set pinboard flag tv video-game envelope headphones mousepad jukebox),
numbers: (1..100).to_a
}
end

def self.adjectives more = []
dictionary[:adjectives] | more
end

def self.colors more = []
dictionary[:colors] | more
end

def self.patterns more = []
dictionary[:patterns] | more
end

def self.nouns more = []
dictionary[:nouns] | more
end

def self.numbers more = []
dictionary[:numbers] | more
end
end

33 changes: 33 additions & 0 deletions lib/token_phrase/generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module TokenPhrase
class Generator
attr_accessor :separator, :dictionary, :order

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

def generate
lists.map(&:sample).join(separator).chomp(separator)
end

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

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

0 comments on commit 7e5a087

Please sign in to comment.