Class: Puppet::Interface::OptionBuilder

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

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.



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

def initialize(face, *declaration, &block)
  @face   = face
  @option = Puppet::Interface::Option.new(face, *declaration)
  instance_eval(&block) if block_given?
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



8
9
10
# File 'lib/puppet/interface/option_builder.rb', line 8

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



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

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)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/interface/option_builder.rb', line 65

def after_action(&block)
  unless block
    # TRANSLATORS 'after_action' is a method name and should not be translated
    raise ArgumentError, _("%{option} after_action requires a block") % { option: @option }
  end
  if @option.after_action
    # TRANSLATORS 'after_action' is a method name and should not be translated
    raise ArgumentError, _("%{option} already has an after_action set") % { option: @option }
  end
  unless block.arity == 3 then
    # TRANSLATORS 'after_action' is a method name and should not be translated
    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:

  • action (Puppet::Interface::Action)

    The action being invoked

  • args (Array)

    The arguments given to the action

  • options (Hash<Symbol=>Object>)

    Any options set



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/interface/option_builder.rb', line 44

def before_action(&block)
  unless block
    # TRANSLATORS 'before_action' is a method name and should not be translated
    raise ArgumentError, _("%{option} before_action requires a block") % { option: @option }
  end
  if @option.before_action
    # TRANSLATORS 'before_action' is a method name and should not be translated
    raise ArgumentError, _("%{option} already has a before_action set") % { option: @option }
  end
  unless block.arity == 3 then
    # TRANSLATORS 'before_action' is a method name and should not be translated
    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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/interface/option_builder.rb', line 95

def default_to(&block)
  unless block
    # TRANSLATORS 'default_to' is a method name and should not be translated
    raise ArgumentError, _("%{option} default_to requires a block") % { option: @option }
  end
  if @option.has_default?
    raise ArgumentError, _("%{option} already has a default value") % { option: @option }
  end
  unless block.arity == 0
    # TRANSLATORS 'default_to' is a method name and should not be translated
    raise ArgumentError, _("%{option} default_to block should not take any arguments") % { option: @option }
  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.



86
87
88
# File 'lib/puppet/interface/option_builder.rb', line 86

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