Class: XKPassword::Words

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeWords

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

#wordsHash (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.

Returns:

  • (Hash)

    the current value of words



13
14
15
# File 'lib/xkpassword/words.rb', line 13

def words
  @words
end

Instance Method Details

#lengthsArray<Integer>

Provide lengths available in the databse

Returns:

  • (Array<Integer>)

    A collection of lengths of words available



45
46
47
# File 'lib/xkpassword/words.rb', line 45

def lengths
  words.keys.map{ |key| key.delete("l").to_i }
end

#max_lengthInteger

The length of the longest word

Returns:

  • (Integer)

    The length of the longest word



59
60
61
# File 'lib/xkpassword/words.rb', line 59

def max_length
  lengths.max
end

#min_lengthInteger

The lenght of the shortest word

Returns:

  • (Integer)

    The length 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

Parameters:

  • length (Integer)

    The number of characters the word should contain

Returns:

  • (String)

    A random word with 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

Parameters:

  • length (String)

    The number of characters of words should contain

Returns:

  • (Array<String>)

    Words from the source that match the length requirement.



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