Module: Puppet::Parser::Functions

Extended by:
Util
Defined in:
lib/vendor/puppet/parser/functions.rb,
lib/vendor/puppet/parser/functions/split.rb,
lib/vendor/puppet/parser/functions/sprintf.rb,
lib/vendor/puppet/parser/functions/regsubst.rb,
lib/vendor/puppet/parser/functions/extlookup.rb

Overview

Copyright © 2009 Thomas Bellman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THOMAS BELLMAN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Thomas Bellman shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Thomas Bellman.

Constant Summary collapse

Environment =
Puppet::Node::Environment

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Class Method Summary collapse

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, execfail, execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

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

Class Method Details

.autoloaderObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vendor/puppet/parser/functions.rb', line 28

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



40
41
42
43
44
# File 'lib/vendor/puppet/parser/functions.rb', line 40

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vendor/puppet/parser/functions.rb', line 91

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

  func = nil
  @functions.synchronize do
    unless func = get_function(name)
      autoloader.load(name, Environment.current)
      func = get_function(name)
    end
  end

  if func
    func[:name]
  else
    false
  end
end

.functiondocsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vendor/puppet/parser/functions.rb', line 109

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

.functions(env = nil) ⇒ Object



128
129
130
131
132
133
# File 'lib/vendor/puppet/parser/functions.rb', line 128

def self.functions(env = nil)
  Puppet.deprecation_warning "Puppet::Parser::Functions.functions is deprecated and will be removed in 3.0"
  @functions.synchronize {
    @functions[ env || Environment.current || Environment.root ]
  }
end

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

Create a new function type.

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vendor/puppet/parser/functions.rb', line 47

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

  raise Puppet::DevError, "Function #{name} already defined" if get_function(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}
  func = {:type => ftype, :name => fname}
  func[:doc] = options[:doc] if options[:doc]

  add_function(name, func)
  func
end

.resetObject

This is used by tests



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

def self.reset
  @functions = Hash.new { |h,k| h[k] = {} }.extend(MonitorMixin)
  @modules = Hash.new.extend(MonitorMixin)

  # 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

.rmfunction(name) ⇒ Object

Remove a function added by newfunction

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vendor/puppet/parser/functions.rb', line 71

def self.rmfunction(name)
  Puppet.deprecation_warning "Puppet::Parser::Functions.rmfunction is deprecated and will be removed in 3.0"
  name = name.intern

  raise Puppet::DevError, "Function #{name} is not defined" unless get_function(name)

  @functions.synchronize {
    @functions[Environment.current].delete(name)
    # This seems wrong because it won't delete a function defined on root if
    # the current environment is different
    #@functions[Environment.root].delete(name)
  }

  fname = "function_#{name}"
  # This also only deletes from the module associated with
  # Environment.current
  environment_module.send(:remove_method, fname)
end

.rvalue?(name) ⇒ Boolean

Determine if a given function returns a value or not.

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/vendor/puppet/parser/functions.rb', line 136

def self.rvalue?(name)
  func = get_function(name)
  func ? func[:type] == :rvalue : false
end