Skip to content

Commit 6935f8c

Browse files
committed
Namespace function seeded_rand()
1 parent 6051c2b commit 6935f8c

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Generates a random whole number greater than or equal to 0 and less than max, using the value of seed for repeatable randomness.
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::seeded_rand`](#stdlibseeded_rand) instead.
56
Puppet::Functions.create_function(:seeded_rand) do
6-
# @param max The maximum value.
7-
# @param seed The seed used for repeatable randomness.
8-
#
9-
# @return [Integer]
10-
# A random number greater than or equal to 0 and less than max
11-
dispatch :seeded_rand do
12-
param 'Integer[1]', :max
13-
param 'String', :seed
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
149
end
15-
16-
def seeded_rand(max, seed)
17-
require 'digest/md5'
18-
19-
seed = Digest::MD5.hexdigest(seed).hex
20-
Puppet::Util.deterministic_rand_int(seed, max)
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'seeded_rand', 'This function is deprecated, please use stdlib::seeded_rand instead.')
12+
call_function('stdlib::seeded_rand', *args)
2113
end
2214
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Generates a random whole number greater than or equal to 0 and less than max, using the value of seed for repeatable randomness.
5+
Puppet::Functions.create_function(:'stdlib::seeded_rand') do
6+
# @param max The maximum value.
7+
# @param seed The seed used for repeatable randomness.
8+
#
9+
# @return [Integer]
10+
# A random number greater than or equal to 0 and less than max
11+
dispatch :seeded_rand do
12+
param 'Integer[1]', :max
13+
param 'String', :seed
14+
end
15+
16+
def seeded_rand(max, seed)
17+
require 'digest/md5'
18+
19+
seed = Digest::MD5.hexdigest(seed).hex
20+
Puppet::Util.deterministic_rand_int(seed, max)
21+
end
22+
end

spec/functions/seeded_rand_spec.rb

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

33
require 'spec_helper'
44

5-
describe 'seeded_rand' do
5+
describe 'stdlib::seeded_rand' do
66
it { is_expected.not_to be_nil }
7-
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got none}i) }
8-
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{'seeded_rand' expects 2 arguments, got 1}i) }
7+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::seeded_rand' expects 2 arguments, got none}i) }
8+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{'stdlib::seeded_rand' expects 2 arguments, got 1}i) }
99
it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer\[1\] value, got Integer\[0, 0\]}) }
1010
it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer value, got Float}) }
1111
it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, %r{parameter 'max' expects an Integer\[1\] value, got Integer\[-10, -10\]}) }

0 commit comments

Comments
 (0)