summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/action.rb
diff options
context:
space:
mode:
authorPieter van de Bruggen <pieter@puppetlabs.com>2011-04-17 22:23:44 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-17 22:23:44 -0700
commit9d2ec219bbd77bfca48a72b52fe5d0d3fcc0dcf7 (patch)
tree1b01ad690ce899fb688356ac2deb7eed4f50c302 /lib/puppet/interface/action.rb
parent0d0318f9f0eadff7f9934d3d02a7081bba05164c (diff)
downloadpuppet-9d2ec219bbd77bfca48a72b52fe5d0d3fcc0dcf7.tar.gz
puppet-9d2ec219bbd77bfca48a72b52fe5d0d3fcc0dcf7.tar.xz
puppet-9d2ec219bbd77bfca48a72b52fe5d0d3fcc0dcf7.zip
(#7013) Add support for required options.
This adds another hook into the generated wrapper, which invokes a method to validate arguments. This is used to raise an exception when required options have not been passed to the method. Reviewed-By: Daniel Pittman <daniel@puppetlabs.com>
Diffstat (limited to 'lib/puppet/interface/action.rb')
-rw-r--r--lib/puppet/interface/action.rb16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb
index b94298963..412e39449 100644
--- a/lib/puppet/interface/action.rb
+++ b/lib/puppet/interface/action.rb
@@ -83,10 +83,10 @@ class Puppet::Interface::Action
# idea how motivated we were to make this cleaner. Sorry. --daniel 2011-03-31
internal_name = "#{@name} implementation, required on Ruby 1.8".to_sym
- file = __FILE__ + "+eval"
- line = __LINE__ + 1
+ file = __FILE__ + "+eval"
+ line = __LINE__ + 1
wrapper = <<WRAPPER
-def #{@name}(*args, &block)
+def #{@name}(*args)
if args.last.is_a? Hash then
options = args.last
else
@@ -94,6 +94,7 @@ def #{@name}(*args, &block)
end
action = get_action(#{name.inspect})
+ action.validate_args(args)
__invoke_decorations(:before, action, args, options)
rval = self.__send__(#{internal_name.inspect}, *args)
__invoke_decorations(:after, action, args, options)
@@ -142,6 +143,15 @@ WRAPPER
option
end
+ def validate_args(args)
+ required = options.map do |name|
+ get_option(name)
+ end.select(&:required?).collect(&:name) - args.last.keys
+
+ return if required.empty?
+ raise ArgumentError, "missing required options (#{required.join(', ')})"
+ end
+
########################################################################
# Support code for action decoration; see puppet/interface.rb for the gory
# details of why this is hidden away behind private. --daniel 2011-04-15