Class: Minfra::Cli::Templater
- Inherits:
-
Object
- Object
- Minfra::Cli::Templater
show all
- Defined in:
- lib/minfra/cli/templater.rb
Overview
Defined Under Namespace
Classes: ERBPlus, TemplateBinding
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(template, helpers: []) ⇒ Templater
Returns a new instance of Templater.
65
66
67
68
69
70
|
# File 'lib/minfra/cli/templater.rb', line 65
def initialize(template, helpers: [])
@erb = ERBPlus.new(template)
@check_mode = false
@check_missing = []
@helpers = helpers
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/minfra/cli/templater.rb', line 93
def method_missing(name)
if @check_mode
@check_block&.call(name)
@check_missing << name
else
super
end
end
|
Class Method Details
.read(path, params: {}, helpers: [], fallback: nil) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/minfra/cli/templater.rb', line 27
def self.read(path, params: {}, helpers: [], fallback: nil)
p = Pathname.new(path)
if p.exist?
content = File.read(path)
else
raise "file #{path} not found" unless fallback
content = fallback
end
render(content, params, helpers: )
end
|
.render(template, params, helpers: []) ⇒ Object
40
41
42
|
# File 'lib/minfra/cli/templater.rb', line 40
def self.render(template, params, helpers: [])
new(template, helpers: ).render(params)
end
|
.template_dir(src, dst, extensions, helpers: [], params: {}) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/minfra/cli/templater.rb', line 44
def self.template_dir(src, dst, extensions, helpers: [], params: {})
destination = Pathname.new(dst)
destination.mkpath
source = Pathname.new(src)
source.glob('**/*') do |filename|
rel_path = filename.relative_path_from(source)
if File.directory?(filename) FileUtils.mkdir_p("#{destination}/#{rel_path}")
elsif extensions.include?(File.extname(filename)) content = File.read(filename)
modified_content = Minfra::Cli::Templater.render(content, params, helpers: )
File.write("#{destination}/#{rel_path}", modified_content)
else
FileUtils.cp(filename, destination.join(rel_path))
end
end
end
|
Instance Method Details
#check_missing(&block) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/minfra/cli/templater.rb', line 76
def check_missing(&block)
begin
@check_mode = true
@check_block = block
@check_missing = []
@erb.result(binding)
ensure
@check_block = nil
@check_mode = false
end
@check_missing
end
|
#missing? ⇒ Boolean
72
73
74
|
# File 'lib/minfra/cli/templater.rb', line 72
def missing?
!check_missing.empty?
end
|
#render(params) ⇒ Object
89
90
91
|
# File 'lib/minfra/cli/templater.rb', line 89
def render(params)
@erb.result_with(hash: params, helpers: @helpers)
end
|