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
- .adjective(options = {}) ⇒ Object
- .get_item(filename, options = {}) ⇒ Object
- .items_from_file(filename) ⇒ Object
- .noun(options = {}) ⇒ Object
- .username(options = {}) ⇒ Object
Class Method Details
.adjective(options = {}) ⇒ Object
6 7 8 |
# File 'lib/random_username.rb', line 6 def self.adjective( = {}) get_item("adjectives", ) end |
.get_item(filename, options = {}) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/random_username.rb', line 20 def self.get_item(filename, = {}) items = items_from_file(filename) items.select!{ |item| item.length <= [:max_length] } if [:max_length] items.select!{ |item| item.length >= [:min_length] } if [:min_length] items.sample(random: [: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.("../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( = {}) get_item("nouns", ) end |
.username(options = {}) ⇒ Object
14 15 16 17 18 |
# File 'lib/random_username.rb', line 14 def self.username( = {}) [:max_length] /= 2 if [:max_length] [:min_length] /= 2 if [:min_length] adjective() + noun() end |