summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/action_builder.rb
blob: 777fcaf850a258f1027f638b2e0ea0a7feeeed23 (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
require 'puppet/interface'

class Puppet::Interface::ActionBuilder
  attr_reader :action

  def self.build(interface, name, &block)
    name = name.to_s
    raise "Action '#{name}' must specify a block" unless block
    builder = new(interface, name, &block)
    builder.action
  end

  def initialize(interface, name, &block)
    @interface = interface
    @action = Puppet::Interface::Action.new(interface, name)
    instance_eval(&block)
  end

  # Ideally the method we're defining here would be added to the action, and a
  # method on the interface would defer to it
  def invoke(&block)
    raise "Invoke called on an ActionBuilder with no corresponding Action" unless @action
    if @interface.is_a?(Class)
      @interface.define_method(@action.name, &block)
    else
      @interface.meta_def(@action.name, &block)
    end
  end
end