Module: Flexkey::CharPool
Instance Method Summary collapse
-
#available_char_pools ⇒ Hash{ Symbol => String }
Provides a list of the available built-in character pool types with the characters of each type.
-
#available_char_types ⇒ Array<Symbol>
Provides a list of the available built-in character pool types.
-
#generate(arg) ⇒ Array<String>
Generates an array of characters of the specified types and proportions.
Instance Method Details
permalink #available_char_pools ⇒ Hash{ Symbol => String }
Provides a list of the available built-in character pool types with the characters of each type.
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 |
permalink #available_char_types ⇒ Array<Symbol>
Provides a list of the available built-in character pool types.
45 46 47 |
# File 'lib/flexkey/char_pool.rb', line 45 def available_char_types character_types.keys end |
permalink #generate(arg) ⇒ Array<String>
Generates an array of characters of the specified types and proportions
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 |