Class: Mccloud::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env) ⇒ Template

Returns a new instance of Template.



12
13
14
15
16
17
18
19
20
21
# File 'lib/mccloud/template.rb', line 12

def initialize(name,env)
  @name=name
  @env=env

  @erb=true
  @file=nil
  @params=Hash.new
  @author="No author specified"
  @bootstrap=nil
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



6
7
8
# File 'lib/mccloud/template.rb', line 6

def author
  @author
end

#bootstrapObject

Returns the value of attribute bootstrap.



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

def bootstrap
  @bootstrap
end

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#erbObject

Returns the value of attribute erb.



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

def erb
  @erb
end

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/mccloud/template.rb', line 3

def file
  @file
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/mccloud/template.rb', line 5

def params
  @params
end

Instance Method Details

#comment(flag) ⇒ Object



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

def comment(flag)
  cur_pos=@output.length
  yield
  new_pos=@output.length
  if flag

    # Extraxt the block
    block_text=@output[cur_pos..new_pos]

    # Remove the block
    @output[cur_pos..new_pos]=''

    # Comment the block, with leading spaces into account
    block_text.gsub!(/^(\s)*/,'\1# ')

    # Re-insert the block
    @output=@output.insert cur_pos, block_text
  end

end

#exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mccloud/template.rb', line 27

def exists?
  File.directory?(self.path)
end

#pathObject



23
24
25
# File 'lib/mccloud/template.rb', line 23

def path
  File.join(@env.config.templates.path,@name)
end

#to_sObject



58
59
60
# File 'lib/mccloud/template.rb', line 58

def to_s
  "Template #{name}"
end

#to_template(vm_name = "noname") ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/mccloud/template.rb', line 62

def to_template(vm_name="noname")
  result=""
  filename=@file
  env.logger.info "Opening template file #{@file}"
  if File.exists?(filename)
    template=File.new(filename).read
    result=ERB.new(template,nil,"-","@output").result(binding)
  end
  return result
end