Created
September 17, 2012 20:26
-
-
Save reidmv/3739568 to your computer and use it in GitHub Desktop.
Prefetch match composite namevar(s)
This file contains hidden or 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
def prefetch(resources = {}) | |
# generate hash of {provider_name => provider} | |
providers = instances.inject({}) do |hash, instance| | |
hash[instance.name] = instance | |
hash | |
end | |
# Identify the namevar(s) for the type | |
nv_properties = resource_type.properties.select(&:isnamevar?).map(&:name) | |
nv_params = resource_type.parameters.select do |param| | |
resource_type.paramclass(param).isnamevar? | |
end | |
namevars = (nv_properties | nv_params || [:name]) | |
# For each prefetched resource, try to match it to a provider | |
resources.each_pair do |resource_name, resource| | |
if provider = providers[resource_name] | |
resource.provider = provider | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout
resource_type.key_attribute_parameters
orresource_type.class.key_attribute_parameters
- can't remember which, but that will give you the namevars.