Class: Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, parameter_array, target_file = nil) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/template.rb', line 11

def initialize( template, parameter_array, target_file=nil)
	if  ! parameter_array.kind_of? Array
 raise "parameter should be array"
	end

	if  parameter_array.size == 0
 raise "parameter files should not be empty"
	end

	check_file_existance template
	@template_file = template

	parameter_array.each_with_index do |param_file, index|
 check_file_existance param_file
	end

	if parameter_array.size == 1
 @param_file = parameter_array[0]
	else
 @param_file = Utils.merge_files parameter_array
	end

	merge

	if target_file != nil
 write target_file
      else
        puts @result
	end
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

#target_fileObject (readonly)

Returns the value of attribute target_file.



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

def target_file
  @target_file
end

Instance Method Details

#mergeObject



42
43
44
45
46
# File 'lib/template.rb', line 42

def merge
	template = ERB.new(File.read(@template_file), 0, "%")
	instance_eval(File.read(@param_file))
	@result = template.result(binding)
end

#write(target_file) ⇒ Object



48
49
50
51
52
# File 'lib/template.rb', line 48

def write target_file
	File.open(target_file, "w") do |file|
 file.puts @result
	end
end