[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Daniel Pittman daniel at puppetlabs.com
Tue May 10 08:08:13 UTC 2011


The following commit has been merged in the experimental branch:
commit 633f63cdbc1d5630e546041bb0c1e714216158d0
Author: Daniel Pittman <daniel at puppetlabs.com>
Date:   Thu Mar 24 13:33:30 2011 -0700

    (#6833) support 'script' as a short form of 'action'
    
    At the moment the action method is a fairly heavy tool: it provides a DSL, and
    is designed to allow substantial metadata to be added to the action.
    
    For some users this is low on value, since they just want to write a little
    script that drives things a bit differently. Which there is substantial value
    in the metadata, adding the capability to do these light-weight things quickly
    is valid.
    
    To meet this we add a script action; the contrast is:
    
      action :foo do
        # other metadata goes here
        invoke do |args|
          # method body goes here
        end
      end
    
      script :bar do |args|
        # method body goes here
      end
      # ...and if you want metadata, you have to add it in more ugly, procedural
      # ways, which we are not going to encourage.
    
    Reviewed-By: Pieter van de Bruggen <pieter at puppetlabs.com>

diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb
index e4c2a46..1c19bd0 100644
--- a/lib/puppet/interface/action.rb
+++ b/lib/puppet/interface/action.rb
@@ -3,9 +3,11 @@ require 'puppet/interface'
 class Puppet::Interface::Action
   attr_reader :name
 
-  def initialize(interface, name)
+  def initialize(interface, name, attrs = {})
     name = name.to_s
     raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/
+
+    attrs.each do |k,v| send("#{k}=", v) end
     @interface = interface
     @name = name
   end
@@ -13,4 +15,12 @@ class Puppet::Interface::Action
   def invoke(*args, &block)
     @interface.method(name).call(*args,&block)
   end
+
+  def invoke=(block)
+    if @interface.is_a?(Class)
+      @interface.define_method(@name, &block)
+    else
+      @interface.meta_def(@name, &block)
+    end
+  end
 end
diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb
index e76fb1c..e389ea3 100644
--- a/lib/puppet/interface/action_builder.rb
+++ b/lib/puppet/interface/action_builder.rb
@@ -18,13 +18,10 @@ class Puppet::Interface::ActionBuilder
   end
 
   # Ideally the method we're defining here would be added to the action, and a
-  # method on the interface would defer to it
+  # method on the interface would defer to it, but we can't get scope correct,
+  # so we stick with this. --daniel 2011-03-24
   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
+    @action.invoke = block
   end
 end
diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb
index 0db82d6..8b2944b 100644
--- a/lib/puppet/interface/action_manager.rb
+++ b/lib/puppet/interface/action_manager.rb
@@ -14,6 +14,15 @@ module Puppet::Interface::ActionManager
     @actions[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 ||= {}
+    name = name.to_s.downcase.to_sym
+    raise "Action #{name} already defined for #{self}" if action?(name)
+    @actions[name] = Puppet::Interface::Action.new(self, name, :invoke => block)
+  end
+
   def actions
     @actions ||= {}
     result = @actions.keys
diff --git a/spec/unit/interface/action_manager_spec.rb b/spec/unit/interface/action_manager_spec.rb
old mode 100644
new mode 100755
index d1a7e31..3aff7ac
--- a/spec/unit/interface/action_manager_spec.rb
+++ b/spec/unit/interface/action_manager_spec.rb
@@ -19,6 +19,12 @@ describe Puppet::Interface::ActionManager do
       end
     end
 
+    it "should be able to define a 'script' style action" do
+      subject.script :bar do
+        "a bar is where beer is found"
+      end
+    end
+
     it "should be able to list defined actions" do
       subject.action(:foo) do
         invoke { "something" }
@@ -27,8 +33,21 @@ describe Puppet::Interface::ActionManager do
         invoke { "something" }
       end
 
-      subject.actions.should include(:bar)
-      subject.actions.should include(:foo)
+      subject.actions.should =~ [:foo, :bar]
+    end
+
+    it "should list 'script' actions" do
+      subject.script :foo do "foo" end
+      subject.actions.should =~ [:foo]
+    end
+
+    it "should list both script and normal actions" do
+      subject.action :foo do
+        invoke do "foo" end
+      end
+      subject.script :bar do "a bar is where beer is found" end
+
+      subject.actions.should =~ [:foo, :bar]
     end
 
     it "should be able to indicate when an action is defined" do
@@ -39,6 +58,11 @@ describe Puppet::Interface::ActionManager do
       subject.should be_action(:foo)
     end
 
+    it "should indicate an action is defined for script actions" do
+      subject.script :foo do "foo" end
+      subject.should be_action :foo
+    end
+
     it "should correctly treat action names specified as strings" do
       subject.action(:foo) do
         invoke { "something" }

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list