Class: Puppet::Settings::TTLSetting

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

Overview

A setting that represents a span of time to live, and evaluates to Numeric seconds to live where 0 means shortest possible time to live, a positive numeric value means time to live in seconds, and the symbolic entry ‘unlimited’ is an infinite amount of time.

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

Class Method Summary collapse

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, #set_meta, #to_config, #value

Constructor Details

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

Class Method Details

.munge(value, param_name) ⇒ Object

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/settings/ttl_setting.rb', line 36

def self.munge(value, param_name)
  if value.is_a?(Numeric)
    if value < 0
      raise Puppet::Settings::ValidationError, _("Invalid negative 'time to live' %{value} - did you mean 'unlimited'?") % { value: value.inspect }
    end

    value

  elsif value == 'unlimited'
    Float::INFINITY

  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 'time to live' format '%{value}' for parameter: %{param_name}") % { value: value.inspect, param_name: param_name }
  end
end

Instance Method Details

#munge(value) ⇒ Object

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



26
27
28
# File 'lib/puppet/settings/ttl_setting.rb', line 26

def munge(value)
  self.class.munge(value, @name)
end


30
31
32
33
# File 'lib/puppet/settings/ttl_setting.rb', line 30

def print(value)
  val = munge(value)
  val == Float::INFINITY ? 'unlimited' : val
end

#typeObject



21
22
23
# File 'lib/puppet/settings/ttl_setting.rb', line 21

def type
  :ttl
end