Class: Templater::Actions::File

Inherits:
Action
  • Object
show all
Defined in:
lib/templater/actions/file.rb

Direct Known Subclasses

Directory

Instance Attribute Summary

Attributes inherited from Action

#destination, #generator, #name, #options, #source

Instance Method Summary collapse

Methods inherited from Action

#relative_destination

Constructor Details

#initialize(generator, name, source, destination, options = {}) ⇒ File

Builds a new file.

Parameters

generator

The generator

name

The name of this file

source

Full path to the source of this file

destination

Full path to the destination of this file

options<HashSymbol=>Symbol

Options, including callbacks.



13
14
15
16
17
18
19
# File 'lib/templater/actions/file.rb', line 13

def initialize(generator, name, source, destination, options={})
  self.generator = generator
  self.name = name
  self.source = source
  self.destination = destination
  self.options = options
end

Instance Method Details

#exists?Boolean

Checks if the destination file already exists.

Returns

Boolean

true if the file exists, false otherwise.

Returns:

  • (Boolean)


33
34
35
# File 'lib/templater/actions/file.rb', line 33

def exists?
  ::File.exists?(destination)
end

#identical?Boolean

Checks if the content of the file at the destination is identical to the rendered result.

Returns

Boolean

true if it is identical, false otherwise.

Returns:

  • (Boolean)


41
42
43
# File 'lib/templater/actions/file.rb', line 41

def identical?
  exists? && ::FileUtils.identical?(source, destination)
end

#invoke!Object

Renders the template and copies it to the destination.



46
47
48
49
50
51
# File 'lib/templater/actions/file.rb', line 46

def invoke!
  callback(:before)
  ::FileUtils.mkdir_p(::File.dirname(destination))
  ::FileUtils.cp_r(source, destination)
  callback(:after)
end

#renderObject

Returns the contents of the source file as a String

Returns

String

The source file.



25
26
27
# File 'lib/templater/actions/file.rb', line 25

def render
  ::File.read(source)
end

#revoke!Object

removes the destination file



54
55
56
# File 'lib/templater/actions/file.rb', line 54

def revoke!
  ::FileUtils.rm_r(destination, :force => true)
end