Module: Puppet::Parser::Functions
- Extended by:
- Util
- Defined in:
- lib/puppet/parser/functions.rb,
lib/puppet/parser/functions/hiera.rb,
lib/puppet/parser/functions/split.rb,
lib/puppet/parser/functions/extlookup.rb,
lib/puppet/parser/functions/hiera_hash.rb,
lib/puppet/parser/functions/hiera_array.rb,
lib/puppet/parser/functions/hiera_include.rb
Overview
A module for managing parser functions. Each specified function is added to a central module that then gets included into the Scope class.
Constant Summary collapse
- Environment =
Puppet::Node::Environment
Constants included from Util
Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE
Constants included from Util::POSIX
Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS
Constants included from Util::SymbolicFileMode
Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit
Class Method Summary collapse
-
.arity(name) ⇒ Integer
Return the number of arguments a function expects.
-
.autoloader ⇒ Object
private
Accessor for singleton autoloader.
-
.environment_module(env = nil) ⇒ Object
private
Get the module that functions are mixed into corresponding to an environment.
-
.function(name) ⇒ Symbol, false
Determine if a function is defined.
- .functiondocs ⇒ Object
-
.newfunction(name, options = {}, &block) ⇒ Hash
Create a new Puppet DSL function.
-
.reset ⇒ Object
private
Reset the list of loaded functions.
-
.rvalue?(name) ⇒ Boolean
Determine whether a given function returns a value.
Methods included from Util
absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::SymbolicFileMode
#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Class Method Details
.arity(name) ⇒ Integer
Return the number of arguments a function expects.
218 219 220 221 |
# File 'lib/puppet/parser/functions.rb', line 218 def self.arity(name) func = get_function(name) func ? func[:arity] : -1 end |
.autoloader ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Accessor for singleton autoloader
34 35 36 37 38 |
# File 'lib/puppet/parser/functions.rb', line 34 def self.autoloader @autoloader ||= Puppet::Util::Autoload.new( self, "puppet/parser/functions", :wrap => false ) end |
.environment_module(env = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the module that functions are mixed into corresponding to an environment
44 45 46 47 48 49 |
# File 'lib/puppet/parser/functions.rb', line 44 def self.environment_module(env = nil) if env and ! env.is_a?(Puppet::Node::Environment) env = Puppet::Node::Environment.new(env) end @modules[ (env || Environment.current || Environment.root).name ] ||= Module.new end |
.function(name) ⇒ Symbol, false
Determine if a function is defined
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/puppet/parser/functions.rb', line 166 def self.function(name) name = name.intern func = nil unless func = get_function(name) autoloader.load(name, Environment.current) func = get_function(name) end if func func[:name] else false end end |
.functiondocs ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/puppet/parser/functions.rb', line 182 def self.functiondocs autoloader.loadall ret = "" merged_functions.sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, hash| ret << "#{name}\n#{"-" * name.to_s.length}\n" if hash[:doc] ret << Puppet::Util::Docs.scrub(hash[:doc]) else ret << "Undocumented.\n" end ret << "\n\n- *Type*: #{hash[:type]}\n\n" end ret end |
.newfunction(name, options = {}, &block) ⇒ Hash
Create a new Puppet DSL function.
**The newfunction method provides a public API.**
This method is used both internally inside of Puppet to define parser functions. For example, template() is defined in template.rb using the newfunction method. Third party Puppet modules such as [stdlib](forge.puppetlabs.com/puppetlabs/stdlib) use this method to extend the behavior and functionality of Puppet.
See also [Docs: Custom Functions](docs.puppetlabs.com/guides/custom_functions.html)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/puppet/parser/functions.rb', line 118 def self.newfunction(name, = {}, &block) name = name.intern Puppet.warning "Overwriting previous definition for function #{name}" if get_function(name) arity = [:arity] || -1 ftype = [:type] || :statement unless ftype == :statement or ftype == :rvalue raise Puppet::DevError, "Invalid statement type #{ftype.inspect}" end # the block must be installed as a method because it may use "return", # which is not allowed from procs. real_fname = "real_function_#{name}" environment_module.send(:define_method, real_fname, &block) fname = "function_#{name}" environment_module.send(:define_method, fname) do |*args| Puppet::Util::Profiler.profile("Called #{name}") do if args[0].is_a? Array if arity >= 0 and args[0].size != arity raise ArgumentError, "#{name}(): Wrong number of arguments given (#{args[0].size} for #{arity})" elsif arity < 0 and args[0].size < (arity+1).abs raise ArgumentError, "#{name}(): Wrong number of arguments given (#{args[0].size} for minimum #{(arity+1).abs})" end self.send(real_fname, args[0]) else raise ArgumentError, "custom functions must be called with a single array that contains the arguments. For example, function_example([1]) instead of function_example(1)" end end end func = {:arity => arity, :type => ftype, :name => fname} func[:doc] = [:doc] if [:doc] add_function(name, func) func end |
.reset ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset the list of loaded functions.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/puppet/parser/functions.rb', line 19 def self.reset @functions = Hash.new { |h,k| h[k] = {} } @modules = Hash.new # Runs a newfunction to create a function for each of the log levels Puppet::Util::Log.levels.each do |level| newfunction(level, :doc => "Log a message on the server at level #{level.to_s}.") do |vals| send(level, vals.join(" ")) end end end |
.rvalue?(name) ⇒ Boolean
Determine whether a given function returns a value.
206 207 208 209 |
# File 'lib/puppet/parser/functions.rb', line 206 def self.rvalue?(name) func = get_function(name) func ? func[:type] == :rvalue : false end |