Class: Puppet::Settings::DurationSetting

Inherits:
BaseSetting show all
Defined in:
lib/puppet/settings/duration_setting.rb

Overview

A setting that represents a span of time, and evaluates to an integer number of seconds after being parsed

Constant Summary collapse

UNITMAP =

How we convert from various units to seconds.

{
  # 365 days isn't technically a year, but is sufficient for most purposes
  "y" => 365 * 24 * 60 * 60,
  "d" => 24 * 60 * 60,
  "h" => 60 * 60,
  "m" => 60,
  "s" => 1
}
FORMAT =

A regex describing valid formats with groups for capturing the value and units

/^(\d+)(y|d|h|m|s)?$/

Constants inherited from BaseSetting

BaseSetting::HOOK_TYPES

Instance Attribute Summary

Attributes inherited from BaseSetting

#call_hook, #default, #deprecated, #desc, #name, #section, #short

Instance Method Summary collapse

Methods inherited from BaseSetting

#allowed_on_commandline?, available_call_hook_values, #call_hook_on_define?, #call_hook_on_initialize?, #completely_deprecated?, #deprecated?, #getopt_args, #has_hook?, #hook=, #initialize, #inspect, #iscreated, #iscreated?, #optparse_args, #print, #set_meta, #to_config, #value

Constructor Details

This class inherits a constructor from Puppet::Settings::BaseSetting

Instance Method Details

#munge(value) ⇒ Object

Convert the value to an integer, parsing numeric string with units if necessary.



24
25
26
27
28
29
30
31
32
# File 'lib/puppet/settings/duration_setting.rb', line 24

def munge(value)
  if value.is_a?(Integer) || value.nil?
    value
  elsif value.is_a?(String) and value =~ FORMAT
    ::Regexp.last_match(1).to_i * UNITMAP[::Regexp.last_match(2) || 's']
  else
    raise Puppet::Settings::ValidationError, _("Invalid duration format '%{value}' for parameter: %{name}") % { value: value.inspect, name: @name }
  end
end

#typeObject



19
20
21
# File 'lib/puppet/settings/duration_setting.rb', line 19

def type
  :duration
end