Skip to content

Commit dd627c7

Browse files
committed
Namespace function seeded_rand_string()
1 parent 339a572 commit dd627c7

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed
Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Generates a consistent random string of specific length based on provided seed.
5-
#
6-
# @example Generate a consistently random string of length 8 with a seed:
7-
# seeded_rand_string(8, "${module_name}::redis_password")
8-
#
9-
# @example Generate a random string from a specific set of characters:
10-
# seeded_rand_string(5, '', 'abcdef')
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::seeded_rand_string`](#stdlibseeded_rand_string) instead.
116
Puppet::Functions.create_function(:seeded_rand_string) do
12-
# @param length Length of string to be generated.
13-
# @param seed Seed string.
14-
# @param charset String that contains characters to use for the random string.
15-
#
16-
# @return [String] Random string.
17-
dispatch :rand_string do
18-
param 'Integer[1]', :length
19-
param 'String', :seed
20-
optional_param 'String[2]', :charset
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
219
end
22-
23-
def rand_string(length, seed, charset = nil)
24-
require 'digest/sha2'
25-
26-
charset ||= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
27-
28-
random_generator = Random.new(Digest::SHA256.hexdigest(seed).to_i(16))
29-
30-
Array.new(length) { charset[random_generator.rand(charset.size)] }.join
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'seeded_rand_string', 'This function is deprecated, please use stdlib::seeded_rand_string instead.')
12+
call_function('stdlib::seeded_rand_string', *args)
3113
end
3214
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Generates a consistent random string of specific length based on provided seed.
5+
#
6+
# @example Generate a consistently random string of length 8 with a seed:
7+
# stdlib::seeded_rand_string(8, "${module_name}::redis_password")
8+
#
9+
# @example Generate a random string from a specific set of characters:
10+
# stdlib::seeded_rand_string(5, '', 'abcdef')
11+
Puppet::Functions.create_function(:'stdlib::seeded_rand_string') do
12+
# @param length Length of string to be generated.
13+
# @param seed Seed string.
14+
# @param charset String that contains characters to use for the random string.
15+
#
16+
# @return [String] Random string.
17+
dispatch :rand_string do
18+
param 'Integer[1]', :length
19+
param 'String', :seed
20+
optional_param 'String[2]', :charset
21+
end
22+
23+
def rand_string(length, seed, charset = nil)
24+
require 'digest/sha2'
25+
26+
charset ||= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
27+
28+
random_generator = Random.new(Digest::SHA256.hexdigest(seed).to_i(16))
29+
30+
Array.new(length) { charset[random_generator.rand(charset.size)] }.join
31+
end
32+
end

spec/functions/seeded_rand_string_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe 'seeded_rand_string' do
5+
describe 'stdlib::seeded_rand_string' do
66
it { is_expected.not_to be(nil) }
77

88
# Test for erroneous params
@@ -22,14 +22,14 @@
2222

2323
# Test behavior
2424
it 'generates the same string with the same seed' do
25-
rand_str_one = call_function(:seeded_rand_string, 300, 'my_seed')
26-
rand_str_two = call_function(:seeded_rand_string, 300, 'my_seed')
25+
rand_str_one = call_function(:'stdlib::seeded_rand_string', 300, 'my_seed')
26+
rand_str_two = call_function(:'stdlib::seeded_rand_string', 300, 'my_seed')
2727

2828
expect(rand_str_one).to eq(rand_str_two)
2929
end
3030
it 'generates different strings if seeded differently' do
31-
rand_str_one = call_function(:seeded_rand_string, 300, 'my_seed_one')
32-
rand_str_two = call_function(:seeded_rand_string, 300, 'my_seed_two')
31+
rand_str_one = call_function(:'stdlib::seeded_rand_string', 300, 'my_seed_one')
32+
rand_str_two = call_function(:'stdlib::seeded_rand_string', 300, 'my_seed_two')
3333

3434
expect(rand_str_one).not_to eq(rand_str_two)
3535
end

0 commit comments

Comments
 (0)