Class: CW::Randomize

Inherits:
Object
  • Object
show all
Defined in:
lib/cw/randomize.rb

Overview

class Randomize provides character randomising

Instance Method Summary collapse

Constructor Details

#initialize(options, chars) ⇒ Randomize

Returns a new instance of Randomize.



9
10
11
12
# File 'lib/cw/randomize.rb', line 9

def initialize(options, chars)
  @options = options
  @chars = chars
end

Instance Method Details

#chars_processed?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/cw/randomize.rb', line 60

def chars_processed?
  process_chars
  ! missing_letters_or_numbers?
end

#chars_to_alphaObject



34
35
36
# File 'lib/cw/randomize.rb', line 34

def chars_to_alpha
  @chrs.collect{|char| char.chr}.join
end

#generateObject



65
66
67
68
69
70
71
72
73
# 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?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/cw/randomize.rb', line 38

def has_no_letter?
  @alpha[/[a-zA-Z]+/] == @alpha
end

#has_no_number?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cw/randomize.rb', line 42

def has_no_number?
  @alpha[/[0-9]+/] == @alpha
end

#lengthen_charsObject



22
23
24
# File 'lib/cw/randomize.rb', line 22

def lengthen_chars
  @chars += @chars while(@chars.length < size)
end

#missing_letters_or_numbers?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# 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_charsObject



53
54
55
56
57
58
# File 'lib/cw/randomize.rb', line 53

def process_chars
  lengthen_chars
  shuffle_chars
  @chrs = take_chars size
  @alpha = chars_to_alpha
end

#shuffle_charsObject



26
27
28
# File 'lib/cw/randomize.rb', line 26

def shuffle_chars
  @chars.shuffle!
end

#sizeObject



18
19
20
# File 'lib/cw/randomize.rb', line 18

def size
  @options[:size] ? @options[:size] : 4
end

#take_chars(size) ⇒ Object



30
31
32
# File 'lib/cw/randomize.rb', line 30

def take_chars size
  @chars.take size
end

#word_countObject



14
15
16
# File 'lib/cw/randomize.rb', line 14

def word_count
  @options[:count] ? @options[:count] : 50
end