Last active
January 30, 2020 15:52
-
-
Save hlindberg/e033ab9c8e20282cc494db6f04afc308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The latch function remembers what it was called with | |
# and returns what it was previously called with. | |
# | |
# This is using global namespace - in your code use a module namespace | |
# | |
Puppet::Functions.create_function(:latch) do | |
dispatch :example do | |
repeated_param 'Any', :arg | |
end | |
def example(*args) | |
adapter = adapterclass.adapt(closure_scope().compiler) | |
result = adapter.thing | |
adapter.thing = args | |
result | |
end | |
def adapterclass | |
@adapterclass ||= Class.new(Puppet::Pops::Adaptable::Adapter) do | |
attr_accessor :thing | |
def self.name() | |
# This is using global namespace - in your code use a module namespace | |
"LatchFunctionAdapter" | |
end | |
end | |
@adapterclass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be safe in general use of the pattern showed in the gist, the name of the class returned by name should actually include the
id
of the class as well - this because it could exist in a different version in memory in another environment - it will however not be a problem as long as it is being used to adapt to the environment or compiler (as shown in this gist) since any other version of the adapter would be adapted to other instances of the compiler/environment...