Class: Texico::Template::File

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

Overview

File

The Template File object is an internal object used by the Template class to copy and render template files transparently. It should never be used directly.

Constant Summary collapse

TEMPLATE_EXTNAME =
'.erb'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, base_path) ⇒ File

Returns a new instance of File.



13
14
15
16
17
18
# File 'lib/texico/template/file.rb', line 13

def initialize(relative_path, base_path)
  @relative_path = relative_path.freeze
  @base_path     = base_path.freeze

  freeze
end

Instance Method Details

#basenameObject

Basename

Returns the name of the file. In case of template files the .erb extension is removed.



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

def basename
  ::File.basename @relative_path, TEMPLATE_EXTNAME
end

#copy(params, dest_base_path = '.', opts = {}) ⇒ Object

Copy

Copy the file with its relative path intact to the dest_base_path root.

Returns a FileStatus object.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/texico/template/file.rb', line 53

def copy(params, dest_base_path = '.', opts = {})
  dest_dir  = ::File.expand_path dirname, dest_base_path
  dest_path = ::File.expand_path basename, dest_dir
  force     = false

  if ::File.exist? dest_path
    return FileStatus.new(self, :target_exist) unless opts[:force]
    force = true
  else
    FileUtils.mkdir_p dest_dir unless opts[:dry_run]
  end

  if template?
    err = copy_template src_path, dest_path, params,
                        noop: opts[:dry_run], verbose: opts[:verbose]
    return err if err
  else
    FileUtils.cp src_path, dest_path,
                 noop: opts[:dry_run], verbose: opts[:verbose]
  end

  FileStatus.new(self, force ? :replaced_target : :successful)
end

#dirnameObject

Dirname

Returns the local directory path of the file.



39
40
41
# File 'lib/texico/template/file.rb', line 39

def dirname
  ::File.dirname @relative_path
end

#extnameObject

Basename

Returns the extension of the file. In case of template files the extension of the target file is returned.



32
33
34
# File 'lib/texico/template/file.rb', line 32

def extname
  ::File.extname basename
end

#to_sObject

Returns the filename



44
45
46
# File 'lib/texico/template/file.rb', line 44

def to_s
  basename
end