Class: Keybox::StringGenerator
- Inherits:
-
Object
- Object
- Keybox::StringGenerator
- 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
Instance Attribute Summary collapse
-
#autoclear ⇒ Object
Returns the value of attribute autoclear.
-
#chunks ⇒ Object
Returns the value of attribute chunks.
-
#max_length ⇒ Object
Returns the value of attribute max_length.
-
#min_length ⇒ Object
Returns the value of attribute min_length.
Instance Method Summary collapse
- #clear ⇒ Object
- #generate ⇒ Object
- #generate_chunk ⇒ Object
-
#initialize ⇒ StringGenerator
constructor
A new instance of StringGenerator.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ StringGenerator
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
#autoclear ⇒ Object
Returns the value of attribute autoclear.
12 13 14 |
# File 'lib/keybox/string_generator.rb', line 12 def autoclear @autoclear end |
#chunks ⇒ Object
Returns the value of attribute chunks.
9 10 11 |
# File 'lib/keybox/string_generator.rb', line 9 def chunks @chunks end |
#max_length ⇒ Object
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_length ⇒ Object
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
#clear ⇒ Object
28 29 30 |
# File 'lib/keybox/string_generator.rb', line 28 def clear @chunks.clear end |
#generate ⇒ Object
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_chunk ⇒ Object
41 42 43 |
# File 'lib/keybox/string_generator.rb', line 41 def generate_chunk raise Keybox::KeyboxError, "generate_chunk Not Implemented" end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/keybox/string_generator.rb', line 32 def to_s @chunks.join('')[0...@max_length] end |
#valid? ⇒ Boolean
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 |