Bug #6758

When calling interfaces through Ruby, options should be passed as an argument

Added by Paul Berry about 1 year ago. Updated 11 months ago.

Status:Closed Start date:03/17/2011
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:Faces
Target version:2.7.0
Affected Puppet version: Branch:
Keywords:
Votes: 0

Description

In the current implementation of interfaces, in order to pass an option to an interface action, one must set the option on the interface object itself, either like this:

interface = Puppet::Interface.interface(:<interface_name>)
interface.options = <options_hash>
interface.<action>(<args>)

Or like this:

Puppet::Interface::<Interface_name>.options = <options_hash>
Puppet::Interface::<Interface_name>.<action>(<args>)

In addition to being clumsy, this places the options in a location that is effectively global, so it carries all the disadvantages of global variables. For example, if the implementation of action 1 calls action 2, and the implementation of action 2 changes the options and calls action 3, then there is a danger that the implementor of action 2 will forget to restore the options back to the initial state before returning to action 1.

Also, all kinds of havoc is likely to ensue if this mechanism is ever used in a multithreaded context.

It would be far better if we passed the options to the action using the conventional Ruby options hash mechanism:

Puppet::Interface::<Interface_name>.<action>(<args>, <options_hash>)

History

Updated by Paul Berry about 1 year ago

An additional problem with the existing mechanism is that if a user were ever to try to create an interface action called “options”, many things would likely break.

Updated by Paul Berry about 1 year ago

  • Category changed from 21 to Faces

Updated by Pieter van de Bruggen about 1 year ago

We should provide the interface demonstrated on the last line of the ticket’s body, but providing the options hash should be optional.

Also, since the block provided for the action is unlikely to (read: not going to) contain a slot for the options hash, we should make the hash available as a call to the method options.

Puppet::Interface.new :action do
  options[:foo]
end

This solution should be as tightly localized to the invocation of the action as possible — global variables / properties are not acceptable (or safe).

Updated by Daniel Pittman about 1 year ago

https://github.com/puppetlabs/puppet-interfaces/commit/9ce031d300738c778254358792ee295c08252cff

This has been implemented by passing the options hash as the last argument to the action, uniformly.

Updated by Daniel Pittman about 1 year ago

  • Status changed from Accepted to Closed

Updated by James Turnbull about 1 year ago

  • Target version changed from 2.6.x to 2.7.x

Updated by James Turnbull 11 months ago

  • Target version changed from 2.7.x to 2.7.0

Also available in: Atom PDF