Module: Flexkey::CharPool

Extended by:
CharPool
Included in:
CharPool
Defined in:
lib/flexkey/char_pool.rb

Instance Method Summary collapse

Instance Method Details

#available_char_poolsHash{ Symbol => String }

Provides a list of the available built-in character pool types with the characters of each type.

Examples:

Flexkey::CharPool.available_char_pools

Returns:

  • (Hash{ Symbol => String })

    a hash of character pool types and their characters



56
57
58
# File 'lib/flexkey/char_pool.rb', line 56

def available_char_pools
  character_types.inject({}) { |acc, (k, v)| acc[k] = v.join; acc }
end

#available_char_typesArray<Symbol>

Provides a list of the available built-in character pool types.

Examples:

Flexkey::CharPool.available_char_types

Returns:

  • (Array<Symbol>)

    the list of character pool types



45
46
47
# File 'lib/flexkey/char_pool.rb', line 45

def available_char_types
  character_types.keys
end

#generate(arg) ⇒ Array<String>

Generates an array of characters of the specified types and proportions

Examples:

Flexkey::CharPool.generate(:numeric)
Flexkey::CharPool.generate('XYZ01234')
Flexkey::CharPool.generate({ numeric: 0.25, alpha_upper: 0.75 })
Flexkey::CharPool.generate({ alpha_lower_clear: 1, alpha_upper_clear: 3 })
Flexkey::CharPool.generate({ 'AEIOU' => 1, 'BCDFGHJKLMNPQRSTVWXYZ' => 1, symbol: 0.5 })

Parameters:

  • arg (Hash{ Symbol, String => Fixnum }, Symbol, String)

    a single character type or a hash of several character types and their proportions

Returns:

  • (Array<String>)

    the requested character pool



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flexkey/char_pool.rb', line 20

def generate(arg)
  if arg.is_a?(Hash)
    # Extract character types and proportions.
    char_arrays = arg.keys.map { |t| single_pool(t) }
    char_props = arg.values.each do |p|
      raise CharPoolError.new("Invalid char_pool proportion #{p.inspect}") unless
        p.is_a?(Numeric) && p >= 0
    end

    # Standardize the proportions if they don't sum to 1.
    total_prop = char_props.inject(:+).to_f
    char_props = char_props.map { |prop| prop / total_prop }

    multiple_pool(char_arrays, char_props)
  else
    single_pool(arg)
  end
end