Class: AppArchetype::Template::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/app_archetype/template/plan.rb

Overview

Plan builds an in memory representation of template output

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, variables, destination_path: nil, overwrite: false) ⇒ Plan

Creates a new plan from given source and variables.

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/app_archetype/template/plan.rb', line 18

def initialize(
  template,
  variables,
  destination_path: nil,
  overwrite: false
)
  @template = template
  @destination_path = destination_path
  @files = []
  @variables = variables
  @overwrite = overwrite
end

Instance Attribute Details

#destination_pathObject (readonly)

Returns the value of attribute destination_path.



8
9
10
# File 'lib/app_archetype/template/plan.rb', line 8

def destination_path
  @destination_path
end

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/app_archetype/template/plan.rb', line 8

def files
  @files
end

#templateObject (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/app_archetype/template/plan.rb', line 8

def template
  @template
end

#variablesObject (readonly)

Returns the value of attribute variables.



8
9
10
# File 'lib/app_archetype/template/plan.rb', line 8

def variables
  @variables
end

Instance Method Details

#destination_exist?Boolean

Check for whether the destination exists

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/app_archetype/template/plan.rb', line 66

def destination_exist?
  return false unless @destination_path

  File.exist?(
    File.dirname(@destination_path)
  )
end

#deviseObject

Devise builds an in memory representation of what needs to be done to render the template.

When the destination path does not exist - a RuntimeError is raised - however at this stage we should always have a destination path to render to.



39
40
41
42
43
44
45
46
47
48
# File 'lib/app_archetype/template/plan.rb', line 39

def devise
  raise 'destination path does not exist' unless destination_exist?

  @template.files.each do |file|
    @files << OutputFile.new(
      file,
      render_dest_file_path(file)
    )
  end
end

#executeObject

Execute will render the plan to disk



53
54
55
56
57
58
59
60
# File 'lib/app_archetype/template/plan.rb', line 53

def execute
  renderer = Renderer.new(
    self,
    @overwrite
  )

  renderer.render
end

#render_dest_file_path(source_path) ⇒ String

Determines what the destination file path is going to be by taking the source path, subbing the template path and then joining it with the specified destination path.

Calls render path to handle any handlebars moustaches included within the file name.

Parameters:

Returns:



85
86
87
88
89
90
91
# File 'lib/app_archetype/template/plan.rb', line 85

def render_dest_file_path(source_path)
  rel_path = render_path(
    source_path.gsub(@template.path, '')
  )

  File.join(@destination_path, rel_path)
end

#render_path(path) ⇒ String

Renders template variables into any moustaches included in the filename

This permits us to have variable file names as well as variable file content.

Parameters:

Returns:



103
104
105
106
# File 'lib/app_archetype/template/plan.rb', line 103

def render_path(path)
  hbs = Handlebars::Handlebars.new
  hbs.compile(path).call(@variables.to_h)
end