Class: Puppet::Interface::OptionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/interface/option_builder.rb

Overview

API:

  • public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(face, *declaration, &block) ⇒ OptionBuilder

Returns a new instance of OptionBuilder.

API:

  • public



15
16
17
18
19
20
# File 'lib/puppet/interface/option_builder.rb', line 15

def initialize(face, *declaration, &block)
  @face   = face
  @option = Puppet::Interface::Option.new(face, *declaration)
  instance_eval(&block) if block_given?
  @option
end

Instance Attribute Details

#optionPuppet::Interface::Option (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 option under construction

Returns:

API:

  • private



6
7
8
# File 'lib/puppet/interface/option_builder.rb', line 6

def option
  @option
end

Class Method Details

.build(face, *declaration, &block) ⇒ Puppet::Interface::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.

Build an option

Returns:

API:

  • private



11
12
13
# File 'lib/puppet/interface/option_builder.rb', line 11

def self.build(face, *declaration, &block)
  new(face, *declaration, &block).option
end

Instance Method Details

#after_action(&block) ⇒ Object

Sets a block to be executed after an action is invoked. !(see before_action)

API:

  • public



57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/interface/option_builder.rb', line 57

def after_action(&block)
  block or raise ArgumentError, "#{@option} after_action requires a block"
  if @option.after_action
    raise ArgumentError, "#{@option} already has an after_action set"
  end
  unless block.arity == 3 then
    raise ArgumentError, "after_action takes three arguments, action, args, and options"
  end
  @option.after_action = block
end

#before_action {|action, args, options| ... } ⇒ Object

Sets a block to be executed when an action is invoked before the main action code. This is most commonly used to validate an option.

Yield Parameters:

API:

  • public



42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/interface/option_builder.rb', line 42

def before_action(&block)
  block or raise ArgumentError, "#{@option} before_action requires a block"
  if @option.before_action
    raise ArgumentError, "#{@option} already has a before_action set"
  end
  unless block.arity == 3 then
    raise ArgumentError, "before_action takes three arguments, action, args, and options"
  end
  @option.before_action = block
end

#default_to(&block) ⇒ Object

Sets a block that will be used to compute the default value for this option. It will be evaluated when the action is invoked. The block should take no arguments.

API:

  • public



81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet/interface/option_builder.rb', line 81

def default_to(&block)
  block or raise ArgumentError, "#{@option} default_to requires a block"
  if @option.has_default?
    raise ArgumentError, "#{@option} already has a default value"
  end
  unless block.arity == 0
    raise ArgumentError, "#{@option} default_to block should not take any arguments"
  end
  @option.default = block
end

#required(value = true) ⇒ Object

Sets whether the option is required. If no argument is given it defaults to setting it as a required option.

API:

  • public



72
73
74
# File 'lib/puppet/interface/option_builder.rb', line 72

def required(value = true)
  @option.required = value
end