Sha256: bc427f67742fd90b50b6f117641fa36d415d9c97080a16cdf9592684914eb28c

Contents?: true

Size: 922 Bytes

Versions: 3

Compression:

Stored size: 922 Bytes

Contents

class NameGenerator

  def self.instance
    @@instance ||= NameGenerator.new
  end

  def initialize
    @female_first_names = parse_name_file('female_first_names.txt') # Top 500 female names.
    @male_first_names = parse_name_file('male_first_names.txt') # Top 500 male names.
    @last_names = parse_name_file('last_names.txt') # Top 3000 last names.
    @first_names = @female_first_names + @male_first_names
  end

  def parse_name_file(filename)
    File.readlines(File.join(DB_DIR, filename)).collect {|line| line.strip}
  end

  def random(options = {})
    [random_first_name(options), random_last_name(options)]
  end  

  def random_first_name(options = {})
    case options[:gender]
      when 'm', 'male' then @male_first_names.random
      when 'f', 'female' then @female_first_names.random
      else @first_names.random
    end
  end

  def random_last_name(options = {})
    @last_names.random
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
randomizer-0.0.4 lib/randomizer/name_generator.rb
randomizer-0.0.3 lib/randomizer/name_generator.rb
randomizer-0.0.2 lib/randomizer/name_generator.rb