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

module Puppet::Interface::ActionManager
  # Declare that this app can take a specific action, and provide
  # the code to do so.
  def action(name, &block)
    @actions ||= {}
    raise "Action #{name} already defined for #{self}" if action?(name)
    action = Puppet::Interface::ActionBuilder.build(self, name, &block)
    @actions[action.name] = action
  end

  # This is the short-form of an action definition; it doesn't use the
  # builder, just creates the action directly from the block.
  def script(name, &block)
    @actions ||= {}
    raise "Action #{name} already defined for #{self}" if action?(name)
    @actions[name] = Puppet::Interface::Action.new(self, name, :when_invoked => block)
  end

  def actions
    @actions ||= {}
    result = @actions.keys

    if self.is_a?(Class) and superclass.respond_to?(:actions)
      result += superclass.actions
    elsif self.class.respond_to?(:actions)
      result += self.class.actions
    end
    result.sort
  end

  def get_action(name)
    @actions ||= {}
    result = @actions[name.to_sym]
    if result.nil?
      if self.is_a?(Class) and superclass.respond_to?(:get_action)
        result = superclass.get_action(name)
      elsif self.class.respond_to?(:get_action)
        result = self.class.get_action(name)
      end
    end
    return result
  end

  def action?(name)
    actions.include?(name.to_sym)
  end
end