class Puppet::Settings::BaseSetting
The base setting type
Constants
- HOOK_TYPES
Hooks are called during different parts of the settings lifecycle:
-
:on_write_only - This is the default hook type. The hook will be called if its value is set in `main` or programmatically. If its value is set in a section that doesn't match the application's run mode, it will be ignored entirely. If the section does match the run mode, the value will be used, but the hook will not be called!
-
:on_define_and_write - The hook behaves the same as above, except it is also called immediately when the setting is defined in {Puppet::Settings.define_settings}. In that case, the hook receives the default value as specified.
-
:on_initialize_and_write - The hook will be called if the value is set in `main`, the section that matches the run mode, or programmatically.
-
Attributes
Public Class Methods
# File lib/puppet/settings/base_setting.rb 27 def self.available_call_hook_values 28 HOOK_TYPES.to_a 29 end
Create the new element. Pretty much just sets the name.
# File lib/puppet/settings/base_setting.rb 85 def initialize(args = {}) 86 @settings = args.delete(:settings) 87 unless @settings 88 raise ArgumentError.new("You must refer to a settings object") 89 end 90 91 # explicitly set name prior to calling other param= methods to provide meaningful feedback during 92 # other warnings 93 @name = args[:name] if args.include? :name 94 95 #set the default value for call_hook 96 @call_hook = :on_write_only if args[:hook] and not args[:call_hook] 97 @has_hook = false 98 99 if args[:call_hook] and not args[:hook] 100 #TRANSLATORS ':call_hook' and ':hook' are specific setting names and should not be translated 101 raise ArgumentError, _("Cannot reference :call_hook for :%{name} if no :hook is defined") % { name: @name } 102 end 103 104 args.each do |param, value| 105 method = param.to_s + "=" 106 unless self.respond_to? method 107 raise ArgumentError, _("%{class_name} (setting '%{setting}') does not accept %{parameter}") % 108 { class_name: self.class, setting: args[:name], parameter: param } 109 end 110 111 self.send(method, value) 112 end 113 unless self.desc 114 raise ArgumentError, _("You must provide a description for the %{class_name} config option") % { class_name: self.name } 115 end 116 end
Public Instance Methods
True if we should raise a deprecation_warning if the setting is found in puppet.conf, but not if the user sets it on the commandline
# File lib/puppet/settings/base_setting.rb 215 def allowed_on_commandline? 216 @deprecated == :allowed_on_commandline 217 end
Registers a hook to be called later based on the type of hook specified in `value`.
@param value [Symbol] One of {HOOK_TYPES}
# File lib/puppet/settings/base_setting.rb 34 def call_hook=(value) 35 if value.nil? 36 #TRANSLATORS ':%{name}', ':call_hook', and ':on_write_only' should not be translated 37 Puppet.warning _("Setting :%{name} :call_hook is nil, defaulting to :on_write_only") % { name: name } 38 value = :on_write_only 39 end 40 unless HOOK_TYPES.include?(value) 41 #TRANSLATORS 'call_hook' is a Puppet option name and should not be translated 42 raise ArgumentError, _("Invalid option %{value} for call_hook") % { value: value } 43 end 44 @call_hook = value 45 end
@see {HOOK_TYPES}
# File lib/puppet/settings/base_setting.rb 48 def call_hook_on_define? 49 call_hook == :on_define_and_write 50 end
@see {HOOK_TYPES}
# File lib/puppet/settings/base_setting.rb 53 def call_hook_on_initialize? 54 call_hook == :on_initialize_and_write 55 end
True if we should raise a deprecation_warning if the setting is submitted on the commandline or is set in puppet.conf.
# File lib/puppet/settings/base_setting.rb 209 def completely_deprecated? 210 @deprecated == :completely 211 end
# File lib/puppet/settings/base_setting.rb 194 def deprecated=(deprecation) 195 unless [:completely, :allowed_on_commandline].include?(deprecation) 196 #TRANSLATORS 'deprecated' is a Puppet setting and ':completely' and ':allowed_on_commandline' are possible values and should not be translated 197 raise ArgumentError, _("Unsupported deprecated value '%{deprecation}'.") % { deprecation: deprecation } + 198 ' ' + _("Supported values for deprecated are ':completely' or ':allowed_on_commandline'") 199 end 200 @deprecated = deprecation 201 end
# File lib/puppet/settings/base_setting.rb 203 def deprecated? 204 !!@deprecated 205 end
get the arguments in getopt format
# File lib/puppet/settings/base_setting.rb 58 def getopt_args 59 if short 60 [["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]] 61 else 62 [["--#{name}", GetoptLong::REQUIRED_ARGUMENT]] 63 end 64 end
# File lib/puppet/settings/base_setting.rb 80 def has_hook? 81 @has_hook 82 end
# File lib/puppet/settings/base_setting.rb 75 def hook=(block) 76 @has_hook = true 77 meta_def :handle, &block 78 end
# File lib/puppet/settings/base_setting.rb 219 def inspect 220 %Q{<#{self.class}:#{self.object_id} @name="#{@name}" @section="#{@section}" @default="#{@default}" @call_hook="#{@call_hook}">} 221 end
# File lib/puppet/settings/base_setting.rb 118 def iscreated 119 @iscreated = true 120 end
# File lib/puppet/settings/base_setting.rb 122 def iscreated? 123 @iscreated 124 end
Modify the value when it is first evaluated
# File lib/puppet/settings/base_setting.rb 181 def munge(value) 182 value 183 end
get the arguments in OptionParser format
# File lib/puppet/settings/base_setting.rb 67 def optparse_args 68 if short 69 ["--#{name}", "-#{short}", desc, :REQUIRED] 70 else 71 ["--#{name}", desc, :REQUIRED] 72 end 73 end
Print the value for the user in a config compatible format
# File lib/puppet/settings/base_setting.rb 186 def print(value) 187 munge(value) 188 end
# File lib/puppet/settings/base_setting.rb 190 def set_meta(meta) 191 Puppet.notice("#{name} does not support meta data. Ignoring.") 192 end
short name for the element
# File lib/puppet/settings/base_setting.rb 127 def short=(value) 128 raise ArgumentError, _("Short names can only be one character.") if value.to_s.length != 1 129 @short = value.to_s 130 end
Convert the object to a config statement.
# File lib/puppet/settings/base_setting.rb 147 def to_config 148 require_relative '../../puppet/util/docs' 149 # Scrub any funky indentation; comment out description. 150 str = Puppet::Util::Docs.scrub(@desc).gsub(/^/, "# ") + "\n" 151 152 # Add in a statement about the default. 153 str << "# The default value is '#{default(true)}'.\n" if default(true) 154 155 # If the value has not been overridden, then print it out commented 156 # and unconverted, so it's clear that that's the default and how it 157 # works. 158 value = @settings.value(self.name) 159 160 if value != @default 161 line = "#{@name} = #{value}" 162 else 163 line = "# #{@name} = #{@default}" 164 end 165 166 str << (line + "\n") 167 168 # Indent 169 str.gsub(/^/, " ") 170 end
@param bypass_interpolation [Boolean] Set this true to skip the
interpolation step, returning the raw setting value. Defaults to false.
@return [String] Retrieves the value, or if it's not set, retrieves the default. @api public
# File lib/puppet/settings/base_setting.rb 176 def value(bypass_interpolation = false) 177 @settings.value(self.name, nil, bypass_interpolation) 178 end