Class: Keybox::StringGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/keybox/string_generator.rb

Overview

base class for string generation. A string generator can be called serially to generate a new string in chunks, or all at once to generate one of a certain size.

Direct Known Subclasses

CharGramGenerator, SymbolSetGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringGenerator

Returns a new instance of StringGenerator.



14
15
16
17
18
19
20
# File 'lib/keybox/string_generator.rb', line 14

def initialize
    @chunks = Array.new
    @min_length = 8
    @max_length = 10
    @randomizer = Keybox::Randomizer.new
    @autoclear = true
end

Instance Attribute Details

#autoclearObject

Returns the value of attribute autoclear.



12
13
14
# File 'lib/keybox/string_generator.rb', line 12

def autoclear
  @autoclear
end

#chunksObject

Returns the value of attribute chunks.



9
10
11
# File 'lib/keybox/string_generator.rb', line 9

def chunks
  @chunks
end

#max_lengthObject

Returns the value of attribute max_length.



11
12
13
# File 'lib/keybox/string_generator.rb', line 11

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



10
11
12
# File 'lib/keybox/string_generator.rb', line 10

def min_length
  @min_length
end

Instance Method Details

#clearObject



28
29
30
# File 'lib/keybox/string_generator.rb', line 28

def clear
    @chunks.clear
end

#generateObject



22
23
24
25
26
# File 'lib/keybox/string_generator.rb', line 22

def generate
    clear if autoclear
    generate_chunk until valid?
    self.to_s
end

#generate_chunkObject



41
42
43
# File 'lib/keybox/string_generator.rb', line 41

def generate_chunk
    raise Keybox::KeyboxError, "generate_chunk Not Implemented"
end

#to_sObject



32
33
34
# File 'lib/keybox/string_generator.rb', line 32

def to_s
    @chunks.join('')[0...@max_length]
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:



36
37
38
39
# File 'lib/keybox/string_generator.rb', line 36

def valid?
    raise Keybox::ValidationError, "max_length (#{max_length}) must be greater than or equal to min_length (#{min_length})", caller if max_length < min_length
    @chunks.join('').size >= @min_length 
end