Class: Flexkey::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Flexkey

Initializes and validates a new Flexkey key generator.

Examples:

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' })

Parameters:

  • args (Hash{ Symbol => String, Hash{ String => Symbol, String }}) (defaults to: {})

    the format of the keys to be generated and a pool of available character types specified in the format



30
31
32
33
34
35
# 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

Instance Attribute Details

#char_poolHash{ String => Symbol, String }

Returns a pool of available character types specified in the format.

Returns:

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

    a pool of available character types specified in the format



10
11
12
# File 'lib/flexkey/generator.rb', line 10

def char_pool
  @char_pool
end

#formatString

Returns the format of the keys to be generated.

Returns:

  • (String)

    the format of the keys to be generated



6
7
8
# File 'lib/flexkey/generator.rb', line 6

def format
  @format
end

#n_possible_keysFixnum (readonly)

Returns the number of possible keys for a Flexkey instance with given format and character pool.

Returns:

  • (Fixnum)

    the number of possible keys for a Flexkey instance with given format and character pool



14
15
16
# File 'lib/flexkey/generator.rb', line 14

def n_possible_keys
  @n_possible_keys
end

Instance Method Details

#generate(n = 1) ⇒ String+

Generates a single key or an array of n keys.

Examples:

fk = Flexkey::Generator.new(format: 'nnn-aaa',
                            char_pool: { 'n' => :numeric, 'a' => :alpha_upper_clear })
fk.generate
fk.generate(10)

Parameters:

  • n (Integer) (defaults to: 1)

    the number of keys to generate

Returns:

  • (String)

    a single key

  • (Array<String>)

    n keys



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#inspectObject Also known as: to_s



84
85
86
87
# 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