class Gem2Rpm::Template

Attributes

filename[RW]

Public Class Methods

default_location() click to toggle source
# File lib/gem2rpm/template.rb, line 7
def self.default_location
  @@location ||= File.join(File.dirname(__FILE__), '..', '..', 'templates')
end
default_location=(location) click to toggle source
# File lib/gem2rpm/template.rb, line 11
def self.default_location=(location)
  @@location = location
  @@list = nil
end
find(name = nil, options = {}) click to toggle source

Returns instance of Template class. If the 'name' parameter is specified it tries to instantiate the template of specific name first. When options is specified, it can be taken into consideration when looking for vagrant templates for example.

# File lib/gem2rpm/template.rb, line 26
def self.find(name = nil, options = {})
  if name.nil?
    gem_file = File.basename(options[:gem_file]) if options[:gem_file]
    case gem_file
    when /^vagrant(-|_).*/
      Gem2Rpm.vagrant_plugin_template
    else
      Gem2Rpm.rubygem_template
    end
  else
    begin
      if File.exists?(name)
        Gem2Rpm::Template.new(name)
      else
        Gem2Rpm::Template.new(File.join(Gem2Rpm::Template::default_location, name + '.spec.erb'))
      end
    rescue TemplateError
      raise TemplateError, "Could not locate template #{name}"
    end
  end
end
list() click to toggle source
# File lib/gem2rpm/template.rb, line 16
def self.list
  @@list ||= Dir.chdir(default_location) do
    Dir.glob('*').sort
  end
end
new(filename) click to toggle source

Create instance of Template class of specified template filename. TemplateError is raised when the template file does not exists.

# File lib/gem2rpm/template.rb, line 50
def initialize(filename)
  if File.exists? filename
    @filename = filename
  else
    raise TemplateError, "Could not locate template #{filename}"
  end
end

Public Instance Methods

read() click to toggle source

Read the content of the template file.

# File lib/gem2rpm/template.rb, line 59
def read
  @content ||= begin
    File.open(@filename, OPEN_MODE) {|f| f.read }
  end
end