summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/option_builder.rb
blob: c87adc2c0ae00a0541597917f88ab5dc586ad15e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'puppet/interface/option'

class Puppet::Interface::OptionBuilder
  attr_reader :option

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

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

  # Metaprogram the simple DSL from the option class.
  Puppet::Interface::Option.instance_methods.grep(/=$/).each do |setter|
    next if setter =~ /^=/
    dsl = setter.to_s.chomp('=')

    unless private_method_defined? dsl
      define_method(dsl) do |value| @option.send(setter, value) end
    end
  end

  # Override some methods that deal in blocks, not objects.
  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

  def after_action(&block)
    block or raise ArgumentError, "#{@option} after_action requires a block"
    if @option.after_action
      raise ArgumentError, "#{@option} already has a 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

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

  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
    # Ruby 1.8 treats a block without arguments as accepting any number; 1.9
    # gets this right, so we work around it for now... --daniel 2011-07-20
    unless block.arity == 0 or (RUBY_VERSION =~ /^1\.8/ and block.arity == -1)
      raise ArgumentError, "#{@option} default_to block should not take any arguments"
    end
    @option.default = block
  end
end