Class: Puppet::Parameter

Inherits:
Object show all
Extended by:
Util, Util::Docs
Includes:
Util, Util::Errors, Util::LogPaths, Util::Logging, Util::MethodHelper
Defined in:
lib/vendor/puppet/parameter.rb

Direct Known Subclasses

Path, Property, Type::RelationshipMetaparam

Defined Under Namespace

Classes: Path, Value, ValueCollection

Constant Summary

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Docs

desc, dochook, doctable, markdown_definitionlist, markdown_header, nodoc?, pad, scrub

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

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Logging

#clear_deprecation_warnings, #deprecation_warning, #send_log

Methods included from Util::LogPaths

#path, #source_descriptors

Methods included from Util::Errors

#adderrorcontext, #error_context, #exceptwrap

Constructor Details

#initialize(options = {}) ⇒ Parameter

Basic parameter initialization.



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/vendor/puppet/parameter.rb', line 169

def initialize(options = {})
  options = symbolize_options(options)
  if resource = options[:resource]
    self.resource = resource
    options.delete(:resource)
  else
    raise Puppet::DevError, "No resource set for #{self.class.name}"
  end

  set_options(options)
end

Class Attribute Details

.defaultObject (readonly)

Returns the value of attribute default.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def default
  @default
end

.metaparamObject

Returns the value of attribute metaparam.



19
20
21
# File 'lib/vendor/puppet/parameter.rb', line 19

def metaparam
  @metaparam
end

.mungerObject (readonly)

Returns the value of attribute munger.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def munger
  @munger
end

.nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def name
  @name
end

.required_featuresObject

Returns the value of attribute required_features.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def required_features
  @required_features
end

.validaterObject (readonly)

Returns the value of attribute validater.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def validater
  @validater
end

.value_collectionObject (readonly)

Returns the value of attribute value_collection.



18
19
20
# File 'lib/vendor/puppet/parameter.rb', line 18

def value_collection
  @value_collection
end

Instance Attribute Details

#parentObject

LAK 2007-05-09: Keep the @parent around for backward compatibility.



139
140
141
# File 'lib/vendor/puppet/parameter.rb', line 139

def parent
  @parent
end

#resourceObject

Returns the value of attribute resource.



137
138
139
# File 'lib/vendor/puppet/parameter.rb', line 137

def resource
  @resource
end

Class Method Details

.aliasvalue(name, other) ⇒ Object



119
120
121
# File 'lib/vendor/puppet/parameter.rb', line 119

def aliasvalue(name, other)
  @value_collection.aliasvalue(name, other)
end

.defaultto(value = nil, &block) ⇒ Object

Define the default value for a given parameter or parameter. This means that ‘nil’ is an invalid default value. This defines the ‘default’ instance method.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vendor/puppet/parameter.rb', line 24

def defaultto(value = nil, &block)
  if block
    define_method(:default, &block)
  else
    if value.nil?
      raise Puppet::DevError,
        "Either a default value or block must be provided"
    end
    define_method(:default) do value end
  end
end

.desc(str) ⇒ Object

Store documentation for this parameter.



58
59
60
# File 'lib/vendor/puppet/parameter.rb', line 58

def desc(str)
  @doc = str
end

.docObject

Return a documentation string. If there are valid values, then tack them onto the string.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vendor/puppet/parameter.rb', line 38

def doc
  @doc ||= ""

  unless defined?(@addeddocvals)
    @doc += value_collection.doc

    if f = self.required_features
      @doc += "  Requires features #{f.flatten.collect { |f| f.to_s }.join(" ")}."
    end
    @addeddocvals = true
  end

  @doc
end

.format_value_for_display(value) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/vendor/puppet/parameter.rb', line 297

def self.format_value_for_display(value)
  if value.is_a? Array
    formatted_values = value.collect {|value| format_value_for_display(value)}.join(', ')
    "[#{formatted_values}]"
  elsif value.is_a? Hash
    # Sorting the hash keys for display is largely for having stable
    # output to test against, but also helps when scanning for hash
    # keys, since they will be in ASCIIbetical order.
    hash = value.keys.sort {|a,b| a.to_s <=> b.to_s}.collect do |k|
      "'#{k}' => #{format_value_for_display(value[k])}"
    end.join(', ')

    "{#{hash}}"
  else
    "'#{value}'"
  end
end

.initvarsObject



62
63
64
# File 'lib/vendor/puppet/parameter.rb', line 62

def initvars
  @value_collection = ValueCollection.new
end

.isnamevarObject

Mark whether we’re the namevar.



84
85
86
87
# File 'lib/vendor/puppet/parameter.rb', line 84

def isnamevar
  @isnamevar = true
  @required = true
end

.isnamevar?Boolean

Is this parameter the namevar? Defaults to false.

Returns:

  • (Boolean)


90
91
92
# File 'lib/vendor/puppet/parameter.rb', line 90

def isnamevar?
  @isnamevar
end

.isrequiredObject

This parameter is required.



95
96
97
# File 'lib/vendor/puppet/parameter.rb', line 95

def isrequired
  @required = true
end

.munge(&block) ⇒ Object

This is how we munge the value. Basically, this is our opportunity to convert the value from one form into another.



68
69
70
71
72
73
74
# File 'lib/vendor/puppet/parameter.rb', line 68

def munge(&block)
  # I need to wrap the unsafe version in begin/rescue parameterments,
  # but if I directly call the block then it gets bound to the
  # class's context, not the instance's, thus the two methods,
  # instead of just one.
  define_method(:unsafe_munge, &block)
end

.newvalues(*names) ⇒ Object

Define a new value for our parameter.



115
116
117
# File 'lib/vendor/puppet/parameter.rb', line 115

def newvalues(*names)
  @value_collection.newvalues(*names)
end

.nodefaultObject



53
54
55
# File 'lib/vendor/puppet/parameter.rb', line 53

def nodefault
  undef_method :default if public_method_defined? :default
end

.proxymethods(*values) ⇒ Object

Just a simple method to proxy instance methods to class methods



125
126
127
128
129
130
131
# File 'lib/vendor/puppet/parameter.rb', line 125

def self.proxymethods(*values)
  values.each { |val|
    define_method(val) do
      self.class.send(val)
    end
  }
end

.required?Boolean

Is this parameter required? Defaults to false.

Returns:

  • (Boolean)


105
106
107
# File 'lib/vendor/puppet/parameter.rb', line 105

def required?
  @required
end

.unmunge(&block) ⇒ Object

Does the parameter support reverse munging? This will be called when something wants to access the parameter in a canonical form different to what the storage form is.



79
80
81
# File 'lib/vendor/puppet/parameter.rb', line 79

def unmunge(&block)
  define_method(:unmunge, &block)
end

.validate(&block) ⇒ Object

Verify that we got a good value



110
111
112
# File 'lib/vendor/puppet/parameter.rb', line 110

def validate(&block)
  define_method(:unsafe_validate, &block)
end

Instance Method Details

#devfail(msg) ⇒ Object



147
148
149
# File 'lib/vendor/puppet/parameter.rb', line 147

def devfail(msg)
  self.fail(Puppet::DevError, msg)
end

#fail(*args) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vendor/puppet/parameter.rb', line 151

def fail(*args)
  type = nil
  if args[0].is_a?(Class)
    type = args.shift
  else
    type = Puppet::Error
  end

  error = type.new(args.join(" "))

  error.line = @resource.line if @resource and @resource.line

  error.file = @resource.file if @resource and @resource.file

  raise error
end

#log(msg) ⇒ Object



181
182
183
# File 'lib/vendor/puppet/parameter.rb', line 181

def log(msg)
  send_log(resource[:loglevel], msg)
end

#metaparam?Boolean

Is this parameter a metaparam?

Returns:

  • (Boolean)


186
187
188
# File 'lib/vendor/puppet/parameter.rb', line 186

def metaparam?
  self.class.metaparam
end

#munge(value) ⇒ Object

A wrapper around our munging that makes sure we raise useful exceptions.



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/vendor/puppet/parameter.rb', line 228

def munge(value)
  begin
    ret = unsafe_munge(value)
  rescue Puppet::Error => detail
    Puppet.debug "Reraising #{detail}"
    raise
  rescue => detail
    raise Puppet::DevError, "Munging failed for value #{value.inspect} in class #{self.name}: #{detail}", detail.backtrace
  end
  ret
end

#nameObject

each parameter class must define the name method, and parameter instances do not change that name this implicitly means that a given object can only have one parameter instance of a given parameter class



194
195
196
# File 'lib/vendor/puppet/parameter.rb', line 194

def name
  self.class.name
end

#noopObject

for testing whether we should actually do anything



199
200
201
202
203
204
# File 'lib/vendor/puppet/parameter.rb', line 199

def noop
  @noop ||= false
  tmp = @noop || self.resource.noop || Puppet[:noop] || false
  #debug "noop is #{tmp}"
  tmp
end

#pathbuilderObject

return the full path to us, for logging and rollback; not currently used



208
209
210
211
212
213
214
# File 'lib/vendor/puppet/parameter.rb', line 208

def pathbuilder
  if @resource
    return [@resource.pathbuilder, self.name]
  else
    return [self.name]
  end
end

#providerObject

Retrieve the resource’s provider. Some types don’t have providers, in which case we return the resource object itself.



278
279
280
# File 'lib/vendor/puppet/parameter.rb', line 278

def provider
  @resource.provider
end

#removeObject



259
260
261
# File 'lib/vendor/puppet/parameter.rb', line 259

def remove
  @resource = nil
end

#tagsObject

The properties need to return tags so that logs correctly collect them.



283
284
285
286
287
288
289
290
291
# File 'lib/vendor/puppet/parameter.rb', line 283

def tags
  unless defined?(@tags)
    @tags = []
    # This might not be true in testing
    @tags = @resource.tags if @resource.respond_to? :tags
    @tags << self.name.to_s
  end
  @tags
end

#to_sObject



293
294
295
# File 'lib/vendor/puppet/parameter.rb', line 293

def to_s
  name.to_s
end

#unmunge(value) ⇒ Object

no unmunge by default



223
224
225
# File 'lib/vendor/puppet/parameter.rb', line 223

def unmunge(value)
  value
end

#unsafe_munge(value) ⇒ Object

If the specified value is allowed, then munge appropriately. If the developer uses a ‘munge’ hook, this method will get overridden.



218
219
220
# File 'lib/vendor/puppet/parameter.rb', line 218

def unsafe_munge(value)
  self.class.value_collection.munge(value)
end

#unsafe_validate(value) ⇒ Object

Verify that the passed value is valid. If the developer uses a ‘validate’ hook, this method will get overridden.



242
243
244
# File 'lib/vendor/puppet/parameter.rb', line 242

def unsafe_validate(value)
  self.class.value_collection.validate(value)
end

#validate(value) ⇒ Object

A protected validation method that only ever raises useful exceptions.



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vendor/puppet/parameter.rb', line 247

def validate(value)
  begin
    unsafe_validate(value)
  rescue ArgumentError => detail
    fail detail.to_s
  rescue Puppet::Error, TypeError
    raise
  rescue => detail
    raise Puppet::DevError, "Validate method failed for class #{self.name}: #{detail}", detail.backtrace
  end
end

#valueObject



263
264
265
# File 'lib/vendor/puppet/parameter.rb', line 263

def value
  unmunge(@value) unless @value.nil?
end

#value=(value) ⇒ Object

Store the value provided. All of the checking should possibly be late-binding (e.g., users might not exist when the value is assigned but might when it is asked for).



270
271
272
273
274
# File 'lib/vendor/puppet/parameter.rb', line 270

def value=(value)
  validate(value)

  @value = munge(value)
end