Class: CWG::Randomize

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

Overview

class Randomize provides character randomising

Instance Method Summary (collapse)

Constructor Details

- (Randomize) initialize(options, chars)

Returns a new instance of Randomize



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

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

Instance Method Details

- (Boolean) chars_processed?

Returns:

  • (Boolean)


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

def chars_processed?
  process_chars
  ! missing_letters_or_numbers?
end

- (Object) chars_to_alpha



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

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

- (Object) generate



65
66
67
68
69
70
71
72
73
# File '/Users/martyn/cw/cw_clone/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

- (Boolean) has_no_letter?

Returns:

  • (Boolean)


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

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

- (Boolean) has_no_number?

Returns:

  • (Boolean)


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

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

- (Object) lengthen_chars



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

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

- (Boolean) missing_letters_or_numbers?

Returns:

  • (Boolean)


46
47
48
49
50
51
# File '/Users/martyn/cw/cw_clone/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

- (Object) process_chars



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

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

- (Object) shuffle_chars



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

def shuffle_chars
  @chars.shuffle!
end

- (Object) size



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

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

- (Object) take_chars(size)



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

def take_chars size
  @chars.take size
end

- (Object) word_count



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

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