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.



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

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.



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

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



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

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



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

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



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

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