Module: Passfaker::Generator

Defined in:
lib/passfaker/generator.rb

Overview

パスワード生成のためのモジュール

Constant Summary collapse

CATEGORIES =
[
  -> { Faker::Color.color_name },
  -> { Faker::Creature::Animal.name },
  -> { Faker::Food.ingredient },
  -> { Faker::Games::Pokemon.name },
  -> { Faker::Music.instrument },
  -> { Faker::Superhero.name },
  -> { Faker::Music.genre }
].freeze

Class Method Summary collapse

Class Method Details

.generate(word_count:, separator:, include_number:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/passfaker/generator.rb', line 20

def self.generate(word_count:, separator:, include_number:)
  words = word_count.times.map do
    generator = CATEGORIES[SecureRandom.random_number(CATEGORIES.size)]
    word = generator.call.downcase.gsub(/\s+/, "")
    random_char_upcase(word)
  end

  words << ::SecureRandom.random_number(100).to_s if include_number
  words.shuffle.join(separator)
end

.random_char_upcase(word) ⇒ Object

ランダムな文字を大文字に変換する



32
33
34
35
36
37
38
39
# File 'lib/passfaker/generator.rb', line 32

def self.random_char_upcase(word)
  return word if word.empty?

  chars = word.chars
  index = SecureRandom.random_number(chars.size)
  chars[index] = chars[index].upcase
  chars.join
end