Module: RandomUsername

Defined in:
lib/random_username.rb,
lib/random_username/version.rb

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.adjective(options = {}) ⇒ Object



6
7
8
# File 'lib/random_username.rb', line 6

def self.adjective(options = {})
  get_item("adjectives", options)
end

.get_item(filename, options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/random_username.rb', line 20

def self.get_item(filename, options = {})
  items = items_from_file(filename)
  items.select!{ |item| item.length <= options[:max_length] } if options[:max_length]
  items.select!{ |item| item.length >= options[:min_length] } if options[:min_length]
  items.sample(random: options[:random] || Random.new) || fail(RandomUsername::Error, "No words found")
end

.items_from_file(filename) ⇒ Object



27
28
29
30
# File 'lib/random_username.rb', line 27

def self.items_from_file(filename)
  filepath = File.expand_path("../random_username/#{filename}.txt", __FILE__)
  File.read(filepath).split("\n")
end

.noun(options = {}) ⇒ Object



10
11
12
# File 'lib/random_username.rb', line 10

def self.noun(options = {})
  get_item("nouns", options)
end

.username(options = {}) ⇒ Object



14
15
16
17
18
# File 'lib/random_username.rb', line 14

def self.username(options = {})
  options[:max_length] /= 2 if options[:max_length]
  options[:min_length] /= 2 if options[:min_length]
  adjective(options) + noun(options)
end