Class: Puppet::Interface::ActionBuilder

Inherits:
Object
  • Object
show all
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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Puppet::Interface::Action) action (readonly) private

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



13
14
15
# File 'lib/puppet/interface/action_builder.rb', line 13

def action
  @action
end

Class Method Details

+ (Puppet::Interface::Action) build(face, name, &block)  private

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.



18
19
20
21
# File 'lib/puppet/interface/action_builder.rb', line 18

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)  DSL

This method returns an undefined value.

Set this as the default action for the face.

DSL:

  • Faces



104
105
106
# File 'lib/puppet/interface/action_builder.rb', line 104

def default(value = true)
  @action.default = !!value
end

- (Object) display_global_options(*args)  private 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.



109
110
111
# File 'lib/puppet/interface/action_builder.rb', line 109

def display_global_options(*args)
  @action.add_display_global_options args
end

- (Object) option(*declaration, &block)  DSL

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 intial - 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).

Examples:

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

Parameters:

  • declaration (String)

    Option declarations, as described above and in the example.

DSL:

  • Faces



95
96
97
98
# File 'lib/puppet/interface/action_builder.rb', line 95

def option(*declaration, &block)
  option = Puppet::Interface::OptionBuilder.build(@action, *declaration, &block)
  @action.add_option(option)
end

- (Object) render_as(value = nil)  private

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/puppet/interface/action_builder.rb', line 116

def render_as(value = nil)
  value.nil? and raise ArgumentError, "You must give a rendering format to render_as"

  formats = Puppet::Network::FormatHandler.formats
  unless formats.include? value
    raise ArgumentError, "#{value.inspect} is not a valid rendering format: #{formats.sort.join(", ")}"
  end

  @action.render_as = value
end

- when_invoked({|options| ... })  DSL - when_invoked({|arg1, arg2, options| ... })  DSL

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.

Overloads:

  • - when_invoked({|options| ... })

    An action with no arguments

  • - when_invoked({|arg1, arg2, options| ... })

    An action with two arguments

DSL:

  • Faces



40
41
42
# File 'lib/puppet/interface/action_builder.rb', line 40

def when_invoked(&block)
  @action.when_invoked = block
end

- (Object) when_rendering(type = nil, &block)  privateDSL

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.

TODO:

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.

DSL:

  • Faces



53
54
55
56
57
58
59
60
61
# File 'lib/puppet/interface/action_builder.rb', line 53

def when_rendering(type = nil, &block)
  if type.nil? then           # the default error message sucks --daniel 2011-04-18
    raise ArgumentError, 'You must give a rendering format to when_rendering'
  end
  if block.nil? then
    raise ArgumentError, 'You must give a block to when_rendering'
  end
  @action.set_rendering_method_for(type, block)
end