Class: Senha::Base::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/senha/base/generator.rb

Overview

A class for handling the generation of a password based on options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Creates a new instance of the Generator class



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/senha/base/generator.rb', line 37

def initialize(options)
  @available_chars = Array.new

  @numbers = ('0'..'9').to_a
  @lower_case = ('a'..'z').to_a
  @upper_case = ('A'..'Z').to_a
  @punctuation = %w(. , ! :).to_a
  @symbols = %w(~ ! @ # $ % ^ & * ( ) _).to_a

  if options[:all]
    @available_chars.concat @numbers
    @available_chars.concat @lower_case
    @available_chars.concat @upper_case
    @available_chars.concat @symbols
    @available_chars.concat @punctuation
  else
    if options[:numbers]
      @available_chars.concat @numbers
    end

    if options[:lowercase]
      @available_chars.concat @lower_case
    end

    if options[:uppercase]
      @available_chars.concat @upper_case
    end

    if options[:symbols]
      @available_chars.concat @symbols
    end

    if options[:punct]
      @available_chars.concat @punctuation
    end
  end
end

Instance Attribute Details

#available_charsObject

Returns the value of attribute available_chars.



32
33
34
# File 'lib/senha/base/generator.rb', line 32

def available_chars
  @available_chars
end

Instance Method Details

#password(length = 10) ⇒ String

Generates a password

Returns:

  • (String)

    of the randomly generated password



78
79
80
81
82
# File 'lib/senha/base/generator.rb', line 78

def password(length = 10)
  1.upto(length).collect do |a|
    @available_chars[rand(@available_chars.size)]
  end.join
end