Sha256: eeda3122573904783486a828b7468c210ddcc22a69a807d50431f5762cb49b10

Contents?: true

Size: 1.47 KB

Versions: 58

Compression:

Stored size: 1.47 KB

Contents

# 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.
#
class Puppet::Settings::TTLSetting < Puppet::Settings::BaseSetting
  INFINITY = 1.0 / 0.0

  # How we convert from various units to seconds.
  UNITMAP = {
    # 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
  }

  # A regex describing valid formats with groups for capturing the value and units
  FORMAT = /^(\d+)(y|d|h|m|s)?$/

  def type
    :ttl
  end

  # Convert the value to Numeric, parsing numeric string with units if necessary.
  def munge(value)
    self.class.munge(value, @name)
  end

  # Convert the value to Numeric, parsing numeric string with units if necessary.
  def self.munge(value, param_name)
    case
    when value.is_a?(Numeric)
      if value < 0
        raise Puppet::Settings::ValidationError, "Invalid negative 'time to live' #{value.inspect} - did you mean 'unlimited'?"
      end
      value

    when value == 'unlimited'
      INFINITY

    when (value.is_a?(String) and value =~ FORMAT)
      $1.to_i * UNITMAP[$2 || 's']
    else
      raise Puppet::Settings::ValidationError, "Invalid 'time to live' format '#{value.inspect}' for parameter: #{param_name}"
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-3.8.7 lib/puppet/settings/ttl_setting.rb
puppet-3.8.7-x86-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-3.8.7-x64-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-3.8.6 lib/puppet/settings/ttl_setting.rb
puppet-3.8.6-x86-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-3.8.6-x64-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-3.8.5 lib/puppet/settings/ttl_setting.rb
puppet-3.8.5-x86-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-3.8.5-x64-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-3.8.4 lib/puppet/settings/ttl_setting.rb
puppet-3.8.4-x86-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-3.8.4-x64-mingw32 lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.9.1 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.9.0 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb
puppet-retrospec-0.8.1 vendor/gems/puppet-3.7.3/lib/puppet/settings/ttl_setting.rb