Class: TTY::Templater

Inherits:
Object
  • Object
show all
Includes:
PathHelpers
Defined in:
lib/tty/templater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathHelpers

#name_from_path, #relative_path_from, #root_path, #within_root_path

Constructor Details

#initialize(source_path, target_path) ⇒ Templater

Returns a new instance of Templater.



13
14
15
16
17
# File 'lib/tty/templater.rb', line 13

def initialize(source_path, target_path)
  @source_path = templates_root_path.join(source_path)
  @target_path = target_path
  @templates = []
end

Instance Attribute Details

#templatesObject (readonly)

Returns the value of attribute templates.



11
12
13
# File 'lib/tty/templater.rb', line 11

def templates
  @templates
end

Instance Method Details

#add_mapping(source, target) ⇒ Object

Add mapping to templates

Parameters:

  • source (String)

    the source template location

  • target (String)

    the target template location



34
35
36
# File 'lib/tty/templater.rb', line 34

def add_mapping(source, target)
  @templates << [source, target]
end

#generate(template_options, color_option) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Process templates by injecting vars and moving to location



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tty/templater.rb', line 41

def generate(template_options, color_option)
  templates.each do |src, dst|
    source      = @source_path.join(src)
    destination = @target_path.join(dst).to_s
    next unless ::File.exist?(source)
    within_root_path do
      TTY::File.copy_file(source, destination,
                { context: template_options }.merge(color_option))
    end
  end
end

#templates_root_pathObject

The root path for all the templates



22
23
24
# File 'lib/tty/templater.rb', line 22

def templates_root_path
  Pathname(__dir__).join('templates')
end