Class: Texico::Template

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

Overview

Template

Class for handling Texico templates. A template is really just a folder with some files in it. The template object handles moving those files, as well as rendering any .erb files.

Defined Under Namespace

Classes: File, FileStatus

Constant Summary collapse

BASE_PATH =
::File.expand_path('../../../templates', __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/texico/template.rb', line 13

def name
  @name
end

Class Method Details

.exist?(_) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


49
50
51
52
# File 'lib/texico/template.rb', line 49

def exist?(_)
  raise RuntimeError
  #::File.exist? template
end

.listObject

List

List available templates



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

def list
  Dir.glob "#{BASE_PATH}/*"
end

.load(template) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/texico/template.rb', line 81

def load(template)
  file_tree     = load_file_tree template
  template_name = ::File.basename(template).capitalize

  new template_name, file_tree
rescue Errno::ENOENT
  false
end

.load_file_tree(root, current_dir = '') ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/texico/template.rb', line 54

def load_file_tree(root, current_dir = '')
  base_path = ::File.expand_path current_dir, root
  Dir.entries(base_path)
     .reject { |entry| ['.', '..'].include? entry }
     .map do |entry|
       local_path = (current_dir + entry).freeze
       full_path  = ::File.expand_path local_path, root

       if ::File.file?(full_path)
         File.new local_path, root
       else
         { entry.freeze => load_file_tree(root, local_path + '/') }.freeze
       end
     end.freeze
end

.map_tree(tree, root = '', &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/texico/template.rb', line 70

def map_tree(tree, root = '', &block)
  tree.map do |node|
    if node.is_a? Hash
      dir = node.keys[0]
      { dir => map_tree(node[dir], "#{root}#{dir}/", &block) }
    else
      yield node
    end
  end
end

Instance Method Details

#copy(dest, params, opts, &block) ⇒ Object

Returns a report of what files where copied.



23
24
25
26
27
28
29
30
# File 'lib/texico/template.rb', line 23

def copy(dest, params, opts, &block)
  map_status = block_given? ? block : ->(status) { status.to_s }
  status_tree =
    self.class.map_tree(@file_tree) do |file|
      map_status.call(file.copy(params, dest, opts))
    end
  { name => status_tree }
end

#treeObject

Tree

Returns the template structure in a format compatible with tty-tree.



18
19
20
# File 'lib/texico/template.rb', line 18

def tree
  { name => self.class.map_tree(@file_tree, &:to_s) }
end