Class: Pkgr::Templates::FileTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/pkgr/templates/file_template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, source, opts = {}) ⇒ FileTemplate

Returns a new instance of FileTemplate.



8
9
10
11
12
# File 'lib/pkgr/templates/file_template.rb', line 8

def initialize(target, source, opts = {})
  @target = target
  @source = source
  @opts = opts
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/pkgr/templates/file_template.rb', line 6

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/pkgr/templates/file_template.rb', line 6

def target
  @target
end

Instance Method Details

#install(template_binding) ⇒ Object

Assumes that the current working directory is correct



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pkgr/templates/file_template.rb', line 15

def install(template_binding)
  make_dir
  File.open(target, "w+") do |f|
    if source.respond_to?(:path) && File.extname(source) == ".erb"
      f << ERB.new(source.read).result(template_binding)
    else
      f << source.read
    end
  end

  FileUtils.chmod mode, target
end