Class: KISSGen::Template

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

Defined Under Namespace

Classes: SourceFileNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, path, copy_path) ⇒ Template

Returns a new instance of Template.



6
7
8
9
10
11
12
# File 'lib/kissgen/template.rb', line 6

def initialize(generator, path, copy_path)
  @generator = generator
  @path = path
  @copy_path = copy_path
  
  source_path_check
end

Instance Attribute Details

#copy_pathObject (readonly)

Returns the value of attribute copy_path.



4
5
6
# File 'lib/kissgen/template.rb', line 4

def copy_path
  @copy_path
end

#generatorObject (readonly)

Returns the value of attribute generator.



4
5
6
# File 'lib/kissgen/template.rb', line 4

def generator
  @generator
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/kissgen/template.rb', line 4

def path
  @path
end

Instance Method Details

#createObject

This will create the file with parsed data in the destination directory



27
28
29
30
31
32
33
# File 'lib/kissgen/template.rb', line 27

def create
  puts copy_path
  FileUtils.mkdir_p(File.dirname(full_copy_path))
  @file = File.new full_copy_path, "w"
  @file.print output # Taylor OWNZ
  @file.close
end

#deleteObject

Will remove the file if it exists



41
42
43
44
# File 'lib/kissgen/template.rb', line 41

def delete
  raise "Need to create the file first. File: #{@file.inspect}" unless @file
  File.delete(full_copy_path)
end

#full_copy_pathObject



22
23
24
# File 'lib/kissgen/template.rb', line 22

def full_copy_path
  File.join @generator.copy_path, @copy_path
end

#full_source_pathObject



18
19
20
# File 'lib/kissgen/template.rb', line 18

def full_source_path
  File.join @generator.path, @path
end

#outputObject

Parsed ERB output



36
37
38
# File 'lib/kissgen/template.rb', line 36

def output
  ERB.new(IO.read(full_source_path)).result
end

#source_path_checkObject



14
15
16
# File 'lib/kissgen/template.rb', line 14

def source_path_check
  raise SourceFileNotFoundError, "Template source file could not be found at #{full_source_path}" unless File.exists?(full_source_path)
end