Class: AethernalAgent::Template

Inherits:
Object
  • Object
show all
Includes:
Errors, Filesystem
Defined in:
lib/aethernal_agent/template.rb

Instance Attribute Summary collapse

Attributes included from Errors

#global_errors

Instance Method Summary collapse

Methods included from Errors

#add_errors, #get_errors

Methods included from Filesystem

#aethernal_agent_file, #aethernal_agent_folder, #copy, #directory, #file_settings, #files_folder, #files_path, #home_folder_path, #meta_folder, #meta_path, #set_ownership, #set_permissions, #templates_folder, #write_template

Constructor Details

#initialize(template_path, vars, options) ⇒ Template

Returns a new instance of Template.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aethernal_agent/template.rb', line 12

def initialize(template_path, vars, options)
  self.template_path = template_path
  self.options = options

  vars.each do |k,v|
    AethernalAgent.logger.debug("Setting #{k} to #{v} #{v.class}")
    if [String, Symbol].include?(v.class)
      eval("@#{k}= '#{v}'")
    elsif v.is_a?(NilClass)
      eval("@#{k}= nil")
    else
      eval("@#{k}= #{v}")
    end
  end
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#parsedObject

Returns the value of attribute parsed.



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

def parsed
  @parsed
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#template_pathObject

Returns the value of attribute template_path.



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

def template_path
  @template_path
end

Instance Method Details

#parseObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aethernal_agent/template.rb', line 28

def parse
  erb = ERB.new(File.open(self.template_path).read)
  self.result = erb.result(binding)

  if self.result
    self.parsed = true
    return true
  else
    return false
  end
end

#write_to(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aethernal_agent/template.rb', line 40

def write_to(path)
  return false unless self.parsed

  begin
    file = File.open(path, "w")
    AethernalAgent.logger.debug("Writing template to #{path}")
    file << self.result
    file.close

    file_settings(path, self.options)

  rescue StandardError => e
    add_errors(e, path: path)
    return false
  end
end