class CW::Randomize

class Randomize provides character randomising

Public Class Methods

new(options, chars) click to toggle source
# File lib/cw/randomize.rb, line 9
def initialize(options, chars)
  @options = options
  @chars = chars
end

Public Instance Methods

chars_processed?() click to toggle source
# File lib/cw/randomize.rb, line 60
def chars_processed?
  process_chars
  ! missing_letters_or_numbers?
end
chars_to_alpha() click to toggle source
# File lib/cw/randomize.rb, line 34
def chars_to_alpha
  @chrs.collect{|char| char.chr}.join
end
generate() click to toggle source
# File lib/cw/randomize.rb, line 65
def generate
  @words, count = [], word_count
  while count > 0
    next unless chars_processed?
    @words.push @alpha
    count -= 1
  end
  @words
end
has_no_letter?() click to toggle source
# File lib/cw/randomize.rb, line 38
def has_no_letter?
  @alpha[/[a-zA-Z]+/] == @alpha
end
has_no_number?() click to toggle source
# File lib/cw/randomize.rb, line 42
def has_no_number?
  @alpha[/[0-9]+/] == @alpha
end
lengthen_chars() click to toggle source
# File lib/cw/randomize.rb, line 22
def lengthen_chars
  @chars += @chars while(@chars.length < size)
end
missing_letters_or_numbers?() click to toggle source
# File lib/cw/randomize.rb, line 46
def missing_letters_or_numbers?
  if @options && @options[:letters_numbers]
    return true if has_no_letter?
    return true if has_no_number?
  end
end
process_chars() click to toggle source
# File lib/cw/randomize.rb, line 53
def process_chars
  lengthen_chars
  shuffle_chars
  @chrs = take_chars size
  @alpha = chars_to_alpha
end
shuffle_chars() click to toggle source
# File lib/cw/randomize.rb, line 26
def shuffle_chars
  @chars.shuffle!
end
size() click to toggle source
# File lib/cw/randomize.rb, line 18
def size
  @options[:size] ? @options[:size] : 4
end
take_chars(size) click to toggle source
# File lib/cw/randomize.rb, line 30
def take_chars size
  @chars.take size
end
word_count() click to toggle source
# File lib/cw/randomize.rb, line 14
def word_count
  @options[:count] ? @options[:count] : 50
end