summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-16 02:35:23 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-16 02:35:23 +0000
commit86b33867b156f9d80bdccf895f16ec0ac3a8a204 (patch)
tree48759c24523d5d28db46a58e375678db89e6a6aa
parent32deb3ff495921769ee0cc6f245e121e59336b1a (diff)
downloadpuppet-86b33867b156f9d80bdccf895f16ec0ac3a8a204.tar.gz
puppet-86b33867b156f9d80bdccf895f16ec0ac3a8a204.tar.xz
puppet-86b33867b156f9d80bdccf895f16ec0ac3a8a204.zip
Adding the ability to have hooks for configuration parameters. This will simplify things like setting the shell path.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1783 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/config.rb9
-rwxr-xr-xtest/other/config.rb56
2 files changed, 64 insertions, 1 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb
index 8d7b6b895..5725bcd93 100644
--- a/lib/puppet/config.rb
+++ b/lib/puppet/config.rb
@@ -721,6 +721,10 @@ Generated on #{Time.now}.
@desc = value.gsub(/^\s*/, '')
end
+ def hook=(block)
+ meta_def :handle, &block
+ end
+
# Create the new element. Pretty much just sets the name.
def initialize(args = {})
if args.include?(:parent)
@@ -805,11 +809,16 @@ Generated on #{Time.now}.
if respond_to?(:validate)
validate(value)
end
+
if respond_to?(:munge)
@value = munge(value)
else
@value = value
end
+
+ if respond_to?(:handle)
+ handle(@value)
+ end
end
end
diff --git a/test/other/config.rb b/test/other/config.rb
index 09d6abe3b..9eed6b1e0 100755
--- a/test/other/config.rb
+++ b/test/other/config.rb
@@ -1,4 +1,4 @@
-#!/usr/bin/env ruby
+#!/usr/bin/env ruby -I../lib -I../../lib
require 'puppet'
require 'puppet/config'
@@ -797,6 +797,60 @@ inttest = 27
end
+
+ # Test that config parameters correctly call passed-in blocks when the value
+ # is set.
+ def test_paramblocks
+ config = mkconfig()
+
+ testing = nil
+ elem = nil
+ assert_nothing_raised do
+ elem = config.newelement :default => "yay",
+ :name => :blocktest,
+ :section => :test,
+ :hook => proc { |value| testing = value }
+ end
+
+ assert_nothing_raised do
+ assert_equal("yay", elem.value)
+ end
+
+ assert_nothing_raised do
+ elem.value = "yaytest"
+ end
+
+ assert_nothing_raised do
+ assert_equal("yaytest", elem.value)
+ end
+ assert_equal("yaytest", testing)
+
+ assert_nothing_raised do
+ elem.value = "another"
+ end
+
+ assert_nothing_raised do
+ assert_equal("another", elem.value)
+ end
+ assert_equal("another", testing)
+
+ # Now verify it works from setdefault
+ assert_nothing_raised do
+ config.setdefaults :test,
+ :blocktest2 => {
+ :default => "yay",
+ :hook => proc { |v| testing = v }
+ }
+ end
+
+ assert_equal("yay", config[:blocktest2])
+
+ assert_nothing_raised do
+ config[:blocktest2] = "footest"
+ end
+ assert_equal("footest", config[:blocktest2])
+ assert_equal("footest", testing)
+ end
end
# $Id$