summaryrefslogtreecommitdiffstats
path: root/lib/puppet/interface/action_builder.rb
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-18 13:29:47 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-19 10:53:36 -0700
commit86801b580101315706b1b02a00a36840eabd75cd (patch)
tree9d0db179dabc6b113ff63965df0f01a2e2dadeb2 /lib/puppet/interface/action_builder.rb
parentbe23b8423ba77a5935586e277bf543cd54b9dec7 (diff)
downloadpuppet-86801b580101315706b1b02a00a36840eabd75cd.tar.gz
puppet-86801b580101315706b1b02a00a36840eabd75cd.tar.xz
puppet-86801b580101315706b1b02a00a36840eabd75cd.zip
(#7013) Support 'when_rendering' and 'render_as' in actions.
These define the API used by folks writing actions that supports their rendering hooks. 'when_rendering' defines a helper method on the interface, which runs the users code in their expected context. 'render_as' just sets the default rendering format; by default this is :for_humans. Reviewed-By: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'lib/puppet/interface/action_builder.rb')
-rw-r--r--lib/puppet/interface/action_builder.rb44
1 files changed, 39 insertions, 5 deletions
diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb
index 639d8fc7f..2ffa38709 100644
--- a/lib/puppet/interface/action_builder.rb
+++ b/lib/puppet/interface/action_builder.rb
@@ -20,20 +20,54 @@ class Puppet::Interface::ActionBuilder
# method on the face would defer to it, but we can't get scope correct, so
# we stick with this. --daniel 2011-03-24
def when_invoked(&block)
- raise "when_invoked on an ActionBuilder with no corresponding Action" unless @action
@action.when_invoked = block
end
+ def when_rendering(type = nil, &block)
+ if type.nil? then # the default error message sucks --daniel 2011-04-18
+ raise ArgumentError, 'You must give a rendering format to when_rendering'
+ end
+ if block.nil? then
+ raise ArgumentError, 'You must give a block to when_rendering'
+ end
+ @action.set_rendering_method_for(type, block)
+ end
+
def option(*declaration, &block)
option = Puppet::Interface::OptionBuilder.build(@action, *declaration, &block)
@action.add_option(option)
end
- def default
- @action.default = true
+ def default(value = true)
+ @action.default = !!value
+ end
+
+ def render_as(value = nil)
+ value.nil? and raise ArgumentError, "You must give a rendering format to render_as"
+
+ formats = Puppet::Network::FormatHandler.formats << :for_humans
+ unless formats.include? value
+ raise ArgumentError, "#{value.inspect} is not a valid rendering format: #{formats.sort.join(", ")}"
+ end
+
+ @action.render_as = value
end
- def summary(text)
- @action.summary = text
+ # Metaprogram the simple DSL from the target class.
+ Puppet::Interface::Action.instance_methods.grep(/=$/).each do |setter|
+ next if setter =~ /^=/
+ dsl = setter.sub(/=$/, '')
+
+ unless private_instance_methods.include? dsl
+ # Using eval because the argument handling semantics are less awful than
+ # when we use the define_method/block version. The later warns on older
+ # Ruby versions if you pass the wrong number of arguments, but carries
+ # on, which is totally not what we want. --daniel 2011-04-18
+ eval <<METHOD
+def #{dsl}(value)
+ @action.#{dsl} = value
+end
+METHOD
+ end
end
end