class Puppet::Interface::ActionBuilder
This class is used to build {Puppet::Interface::Action actions}. When an action is defined with {Puppet::Interface::ActionManager#action} the block is evaluated within the context of a new instance of this class. @api public
Attributes
The action under construction @return [Puppet::Interface::Action] @api private
Public Class Methods
Builds a new action. @return [Puppet::Interface::Action] @api private
# File lib/puppet/interface/action_builder.rb 15 def self.build(face, name, &block) 16 raise "Action #{name.inspect} must specify a block" unless block 17 new(face, name, &block).action 18 end
# File lib/puppet/interface/action_builder.rb 157 def initialize(face, name, &block) 158 @face = face 159 @action = Puppet::Interface::Action.new(face, name) 160 instance_eval(&block) 161 unless @action.when_invoked 162 #TRANSLATORS 'when_invoked' is a method name and should not be translated and 'block' is a Ruby code block 163 raise ArgumentError, _("actions need to know what to do when_invoked; please add the block") 164 end 165 end
Public Instance Methods
Set this as the default action for the face. @api public @dsl Faces @return [void]
# File lib/puppet/interface/action_builder.rb 111 def default(value = true) 112 @action.default = !!value 113 end
Deprecates the action @return [void] @api private @dsl Faces
# File lib/puppet/interface/action_builder.rb 24 def deprecate 25 @action.deprecate 26 end
@api private
# File lib/puppet/interface/action_builder.rb 116 def display_global_options(*args) 117 @action.add_display_global_options args 118 end
Declare that this action can take a specific option, and provide the code to do so. One or more strings are given, in the style of OptionParser (see example). These strings are parsed to derive a name for the option. Any `-` characters within the option name (ie excluding the initial `-` or `–` for an option) will be translated to `_`.The first long option will be used as the name, and the rest are retained as aliases. The original form of the option is used when invoking the face, the translated form is used internally.
When the action is invoked the value of the option is available in a hash passed to the {Puppet::Interface::ActionBuilder#when_invoked when_invoked
} block, using the option name in symbol form as the hash key.
The block to this method is used to set attributes for the option (see {Puppet::Interface::OptionBuilder}).
@param declaration [String] Option declarations, as described above
and in the example.
@example Say hi
action :say_hi do option "-u USER", "--user-name USER" do summary "Who to say hi to" end when_invoked do |options| "Hi, #{options[:user_name]}" end end
@api public @dsl Faces
# File lib/puppet/interface/action_builder.rb 102 def option(*declaration, &block) 103 option = Puppet::Interface::OptionBuilder.build(@action, *declaration, &block) 104 @action.add_option(option) 105 end
Sets the default rendering format @api private
# File lib/puppet/interface/action_builder.rb 123 def render_as(value = nil) 124 if value.nil? 125 #TRANSLATORS 'render_as' is a method name and should not be translated 126 raise ArgumentError, _("You must give a rendering format to render_as") 127 end 128 129 formats = Puppet::Network::FormatHandler.formats 130 unless formats.include? value 131 raise ArgumentError, _("%{value} is not a valid rendering format: %{formats_list}") % 132 { value: value.inspect, formats_list: formats.sort.join(", ")} 133 end 134 135 @action.render_as = value 136 end
Sets what the action does when it is invoked. This takes a block which will be called when the action is invoked. The action will accept arguments based on the arity of the block. It should always take at least one argument for options. Options will be the last argument.
@overload when_invoked
({|options| … })
An action with no arguments
@overload when_invoked
({|arg1, arg2, options| … })
An action with two arguments
@return [void] @api public @dsl Faces
# File lib/puppet/interface/action_builder.rb 45 def when_invoked(&block) 46 @action.when_invoked = block 47 end
Sets a block to be run at the rendering stage, for a specific rendering type (eg JSON, YAML, console), after the block for when_invoked
gets run. This manipulates the value returned by the action. It makes it possible to work around limitations in the underlying object returned, and should be avoided in favor of returning a more capable object. @api private @todo this needs more @dsl Faces
# File lib/puppet/interface/action_builder.rb 58 def when_rendering(type = nil, &block) 59 if type.nil? then # the default error message sucks --daniel 2011-04-18 60 #TRANSLATORS 'when_rendering' is a method name and should not be translated 61 raise ArgumentError, _('You must give a rendering format to when_rendering') 62 end 63 if block.nil? then 64 #TRANSLATORS 'when_rendering' is a method name and should not be translated 65 raise ArgumentError, _('You must give a block to when_rendering') 66 end 67 @action.set_rendering_method_for(type, block) 68 end