Class: CW::Randomize
- Inherits:
-
Object
- Object
- CW::Randomize
- Defined in:
- lib/cw/randomize.rb
Overview
class Randomize provides character randomising
Instance Method Summary collapse
- #chars_processed? ⇒ Boolean
- #chars_to_alpha ⇒ Object
- #generate ⇒ Object
- #has_no_letter? ⇒ Boolean
- #has_no_number? ⇒ Boolean
-
#initialize(options, chars) ⇒ Randomize
constructor
A new instance of Randomize.
- #lengthen_chars ⇒ Object
- #missing_letters_or_numbers? ⇒ Boolean
- #process_chars ⇒ Object
- #shuffle_chars ⇒ Object
- #size ⇒ Object
- #take_chars(size) ⇒ Object
- #word_count ⇒ Object
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(, chars) = @chars = chars end |
Instance Method Details
#chars_processed? ⇒ Boolean
60 61 62 63 |
# File 'lib/cw/randomize.rb', line 60 def chars_processed? process_chars ! missing_letters_or_numbers? end |
#chars_to_alpha ⇒ Object
34 35 36 |
# File 'lib/cw/randomize.rb', line 34 def chars_to_alpha @chrs.collect{|char| char.chr}.join end |
#generate ⇒ Object
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
38 39 40 |
# File 'lib/cw/randomize.rb', line 38 def has_no_letter? @alpha[/[a-zA-Z]+/] == @alpha end |
#has_no_number? ⇒ Boolean
42 43 44 |
# File 'lib/cw/randomize.rb', line 42 def has_no_number? @alpha[/[0-9]+/] == @alpha end |
#lengthen_chars ⇒ Object
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
46 47 48 49 50 51 |
# File 'lib/cw/randomize.rb', line 46 def missing_letters_or_numbers? if && [:letters_numbers] return true if has_no_letter? return true if has_no_number? end end |
#process_chars ⇒ Object
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_chars ⇒ Object
26 27 28 |
# File 'lib/cw/randomize.rb', line 26 def shuffle_chars @chars.shuffle! end |
#size ⇒ Object
18 19 20 |
# File 'lib/cw/randomize.rb', line 18 def size [:size] ? [: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_count ⇒ Object
14 15 16 |
# File 'lib/cw/randomize.rb', line 14 def word_count [:count] ? [:count] : 50 end |