class XKPassword::Words

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.

@attr_reader [Hash] words A collection of words store in a hash with the

corresponding key to a word be a function of the
lenght of the word.

Attributes

words[R]

Public Class Methods

new() click to toggle source
# File lib/xkpassword/words.rb, line 16
def initialize
  @words = {}
  setup
end

Public Instance Methods

lengths() click to toggle source

Provide lengths available in the databse

@return [Array<Integer>] A collection of lengths of words available

# File lib/xkpassword/words.rb, line 45
def lengths
  words.keys.map{ |key| key.delete("l").to_i }
end
max_length() click to toggle source

The length of the longest word

@return [Integer] The length of the longest word

# File lib/xkpassword/words.rb, line 59
def max_length
  lengths.max
end
min_length() click to toggle source

The lenght of the shortest word

@return [Integer] The length of the shortest word

# File lib/xkpassword/words.rb, line 52
def min_length
  lengths.min
end
random(length) click to toggle source

Provides a random word with the specified length

@param length [Integer] The number of characters the word should contain

@return [String] A random word with length

# 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) click to toggle source

Provide an array of words having the specified number of characters in it

@param length [String] The number of characters of words should contain

@return [Array<String>] Words from the source that match the length requirement.

# 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

Private Instance Methods

key(length) click to toggle source
# File lib/xkpassword/words.rb, line 76
def key(length)
  "l#{ length }"
end
setup() click to toggle source
# File lib/xkpassword/words.rb, line 65
def setup
  store = XKPassword::Store.new
  data = store.data

  data.each do |datum|
    k = key(datum.length)
    words[k] = [] unless words[k]
    words[k] << datum
  end
end