Method: PDK::Generate::PuppetObject#with_templates

Defined in:
lib/pdk/generators/puppet_object.rb

#with_templates {|template_paths, config_hash| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Search the possible template directories in order of preference to find a template that can be used to render a new object of the specified type.

Yield Parameters:

  • template_paths (Hash{Symbol => String})

    :object contains the path on disk to the template file for the object, :spec contains the path on disk to the template file for the tests for the object (if it exists).

  • config_hash (Hash{Object => Object})

    the contents of the :global key in the config_defaults.yml file.

Raises:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pdk/generators/puppet_object.rb', line 152

def with_templates
  templates.each do |template|
    if template[:url].nil?
      PDK.logger.debug(_('No %{dir_type} template specified; trying next template directory.') % { dir_type: template[:type] })
      next
    end

    PDK::Module::TemplateDir.new(template[:url]) do |template_dir|
      template_paths = template_dir.object_template_for(object_type)

      if template_paths
        config_hash = template_dir.object_config
        yield template_paths, config_hash
        # TODO: refactor to a search-and-execute form instead
        return # work is done # rubocop:disable Lint/NonLocalExitFromIterator
      elsif template[:allow_fallback]
        PDK.logger.debug(_('Unable to find a %{type} template in %{url}, trying next template directory') % { type: object_type, url: template[:url] })
      else
        raise PDK::CLI::FatalError, _('Unable to find the %{type} template in %{url}.') % { type: object_type, url: template[:url] }
      end
    end
  end
end