Class: RandomizedField::Generator
- Inherits:
-
Object
- Object
- RandomizedField::Generator
- Defined in:
- lib/randomized_field/generator.rb
Overview
Class to generate a random string with a given length and set of valid characters.
Constant Summary collapse
- DEFAULTS =
{ length: 8, valid_characters: [*("0".."9"), *("a".."z")] }.freeze
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(options = {}, &block) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(options = {}, &block) ⇒ Generator
Returns a new instance of Generator.
13 14 15 16 17 |
# File 'lib/randomized_field/generator.rb', line 13 def initialize( = {}, &block) @length = .fetch(:length, DEFAULTS[:length]) @valid_characters = .fetch(:valid_characters, DEFAULTS[:valid_characters]) @callback = block || :itself.to_proc end |
Instance Method Details
#generate ⇒ Object
19 20 21 |
# File 'lib/randomized_field/generator.rb', line 19 def generate callback.call(Array.new(length) { valid_characters.sample }.join) end |