Class: Temp::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/temp/template.rb

Overview

A Template object represents a template and provides information about it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, conf) ⇒ Template

Loads a template



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/temp/template.rb', line 20

def initialize(name, conf)
  @conf = conf
  @path = File.join(@conf.template_dir, name)
  @filename = name
  raise Temp::Exceptions::TemplateNonExistentError unless File.exist? @path

  @tempfile = Temp::Tempfile.new(@path, @conf.template_options)
  @name = @tempfile.info[:name] || name
  @desc = @tempfile.info[:desc] || ''
  @files = Template.find_files(@path) - @tempfile.ignore_files
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



8
9
10
# File 'lib/temp/template.rb', line 8

def conf
  @conf
end

#descObject (readonly)

Returns the value of attribute desc.



8
9
10
# File 'lib/temp/template.rb', line 8

def desc
  @desc
end

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/temp/template.rb', line 8

def filename
  @filename
end

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/temp/template.rb', line 8

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/temp/template.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/temp/template.rb', line 8

def path
  @path
end

#tempfileObject (readonly)

Returns the value of attribute tempfile.



8
9
10
# File 'lib/temp/template.rb', line 8

def tempfile
  @tempfile
end

Class Method Details

.all(conf) ⇒ Object

Returns an array of all templates in the template directory



11
12
13
14
15
16
17
# File 'lib/temp/template.rb', line 11

def self.all(conf)
  Dir.entries(conf.template_dir).map do |t|
    unless t == '.' || t == '..'
      Temp::Template.new(t, conf)
    end
  end.compact
end

.find_files(path) ⇒ Object

Finds all of the files in a path



33
34
35
36
# File 'lib/temp/template.rb', line 33

def self.find_files(path)
  path = File.expand_path(path)
  Dir.glob(File.join(path, '**/*')).map { |f| f.sub(path + '/', '') }
end