From 09abea2d47b09b9e8933a1c27c7a454c77760e83 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 16:52:14 +0200 Subject: Moved type.rb --- lib/puppet/parser/functions/type.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/puppet/parser/functions/type.rb (limited to 'lib/puppet/parser/functions/type.rb') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb new file mode 100644 index 0000000..389a056 --- /dev/null +++ b/lib/puppet/parser/functions/type.rb @@ -0,0 +1,33 @@ +# +# type.rb +# + +module Puppet::Parser::Functions + newfunction(:type, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "type(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + klass = value.class + + if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type(): Unknown type') + end + + klass = klass.to_s # Ugly ... + + # We note that Integer is the parent to Bignum and Fixnum ... + result = case klass + when /^(?:Big|Fix)num$/ then 'Integer' + else klass + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 19313b43ea04066a88a0b78e83650ac52785e2e9 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:18:56 +0100 Subject: (#3) Apply missing documentation to more functions. --- lib/puppet/parser/functions/type.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions/type.rb') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 389a056..de6087e 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:type, :type => :rvalue, :doc => <<-EOS +Returns the type when passed a variable. Type can be one of: + +* string +* array +* hash +* float +* integer EOS ) do |arguments| @@ -22,11 +29,19 @@ module Puppet::Parser::Functions # We note that Integer is the parent to Bignum and Fixnum ... result = case klass - when /^(?:Big|Fix)num$/ then 'Integer' + when /^(?:Big|Fix)num$/ then 'integer' else klass end - return result + if result == "String" then + if value == value.to_i.to_s then + result = "Integer" + elsif value == value.to_f.to_s then + result = "Float" + end + end + + return result.downcase end end -- cgit v1.2.3 From a1cae426f1d6b8f2c19184ec8aac3ebc47d97744 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 23:09:30 +0100 Subject: (#3) Provide documentation for remaining functions. --- lib/puppet/parser/functions/type.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions/type.rb') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index de6087e..8d85f11 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -11,6 +11,7 @@ Returns the type when passed a variable. Type can be one of: * hash * float * integer +* boolean EOS ) do |arguments| @@ -21,7 +22,7 @@ Returns the type when passed a variable. Type can be one of: klass = value.class - if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) raise(Puppet::ParseError, 'type(): Unknown type') end @@ -30,6 +31,7 @@ Returns the type when passed a variable. Type can be one of: # We note that Integer is the parent to Bignum and Fixnum ... result = case klass when /^(?:Big|Fix)num$/ then 'integer' + when /^(?:True|False)Class$/ then 'boolean' else klass end -- cgit v1.2.3