Module: Puppet::Parser::Functions

Extended by:
MonitorMixin, Util
Defined in:
lib/puppet/parser/functions.rb,
lib/puppet/parser/functions/split.rb,
lib/puppet/parser/functions/sprintf.rb,
lib/puppet/parser/functions/regsubst.rb,
lib/puppet/parser/functions/extlookup.rb,
lib/puppet/parser/functions/shellquote.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
Safe =

Safe unquoted

'a-zA-Z0-9@%_+=:,./-'
Dangerous =

Unsafe inside double quotes

'!"`$\\'

Class Method Summary collapse

Methods included from Util

activerecord_version, benchmark, chuser, classproxy, execfail, execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Class Method Details

.autoloaderObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/parser/functions.rb', line 17

def self.autoloader
  unless defined?(@autoloader)

          @autoloader = Puppet::Util::Autoload.new(
      self,
      "puppet/parser/functions",

      :wrap => false
    )
  end

  @autoloader
end

.environment_module(env = nil) ⇒ Object



33
34
35
36
37
# File 'lib/puppet/parser/functions.rb', line 33

def self.environment_module(env = nil)
  @modules.synchronize {
    @modules[ env || Environment.current || Environment.root ] ||= Module.new
  }
end

.function(name) ⇒ Object

Determine if a given name is a function



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/parser/functions.rb', line 73

def self.function(name)
  name = symbolize(name)

  @functions.synchronize do
    unless functions.include?(name) or functions(Puppet::Node::Environment.root).include?(name)
      autoloader.load(name,Environment.current || Environment.root)
    end
  end

  ( functions(Environment.root)[name] || functions[name] || {:name => false} )[:name]
end

.functiondocsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puppet/parser/functions.rb', line 85

def self.functiondocs
  autoloader.loadall

  ret = ""

  functions.sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, hash|
    #ret += "#{name}\n#{hash[:type]}\n"
    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

.functions(env = nil) ⇒ Object



105
106
107
108
109
# File 'lib/puppet/parser/functions.rb', line 105

def self.functions(env = nil)
  @functions.synchronize {
    @functions[ env || Environment.current || Environment.root ]
  }
end

.newfunction(name, options = {}, &block) ⇒ Object

Create a new function type.

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/parser/functions.rb', line 40

def self.newfunction(name, options = {}, &block)
  name = symbolize(name)

  raise Puppet::DevError, "Function #{name} already defined" if functions.include?(name)

  ftype = options[:type] || :statement

  unless ftype == :statement or ftype == :rvalue
    raise Puppet::DevError, "Invalid statement type #{ftype.inspect}"
  end

  fname = "function_#{name}"
  environment_module.send(:define_method, fname, &block)

  # Someday we'll support specifying an arity, but for now, nope
  #functions[name] = {:arity => arity, :type => ftype}
  functions[name] = {:type => ftype, :name => fname}
  functions[name][:doc] = options[:doc] if options[:doc]
end

.rmfunction(name) ⇒ Object

Remove a function added by newfunction

Raises:



61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet/parser/functions.rb', line 61

def self.rmfunction(name)
  name = symbolize(name)

  raise Puppet::DevError, "Function #{name} is not defined" unless functions.include? name

  functions.delete name

  fname = "function_#{name}"
  environment_module.send(:remove_method, fname)
end

.rvalue?(name) ⇒ Boolean

Determine if a given function returns a value or not.

Returns:

  • (Boolean)


112
113
114
# File 'lib/puppet/parser/functions.rb', line 112

def self.rvalue?(name)
  (functions[symbolize(name)] || {})[:type] == :rvalue
end