class Flexkey::Generator
Attributes
char_pool[RW]
@return [Hash{ String => Symbol, String }] a pool of available character types specified in
the format
format[RW]
@return [String] the format of the keys to be generated
n_possible_keys[R]
@return [Fixnum] the number of possible keys for a Flexkey
instance with given format and
character pool
Public Class Methods
new(args = {})
click to toggle source
Initializes and validates a new Flexkey
key generator.
@param args [Hash{ Symbol => String, Hash{ String => Symbol, String }}] the format of the keys
to be generated and a pool of available character types specified in the format
@return [Flexkey] the Flexkey
instance
@example
Flexkey::Generator.new(format: 'nnn-aaa', char_pool: { 'n' => :numeric, 'a' => :alpha_upper_clear }) Flexkey::Generator.new(format: 'ccccc-ccccc', char_pool: { 'c' => { alpha_upper: 0.75, alpha_lower: 0.25 } }) Flexkey::Generator.new(format: 'key_#######', char_pool: { '#' => :numeric_clear }) Flexkey::Generator.new(format: 'a-nnnn', char_pool: { 'a' => :alpha_upper, 'n' => '12345' })
# File lib/flexkey/generator.rb, line 30 def initialize(args = {}) @format, @char_pool = args[:format], args[:char_pool] validate! set_char_pool calculate_n_possible_keys end
Public Instance Methods
char_pool=(new_char_pool)
click to toggle source
# File lib/flexkey/generator.rb, line 43 def char_pool=(new_char_pool) @char_pool = new_char_pool validate! set_char_pool calculate_n_possible_keys end
format=(new_format)
click to toggle source
# File lib/flexkey/generator.rb, line 37 def format=(new_format) @format = new_format validate! calculate_n_possible_keys end
generate(n = 1)
click to toggle source
Generates a single key or an array of ‘n` keys.
@param n [Integer] the number of keys to generate
@return [String] a single key @return [Array<String>] ‘n` keys
@example
fk = Flexkey::Generator.new(format: 'nnn-aaa', char_pool: { 'n' => :numeric, 'a' => :alpha_upper_clear }) fk.generate fk.generate(10)
# File lib/flexkey/generator.rb, line 62 def generate(n = 1) validate_n!(n) if n == 1 generate_one elsif n > 1 keys = [] new_key = nil n.times do loop do new_key = generate_one break unless keys.include?(new_key) end keys << new_key end keys end end
inspect()
click to toggle source
# File lib/flexkey/generator.rb, line 84 def inspect %Q{#<#{self.class} @format="#{format}", @char_pool=#{@char_pool}, } + %Q{@n_possible_keys=#{@n_possible_keys}>} end
Also aliased as: to_s
Private Instance Methods
calculate_n_possible_keys()
click to toggle source
# File lib/flexkey/generator.rb, line 105 def calculate_n_possible_keys @n_possible_keys = @format.chars.to_a.map do |c| @_char_pool[c].nil? ? 1 : @_char_pool[c].size end.inject(:*) end
generate_one()
click to toggle source
# File lib/flexkey/generator.rb, line 111 def generate_one @format.chars.to_a.map { |c| @_char_pool[c].nil? ? c : @_char_pool[c].sample }.join end
set_char_pool()
click to toggle source
# File lib/flexkey/generator.rb, line 101 def set_char_pool @_char_pool = @char_pool.inject({}) { |h, (k, v)| h[k] = CharPool.generate(v); h } end
validate!()
click to toggle source
# File lib/flexkey/generator.rb, line 92 def validate! raise GeneratorError.new('format is required') if @format.nil? || @format.empty? raise GeneratorError.new('char_pool is required') if @char_pool.nil? || @char_pool.empty? raise GeneratorError.new('char_pool letters must each be strings of length 1') unless @char_pool.keys.all? { |letter| letter.is_a?(String) && letter.length == 1 } raise GeneratorError.new('No char_pool letters present in format') if (@format.chars.to_a & @char_pool.keys).empty? end
validate_n!(n)
click to toggle source
# File lib/flexkey/generator.rb, line 115 def validate_n!(n) raise GeneratorError.new("There are only #{@n_possible_keys} possible keys") if n > @n_possible_keys end