From dca1f077dd7a818aee447222a7649742f2b1575f Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 14 Apr 2011 15:20:38 -0700 Subject: (#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 --- lib/puppet/interface/action.rb | 39 ++++++++++++++++++++++------ lib/puppet/interface/option.rb | 47 ++++++++++++++++++++++++---------- lib/puppet/interface/option_builder.rb | 25 +++++++++++++++--- lib/puppet/interface/option_manager.rb | 4 +-- 4 files changed, 89 insertions(+), 26 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index db338e39e..3946b743e 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -7,8 +7,9 @@ class Puppet::Interface::Action raise "#{name.inspect} is an invalid action name" unless name.to_s =~ /^[a-z]\w*$/ @face = face @name = name.to_sym - @options = {} attrs.each do |k, v| send("#{k}=", v) end + + @options = {} end # This is not nice, but it is the easiest way to make us behave like the @@ -84,11 +85,21 @@ class Puppet::Interface::Action internal_name = "#{@name} implementation, required on Ruby 1.8".to_sym file = __FILE__ + "+eval" line = __LINE__ + 1 - wrapper = "def #{@name}(*args, &block) - args << {} unless args.last.is_a? Hash - args << block if block_given? - self.__send__(#{internal_name.inspect}, *args) - end" + wrapper = <