Sha256: 6058cff9318fde8121e9ed2b878c89926f415d7d9641ec0561fb16425730e318

Contents?: true

Size: 1006 Bytes

Versions: 23

Compression:

Stored size: 1006 Bytes

Contents

require 'puppet/settings/base_setting'

# A setting that represents a span of time, and evaluates to an integer
# number of seconds after being parsed
class Puppet::Settings::DurationSetting < Puppet::Settings::BaseSetting
  # 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
    :duration
  end

  # Convert the value to an integer, parsing numeric string with units if necessary.
  def munge(value)
    case
    when value.is_a?(Integer)
      value
    when (value.is_a?(String) and value =~ FORMAT)
      $1.to_i * UNITMAP[$2 || 's']
    else
      raise Puppet::Settings::ValidationError, "Invalid duration format '#{value.inspect}' for parameter: #{@name}"
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
puppet-3.2.4 lib/puppet/settings/duration_setting.rb
puppet-3.2.3 lib/puppet/settings/duration_setting.rb
puppet-3.2.3.rc1 lib/puppet/settings/duration_setting.rb
puppet-3.2.2 lib/puppet/settings/duration_setting.rb
puppet-3.2.1 lib/puppet/settings/duration_setting.rb
puppet-3.2.1.rc1 lib/puppet/settings/duration_setting.rb
puppet-3.2.0.rc2 lib/puppet/settings/duration_setting.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/puppet-3.1.0/lib/puppet/settings/duration_setting.rb
puppet-3.2.0.rc1 lib/puppet/settings/duration_setting.rb
puppet-3.1.1 lib/puppet/settings/duration_setting.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/puppet-3.1.0/lib/puppet/settings/duration_setting.rb
puppet-3.1.0 lib/puppet/settings/duration_setting.rb
puppet-3.1.0.rc2 lib/puppet/settings/duration_setting.rb
puppet-3.1.0.rc1 lib/puppet/settings/duration_setting.rb
puppet-3.0.2 lib/puppet/settings/duration_setting.rb
puppet-3.0.2.rc3 lib/puppet/settings/duration_setting.rb
puppet-3.0.2.rc2 lib/puppet/settings/duration_setting.rb
puppet-3.0.2.rc1 lib/puppet/settings/duration_setting.rb
puppet-3.0.1 lib/puppet/settings/duration_setting.rb
puppet-3.0.1.rc1 lib/puppet/settings/duration_setting.rb