X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Ftry_get_value.rb;fp=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Ftry_get_value.rb;h=34f94762e218a379422b425135119c56fc09e870;hb=131e09855e065be940e104d9ab0f18940cc76257;hp=fc19a230adea94526df50fc7c67ab2693fa5262f;hpb=407d322498f4fde815abf381007fbecfe5c10b2b;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/try_get_value.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/try_get_value.rb index fc19a230a..34f94762e 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/try_get_value.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/try_get_value.rb @@ -1,43 +1,46 @@ +# +# try_get_value.rb +# module Puppet::Parser::Functions newfunction( - :try_get_value, - :type => :rvalue, - :arity => -2, - :doc => <<-eos -DEPRECATED: this function is deprecated, please use dig() instead. - -Looks up into a complex structure of arrays and hashes and returns a value -or the default value if nothing was found. - -Key can contain slashes to describe path components. The function will go down -the structure and try to extract the required value. - -$data = { - 'a' => { - 'b' => [ - 'b1', - 'b2', - 'b3', - ] - } -} - -$value = try_get_value($data, 'a/b/2', 'not_found', '/') -=> $value = 'b3' - -a -> first hash key -b -> second hash key -2 -> array index starting with 0 - -not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil. -/ -> (optional) path delimiter. Defaults to '/'. - -In addition to the required "key" argument, "try_get_value" accepts default -argument. It will be returned if no value was found or a path component is -missing. And the fourth argument can set a variable path separator. - eos + :try_get_value, + :type => :rvalue, + :arity => -2, + :doc => <<-DOC + DEPRECATED: this function is deprecated, please use dig() instead. + + Looks up into a complex structure of arrays and hashes and returns a value + or the default value if nothing was found. + + Key can contain slashes to describe path components. The function will go down + the structure and try to extract the required value. + + $data = { + 'a' => { + 'b' => [ + 'b1', + 'b2', + 'b3', + ] + } + } + + $value = try_get_value($data, 'a/b/2', 'not_found', '/') + => $value = 'b3' + + a -> first hash key + b -> second hash key + 2 -> array index starting with 0 + + not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil. + / -> (optional) path delimiter. Defaults to '/'. + + In addition to the required "key" argument, "try_get_value" accepts default + argument. It will be returned if no value was found or a path component is + missing. And the fourth argument can set a variable path separator. + DOC ) do |args| - warning("try_get_value() DEPRECATED: this function is deprecated, please use dig() instead.") + warning('try_get_value() DEPRECATED: this function is deprecated, please use dig() instead.') data = args[0] path = args[1] || '' default = args[2] @@ -47,7 +50,7 @@ missing. And the fourth argument can set a variable path separator. end separator = args[3] || '/' - path = path.split(separator).map{ |key| key =~ /^\d+$/ ? key.to_i : key } + path = path.split(separator).map { |key| (key =~ %r{^\d+$}) ? key.to_i : key } function_dig([data, path, default]) end end