Class: XKPassword::Words
- Inherits:
-
Object
- Object
- XKPassword::Words
- Defined in:
- lib/xkpassword/words.rb
Overview
XKPassword::Words basically is a mini database of words. Its job is to provide words that mach a certain criteria. At the moment this criteria is limited to the length of the word.
This uses ‘XKPassword::Store` which is basically the internal store for words. It is expected in the future to make this store configurable and use an external source.
Instance Attribute Summary collapse
-
#words ⇒ Hash
readonly
A collection of words store in a hash with the corresponding key to a word be a function of the lenght of the word.
Instance Method Summary collapse
-
#initialize ⇒ Words
constructor
A new instance of Words.
-
#lengths ⇒ Array<Integer>
Provide lengths available in the databse.
-
#max_length ⇒ Integer
The length of the longest word.
-
#min_length ⇒ Integer
The lenght of the shortest word.
-
#random(length) ⇒ String
Provides a random word with the specified length.
-
#with_length(length) ⇒ Array<String>
Provide an array of words having the specified number of characters in it.
Constructor Details
#initialize ⇒ Words
Returns a new instance of Words.
16 17 18 19 |
# File 'lib/xkpassword/words.rb', line 16 def initialize @words = {} setup end |
Instance Attribute Details
#words ⇒ Hash (readonly)
A collection of words store in a hash with the corresponding key to a word be a function of the lenght of the word.
13 14 15 |
# File 'lib/xkpassword/words.rb', line 13 def words @words end |
Instance Method Details
#lengths ⇒ Array<Integer>
Provide lengths available in the databse
45 46 47 |
# File 'lib/xkpassword/words.rb', line 45 def lengths words.keys.map{ |key| gsub(/l/, '').to_i } end |
#max_length ⇒ Integer
The length of the longest word
59 60 61 |
# File 'lib/xkpassword/words.rb', line 59 def max_length lengths.max end |
#min_length ⇒ Integer
The lenght of the shortest word
52 53 54 |
# File 'lib/xkpassword/words.rb', line 52 def min_length lengths.min end |
#random(length) ⇒ String
Provides a random word with the specified length
37 38 39 40 |
# File 'lib/xkpassword/words.rb', line 37 def random(length) fail ArgumentError, 'Length should be numeric' unless length.is_a? Numeric with_length(length).sample end |
#with_length(length) ⇒ Array<String>
Provide an array of words having the specified number of characters in it
26 27 28 29 |
# File 'lib/xkpassword/words.rb', line 26 def with_length(length) fail ArgumentError 'Length should be a numeric' unless length.is_a? Numeric words[key(length)] end |