Module: EasyType::Template

Includes:
Helpers
Defined in:
lib/easy_type/template.rb

Overview

Contains a template helper method.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#camelize, #convert_csv_data_to_hash, #get_puppet_file

Class Method Details

.included(parent) ⇒ Object



17
18
19
# File 'lib/easy_type/template.rb', line 17

def self.included(parent)
  parent.extend(Template)
end

Instance Method Details

#local_template(template_name, context) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/easy_type/template.rb', line 44

def local_template(template_name, context)
  segments = template_name.split('/')
  fail "template name #{template_name} invalid. Template name should include module directory" \
    if segments.size < 2
  module_name = segments.shift
  core_template_name = segments.pop
  folder_name = segments.join('/')
  content = File.read("#{template_base_path(module_name)}/provider/#{folder_name}/templates/#{core_template_name}")
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
    ERB.new(content, nil, '-').result(context)
  else
    ERB.new(content, trim_mode: '-').result(context)
  end
rescue Errno::ENOENT
  raise ArgumentError, "Could not find template '#{template_name}'. "
end

#puppet_template(template_name, context) ⇒ Object



61
62
63
# File 'lib/easy_type/template.rb', line 61

def puppet_template(template_name, context)
  ERB.new(get_puppet_file(template_name), trim_mode:'-').result(context)
end

#template(template_name, context) ⇒ String

This allows you to use an erb file. Just like in the normal Puppet classes. The file is searched in the template directory on the same level as the ruby library path. For most puppet classes this is eqal to the normal template path of a module

Examples:

template 'puppet:///modules/my_module_name/create_tablespace.sql.erb', binding

Parameters:

  • name (String)

    this is the name of the template to be used.

  • context (Binding)

    this is the binding to be used in the template

Returns:

  • (String)

    interpreted ERB template

Raises:

  • (ArgumentError)

    when the file doesn’t exist



36
37
38
39
40
41
42
# File 'lib/easy_type/template.rb', line 36

def template(template_name, context)
  if template_name =~ /puppet:/
    puppet_template(template_name, context)
  else
    local_template(template_name, context)
  end
end