Class: Wakame::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_instance) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
# File 'lib/wakame/template.rb', line 11

def initialize(service_instance)
  @service_instance = service_instance
  @tmp_basedir = File.expand_path(Util.gen_id, File.join(Wakame.config.root_path, 'tmp', 'config') )
  FileUtils.mkdir_p @tmp_basedir
end

Instance Attribute Details

#service_instanceObject

Returns the value of attribute service_instance.



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

def service_instance
  @service_instance
end

#tmp_basedirObject (readonly)

Returns the value of attribute tmp_basedir.



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

def tmp_basedir
  @tmp_basedir
end

Instance Method Details

#basedirObject



17
18
19
# File 'lib/wakame/template.rb', line 17

def basedir
  @service_instance.resource.basedir
end

#chmod(path, mode) ⇒ Object



57
58
59
# File 'lib/wakame/template.rb', line 57

def chmod(path, mode)
  File.chmod(mode, File.join(@tmp_basedir, path))
end

#cleanupObject



37
38
39
# File 'lib/wakame/template.rb', line 37

def cleanup
  FileUtils.rm_r( @tmp_basedir, :force=>true)
end

#cp(path) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/wakame/template.rb', line 47

def cp(path)
  destpath = File.expand_path(path, @tmp_basedir)
  FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))
  
  FileUtils.cp_r(File.join(basedir, path),
                 destpath,
                 {:preserve=>true}
                 )
end

#glob_basedir(glob_patterns, &blk) ⇒ Object



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

def glob_basedir(glob_patterns, &blk)
  glob_patterns = [glob_patterns] if glob_patterns.is_a? String

  basedir_obj = Pathname.new(basedir)
  paths = glob_patterns.collect {|pattern| Pathname.glob(File.join(basedir, pattern)).collect {|path| path.relative_path_from(basedir_obj) } }
  paths = paths.flatten.uniq

  paths.each &blk if blk

  paths
end

#load(path) ⇒ Object



61
62
63
64
65
# File 'lib/wakame/template.rb', line 61

def load(path)
  path = path.sub(/^\//, '')
  
  return File.readlines(File.expand_path(path, basedir), "r").join('')
end

#render(path) ⇒ Object



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

def render(path)
  update(path) { |buf|
    ERB.new(buf, nil, '-').result(service_instance.export_binding)
  }
end

#render_configObject



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

def render_config
  service_instance.resource.render_config(self)
end

#save(path, buf) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/wakame/template.rb', line 67

def save(path, buf)
  path = path.sub(/^\//, '')
  
  destpath = File.expand_path(path, @tmp_basedir)
  FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))
  
  File.open(destpath, "w", 0644) { |f|
    f.write(buf)
  }
end

#update(path, &blk) ⇒ Object



78
79
80
81
82
# File 'lib/wakame/template.rb', line 78

def update(path, &blk)
  buf = load(path)
  buf = yield buf if block_given?
  save(path, buf)
end