class Usernamegen

Constants

ROOT
VERSION

Public Class Methods

all(opts = {}, &block) click to toggle source
# File lib/usernamegen.rb, line 17
def self.all(opts = {}, &block)
  new(opts).all(&block)
end
all_for_desc(desc, opts = {}, &block) click to toggle source
# File lib/usernamegen.rb, line 25
def self.all_for_desc(desc, opts = {}, &block)
  new(opts).all_for_desc(desc, &block)
end
all_for_thing(thing, opts = {}, &block) click to toggle source
# File lib/usernamegen.rb, line 21
def self.all_for_thing(thing, opts = {}, &block)
  new(opts).all_for_thing(thing, &block)
end
new(opts = {}) click to toggle source
# File lib/usernamegen.rb, line 29
def initialize opts = {}, &block
  @opts = opts.reverse_merge({
    descriptions: "#{ROOT}/lib/usernamegen/descriptions.txt",
    things: "#{ROOT}/lib/usernamegen/things.txt",
    rng: Random.new,
    format: block || ->(combination){ combination.join(" ").titleize },
  })
  @descriptions = load_file @opts[:descriptions]
  @things = load_file @opts[:things]
end
one(opts = {}, &block) click to toggle source
# File lib/usernamegen.rb, line 9
def self.one(opts = {}, &block)
  new(opts).one(&block)
end
some(amount, opts = {}, &block) click to toggle source
# File lib/usernamegen.rb, line 13
def self.some(amount, opts = {}, &block)
  new(opts).some(amount, &block)
end

Public Instance Methods

all(&block) click to toggle source
# File lib/usernamegen.rb, line 53
def all &block
  @descriptions.product(@things).map(&(block || @opts[:format]))
end
all_for_desc(desc, &block) click to toggle source
# File lib/usernamegen.rb, line 61
def all_for_desc desc, &block
  [desc].product(@things).map(&(block || @opts[:format]))
end
all_for_thing(thing, &block) click to toggle source
# File lib/usernamegen.rb, line 57
def all_for_thing thing, &block
  @descriptions.product([thing]).map(&(block || @opts[:format]))
end
load_file(file) click to toggle source
# File lib/usernamegen.rb, line 40
def load_file file
  File.read(file).split("\n").map(&:strip).reject(&:blank?)
end
one(&block) click to toggle source
# File lib/usernamegen.rb, line 44
def one &block
  combination = [@descriptions.sample(1, random: @opts[:rng]), @things.sample(1, random: @opts[:rng])]
  (block || @opts[:format]).call(combination)
end
some(amount, &block) click to toggle source
# File lib/usernamegen.rb, line 49
def some amount, &block
  amount.times.map{ one(&block) }
end