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)?$/

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.



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

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

#typeObject



17
18
19
# File 'lib/puppet/settings/duration_setting.rb', line 17

def type
  :duration
end