Class: Puppetserver::Settings::TTLSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetserver/settings/ttl_setting.rb

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 collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, setting_value) ⇒ TTLSetting

Returns a new instance of TTLSetting.



23
24
25
26
# File 'lib/puppetserver/settings/ttl_setting.rb', line 23

def initialize(name, setting_value)
  @errors = []
  @munged_value = munge(setting_value, name)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#munged_valueObject (readonly)

Returns the value of attribute munged_value.



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

def munged_value
  @munged_value
end

Instance Method Details

#munge(value, name) ⇒ Object

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppetserver/settings/ttl_setting.rb', line 29

def munge(value, name)
  case
  when value.is_a?(Numeric)
    if value < 0
      @errors << "Invalid negative 'time to live' #{value.inspect} - did you mean 'unlimited'?"
    end
    value

  when value == 'unlimited'
    Float::INFINITY

  when (value.is_a?(String) and value =~ FORMAT)
    $1.to_i * UNITMAP[$2 || 's']
  else
    @errors <<  "Invalid 'time to live' format '#{value.inspect}' for parameter: #{name}"
  end
end