summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/option_builder.rb
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-14 15:20:38 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-15 15:14:29 -0700
commitdca1f077dd7a818aee447222a7649742f2b1575f (patch)
treeb8083451556f444d8e580f298704499063e4aa6e /lib/puppet/interface/option_builder.rb
parent0c60aa28d2d15e7e718792871af3350c3a1fa5c7 (diff)
downloadpuppet-dca1f077dd7a818aee447222a7649742f2b1575f.tar.gz
puppet-dca1f077dd7a818aee447222a7649742f2b1575f.tar.xz
puppet-dca1f077dd7a818aee447222a7649742f2b1575f.zip
(#6978) Add before and after decorators to actions from options.
Options can now add before_action and after_action blocks; these are invoked before or after any action is invoked on the face. This allows these options to declare common behaviour and have it automatically applied to the actions invoked. Option hooks have no defined order of invocation: they will run in a completely random order. Where there are dependencies they should be on the value of the options hash passed to the invocation, not on side-effects of the other invocations. You are not able to influence the arguments, options, or calling of the action body in a before or after decorator. This is by design. The invocation passes to the hook: 1. The action object representing this action. 2. The arguments to the action, as an array. 3. The options for the action, as a hash. Paired-With: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'lib/puppet/interface/option_builder.rb')
-rw-r--r--lib/puppet/interface/option_builder.rb25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/puppet/interface/option_builder.rb b/lib/puppet/interface/option_builder.rb
index 2240b3e4a..ccad0850d 100644
--- a/lib/puppet/interface/option_builder.rb
+++ b/lib/puppet/interface/option_builder.rb
@@ -17,9 +17,28 @@ class Puppet::Interface::OptionBuilder
# Metaprogram the simple DSL from the option class.
Puppet::Interface::Option.instance_methods.grep(/=$/).each do |setter|
- next if setter =~ /^=/ # special case, darn it...
+ next if setter =~ /^=/
+ dsl = setter.sub(/=$/, '')
- dsl = setter.to_s.sub(/=$/, '')
- define_method(dsl) do |value| @option.send(setter, value) end
+ unless self.class.methods.include?(dsl)
+ define_method(dsl) do |value| @option.send(setter, value) end
+ end
+ end
+
+ # Override some methods that deal in blocks, not objects.
+ def before_action(&block)
+ block or raise ArgumentError, "#{@option} before_action requires a block"
+ if @option.before_action
+ raise ArgumentError, "#{@option} already has a before_action set"
+ end
+ @option.before_action = block
+ end
+
+ def after_action(&block)
+ block or raise ArgumentError, "#{@option} after_action requires a block"
+ if @option.after_action
+ raise ArgumentError, "#{@option} already has a after_action set"
+ end
+ @option.after_action = block
end
end