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 29 def self.available_call_hook_values 30 HOOK_TYPES.to_a 31 end
Create the new element. Pretty much just sets the name.
# File lib/puppet/settings/base_setting.rb 87 def initialize(args = {}) 88 @settings = args.delete(:settings) 89 unless @settings 90 raise ArgumentError.new("You must refer to a settings object") 91 end 92 93 # explicitly set name prior to calling other param= methods to provide meaningful feedback during 94 # other warnings 95 @name = args[:name] if args.include? :name 96 97 #set the default value for call_hook 98 @call_hook = :on_write_only if args[:hook] and not args[:call_hook] 99 @has_hook = false 100 101 if args[:call_hook] and not args[:hook] 102 #TRANSLATORS ':call_hook' and ':hook' are specific setting names and should not be translated 103 raise ArgumentError, _("Cannot reference :call_hook for :%{name} if no :hook is defined") % { name: @name } 104 end 105 106 args.each do |param, value| 107 method = param.to_s + "=" 108 unless self.respond_to? method 109 raise ArgumentError, _("%{class_name} (setting '%{setting}') does not accept %{parameter}") % 110 { class_name: self.class, setting: args[:name], parameter: param } 111 end 112 113 self.send(method, value) 114 end 115 unless self.desc 116 raise ArgumentError, _("You must provide a description for the %{class_name} config option") % { class_name: self.name } 117 end 118 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 217 def allowed_on_commandline? 218 @deprecated == :allowed_on_commandline 219 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 36 def call_hook=(value) 37 if value.nil? 38 #TRANSLATORS ':%{name}', ':call_hook', and ':on_write_only' should not be translated 39 Puppet.warning _("Setting :%{name} :call_hook is nil, defaulting to :on_write_only") % { name: name } 40 value = :on_write_only 41 end 42 unless HOOK_TYPES.include?(value) 43 #TRANSLATORS 'call_hook' is a Puppet option name and should not be translated 44 raise ArgumentError, _("Invalid option %{value} for call_hook") % { value: value } 45 end 46 @call_hook = value 47 end
@see {HOOK_TYPES}
# File lib/puppet/settings/base_setting.rb 50 def call_hook_on_define? 51 call_hook == :on_define_and_write 52 end
@see {HOOK_TYPES}
# File lib/puppet/settings/base_setting.rb 55 def call_hook_on_initialize? 56 call_hook == :on_initialize_and_write 57 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 211 def completely_deprecated? 212 @deprecated == :completely 213 end
# File lib/puppet/settings/base_setting.rb 134 def default(check_application_defaults_first = false) 135 if @default.is_a? Proc 136 # Give unit tests a chance to reevaluate the call by removing the instance variable 137 unless instance_variable_defined?(:@evaluated_default) 138 @evaluated_default = @default.call 139 end 140 default_value = @evaluated_default 141 else 142 default_value = @default 143 end 144 return default_value unless check_application_defaults_first 145 return @settings.value(name, :application_defaults, true) || default_value 146 end
# File lib/puppet/settings/base_setting.rb 196 def deprecated=(deprecation) 197 unless [:completely, :allowed_on_commandline].include?(deprecation) 198 #TRANSLATORS 'deprecated' is a Puppet setting and ':completely' and ':allowed_on_commandline' are possible values and should not be translated 199 raise ArgumentError, _("Unsupported deprecated value '%{deprecation}'.") % { deprecation: deprecation } + 200 ' ' + _("Supported values for deprecated are ':completely' or ':allowed_on_commandline'") 201 end 202 @deprecated = deprecation 203 end
# File lib/puppet/settings/base_setting.rb 205 def deprecated? 206 !!@deprecated 207 end
get the arguments in getopt format
# File lib/puppet/settings/base_setting.rb 60 def getopt_args 61 if short 62 [["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]] 63 else 64 [["--#{name}", GetoptLong::REQUIRED_ARGUMENT]] 65 end 66 end
# File lib/puppet/settings/base_setting.rb 82 def has_hook? 83 @has_hook 84 end
# File lib/puppet/settings/base_setting.rb 77 def hook=(block) 78 @has_hook = true 79 meta_def :handle, &block 80 end
# File lib/puppet/settings/base_setting.rb 221 def inspect 222 %Q{<#{self.class}:#{self.object_id} @name="#{@name}" @section="#{@section}" @default="#{@default}" @call_hook="#{@call_hook}">} 223 end
# File lib/puppet/settings/base_setting.rb 120 def iscreated 121 @iscreated = true 122 end
# File lib/puppet/settings/base_setting.rb 124 def iscreated? 125 @iscreated 126 end
Modify the value when it is first evaluated
# File lib/puppet/settings/base_setting.rb 183 def munge(value) 184 value 185 end
get the arguments in OptionParser format
# File lib/puppet/settings/base_setting.rb 69 def optparse_args 70 if short 71 ["--#{name}", "-#{short}", desc, :REQUIRED] 72 else 73 ["--#{name}", desc, :REQUIRED] 74 end 75 end
Print the value for the user in a config compatible format
# File lib/puppet/settings/base_setting.rb 188 def print(value) 189 munge(value) 190 end
# File lib/puppet/settings/base_setting.rb 192 def set_meta(meta) 193 Puppet.notice("#{name} does not support meta data. Ignoring.") 194 end
short name for the element
# File lib/puppet/settings/base_setting.rb 129 def short=(value) 130 raise ArgumentError, _("Short names can only be one character.") if value.to_s.length != 1 131 @short = value.to_s 132 end
Convert the object to a config statement.
# File lib/puppet/settings/base_setting.rb 149 def to_config 150 require_relative '../../puppet/util/docs' 151 # Scrub any funky indentation; comment out description. 152 str = Puppet::Util::Docs.scrub(@desc).gsub(/^/, "# ") + "\n" 153 154 # Add in a statement about the default. 155 str << "# The default value is '#{default(true)}'.\n" if default(true) 156 157 # If the value has not been overridden, then print it out commented 158 # and unconverted, so it's clear that that's the default and how it 159 # works. 160 value = @settings.value(self.name) 161 162 if value != @default 163 line = "#{@name} = #{value}" 164 else 165 line = "# #{@name} = #{@default}" 166 end 167 168 str << (line + "\n") 169 170 # Indent 171 str.gsub(/^/, " ") 172 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 178 def value(bypass_interpolation = false) 179 @settings.value(self.name, nil, bypass_interpolation) 180 end