Class: Puppet::Interface::ActionBuilder
- Extended by:
- Forwardable
- Defined in:
- lib/puppet/interface/action_builder.rb
Overview
This class is used to build 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.
Instance Attribute Summary collapse
-
#action ⇒ Puppet::Interface::Action
readonly
private
The action under construction.
Class Method Summary collapse
-
.build(face, name, &block) ⇒ Puppet::Interface::Action
private
Builds a new action.
Instance Method Summary collapse
-
#default(value = true) ⇒ void
Set this as the default action for the face.
-
#deprecate ⇒ void
private
Deprecates the action.
- #display_global_options(*args) ⇒ Object (also: #display_global_option) private
-
#option(*declaration, &block) ⇒ Object
Declare that this action can take a specific option, and provide the code to do so.
-
#render_as(value = nil) ⇒ Object
private
Sets the default rendering format.
-
#when_invoked(&block) ⇒ void
Sets what the action does when it is invoked.
-
#when_rendering(type = nil, &block) ⇒ Object
private
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.
Instance Attribute Details
#action ⇒ Puppet::Interface::Action (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The action under construction
14 15 16 |
# File 'lib/puppet/interface/action_builder.rb', line 14 def action @action end |
Class Method Details
.build(face, name, &block) ⇒ Puppet::Interface::Action
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a new action.
19 20 21 22 23 |
# File 'lib/puppet/interface/action_builder.rb', line 19 def self.build(face, name, &block) raise "Action #{name.inspect} must specify a block" unless block new(face, name, &block).action end |
Instance Method Details
#default(value = true) ⇒ void
This method returns an undefined value.
Set this as the default action for the face.
117 118 119 |
# File 'lib/puppet/interface/action_builder.rb', line 117 def default(value = true) @action.default = !!value end |
#deprecate ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Deprecates the action
29 30 31 |
# File 'lib/puppet/interface/action_builder.rb', line 29 def deprecate @action.deprecate end |
#display_global_options(*args) ⇒ Object Also known as: display_global_option
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
122 123 124 |
# File 'lib/puppet/interface/action_builder.rb', line 122 def (*args) @action. args end |
#option(*declaration, &block) ⇒ Object
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 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 OptionBuilder).
108 109 110 111 |
# File 'lib/puppet/interface/action_builder.rb', line 108 def option(*declaration, &block) option = Puppet::Interface::OptionBuilder.build(@action, *declaration, &block) @action.add_option(option) end |
#render_as(value = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the default rendering format
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/puppet/interface/action_builder.rb', line 129 def render_as(value = nil) if value.nil? # TRANSLATORS 'render_as' is a method name and should not be translated raise ArgumentError, _("You must give a rendering format to render_as") end formats = Puppet::Network::FormatHandler.formats unless formats.include? value raise ArgumentError, _("%{value} is not a valid rendering format: %{formats_list}") % { value: value.inspect, formats_list: formats.sort.join(", ") } end @action.render_as = value end |
#when_invoked({|options| ... }) ⇒ void #when_invoked({|arg1, arg2, options| ... }) ⇒ void
This method returns an undefined value.
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.
50 51 52 |
# File 'lib/puppet/interface/action_builder.rb', line 50 def when_invoked(&block) @action.when_invoked = block end |
#when_rendering(type = nil, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
this needs more
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.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/puppet/interface/action_builder.rb', line 63 def when_rendering(type = nil, &block) if type.nil? then # the default error message sucks --daniel 2011-04-18 # TRANSLATORS 'when_rendering' is a method name and should not be translated raise ArgumentError, _('You must give a rendering format to when_rendering') end if block.nil? then # TRANSLATORS 'when_rendering' is a method name and should not be translated raise ArgumentError, _('You must give a block to when_rendering') end @action.set_rendering_method_for(type, block) end |