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.



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

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.



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

def service_instance
  @service_instance
end

#tmp_basedirObject (readonly)

Returns the value of attribute tmp_basedir.



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

def tmp_basedir
  @tmp_basedir
end

Instance Method Details

#basedirObject



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

def basedir
  @service_instance.resource.basedir
end

#chmod(fname, mode) ⇒ Object



52
53
54
# File 'lib/wakame/template.rb', line 52

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

#cleanupObject



24
25
26
# File 'lib/wakame/template.rb', line 24

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

#cp(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wakame/template.rb', line 38

def cp(args)
  args = [args] if args.is_a? String
  args.each { |fname|

    destpath = File.expand_path(fname, @tmp_basedir)
    FileUtils.mkpath(File.dirname(destpath)) unless File.directory?(File.dirname(destpath))

    FileUtils.cp_r(File.join(basedir, fname),
                   destpath,
                   {:preserve=>true}
                   )
  }
end

#load(path) ⇒ Object



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

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

#render(args) ⇒ Object



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

def render(args)
  args = [args] if args.is_a? String
  
  args.each { |path|
    update(path) { |buf|
      ERB.new(buf, nil, '-').result(service_instance.export_binding)
    }
  }
end

#render_configObject



20
21
22
# File 'lib/wakame/template.rb', line 20

def render_config
  service_instance.property.render_config(self)
end

#save(path, buf) ⇒ Object



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

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



73
74
75
76
77
# File 'lib/wakame/template.rb', line 73

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