class PuppetGenerator::Models::Template

Public Class Methods

new( name , template_path, suitable_outputs=[], tags=[] ) click to toggle source

create new instance of template model

Calls superclass method
# File lib/puppet_generator/models/template.rb, line 8
def initialize( name , template_path, suitable_outputs=[], tags=[] )
  super(name)

  @template_path = template_path
  @suitable_outputs = suitable_outputs
  @tags = tags
end

Private Class Methods

create_tags(path) click to toggle source
# File lib/puppet_generator/models/template.rb, line 94
def create_tags(path)
  return case path 
         when %r[one_per_file]
           [ :one_per_file ]
         when %r[many_per_file]
           [ :many_per_file ]
         end
end
load_from_filesystem() click to toggle source
# File lib/puppet_generator/models/template.rb, line 83
def load_from_filesystem
  files = Dir.glob( path_to_instances )

  raise FeduxOrg::Stdlib::Models::Exceptions::NoImplementationsForModelFound, "You might need to store files at \"#{File.dirname(path_to_instances)}\" to make the library work." if files.blank?

  files.each do |f| 
    create( name( f ) , f, suitable_outputs_for_path( f ), create_tags( f ) )
  end

end
path() click to toggle source
# File lib/puppet_generator/models/template.rb, line 114
def path
  __FILE__
end
path_to_instances() click to toggle source
# File lib/puppet_generator/models/template.rb, line 73
def path_to_instances
  path = File.expand_path("../../../../#{model_name.downcase.pluralize}/puppet", __FILE__ )

  File.join(path,'**', "*#{ suffix }")
end
suffix() click to toggle source
# File lib/puppet_generator/models/template.rb, line 69
def suffix
  '.pp.erb'
end
suitable_outputs_for_path(path) click to toggle source
# File lib/puppet_generator/models/template.rb, line 103
def suitable_outputs_for_path(path)
  return case path 
         when %r[one_per_file]
           [ :directory, :dir, :stdout , :file ]
         when %r[many_per_file]
           [ :file , :stdout ]
         else
           [ :file , :stdout ]
         end
end

Public Instance Methods

is_suitable_for?(output) click to toggle source

check if a template is suitable for a given output

# File lib/puppet_generator/models/template.rb, line 23
def is_suitable_for?(output)
  @suitable_outputs.include? output
end
is_tagged_with?(asked_tags) click to toggle source

check if a template is tagged

# File lib/puppet_generator/models/template.rb, line 28
def is_tagged_with?(asked_tags)
  if asked_tags
    return asked_tags.all? { |t| @tags.include? t }
  else
    return true
  end
end
path() click to toggle source

output path to template

# File lib/puppet_generator/models/template.rb, line 17
def path
  @template_path
end
render(items) click to toggle source

render the template based on files

# File lib/puppet_generator/models/template.rb, line 37
def render(items)

  if @tags.include? :many_per_file
    return [ Definition.new( nil , template.evaluate( items: items ) ) ]
  elsif @tags.include? :one_per_file
    return items.collect { |item| Definition.new( item.suggested_file_name, template.evaluate( item: item ) ) }
  else
    raise
  end

rescue Exception => e
  raise Exceptions::InvalidTemplate, "An invalid template \"#{@template_path}\" was used. Please check and correct the syntax and try again. The original error message was: #{e.message}."
end

Private Instance Methods

template() click to toggle source
# File lib/puppet_generator/models/template.rb, line 53
def template
  Erubis::Eruby.new( template_content )
rescue
  raise Exceptions::InvalidTemplate, "An invalid template \"#{@template_path}\" was used. Please check and correct the syntax and try again."
end
template_content() click to toggle source
# File lib/puppet_generator/models/template.rb, line 59
def template_content
  File.read( @template_path )
end