Class: SmartPrompt::PromptTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_prompt/prompt_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file) ⇒ PromptTemplate

Returns a new instance of PromptTemplate.



5
6
7
8
# File 'lib/smart_prompt/prompt_template.rb', line 5

def initialize(template_file)
  @template_file = template_file
  @template = File.read(template_file)
end

Class Method Details

.create(name, content) ⇒ Object



38
39
40
41
# File 'lib/smart_prompt/prompt_template.rb', line 38

def create(name, content)
  File.write(File.join(template_dir, "#{name}.erb"), content)
  new(File.join(template_dir, "#{name}.erb"))
end

.load_templates(template_dir) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/smart_prompt/prompt_template.rb', line 29

def load_templates(template_dir)
  templates = {}
  Dir.glob(File.join(template_dir, '*.erb')).each do |file|
    name = File.basename(file, '.erb')
    templates[name] = new(file)
  end
  templates
end

.template_dirObject



43
44
45
# File 'lib/smart_prompt/prompt_template.rb', line 43

def template_dir
  @template_dir ||= 'templates'
end

.template_dir=(dir) ⇒ Object



47
48
49
# File 'lib/smart_prompt/prompt_template.rb', line 47

def template_dir=(dir)
  @template_dir = dir
end

Instance Method Details

#reloadObject



14
15
16
# File 'lib/smart_prompt/prompt_template.rb', line 14

def reload
  @template = File.read(@template_file)
end

#render(params = {}) ⇒ Object



10
11
12
# File 'lib/smart_prompt/prompt_template.rb', line 10

def render(params = {})
  ERB.new(@template, trim_mode: '-').result(binding_with_params(params))
end