Class: RandomizedField::Generator

Inherits:
Object
  • Object
show all
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

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(options = {}, &block)
  @length = options.fetch(:length, DEFAULTS[:length])
  @valid_characters = options.fetch(:valid_characters, DEFAULTS[:valid_characters])
  @callback = block || :itself.to_proc
end

Instance Method Details

#generateObject



19
20
21
# File 'lib/randomized_field/generator.rb', line 19

def generate
  callback.call(Array.new(length) { valid_characters.sample }.join)
end