Class: TouchErb::TemplateDir

Inherits:
Object
  • Object
show all
Defined in:
lib/touch_erb/template_dir.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_dir = ENV['TOUCH_ERB_ROOT'] || File.join(Dir.home, '.touch_erb'), create_dir = true) ⇒ TemplateDir

Returns a new instance of TemplateDir.



5
6
7
8
9
10
11
# File 'lib/touch_erb/template_dir.rb', line 5

def initialize(
  root_dir = ENV['TOUCH_ERB_ROOT'] || File.join(Dir.home, '.touch_erb'),
  create_dir = true
)
  FileUtils.mkdir_p(root_dir) if create_dir &&  !Dir.exist?(root_dir)
  @root =  root_dir
end

Instance Method Details

#add(template_name) ⇒ Object



22
23
24
25
26
# File 'lib/touch_erb/template_dir.rb', line 22

def add(template_name)
  absolute_path = File.join(@root, "#{template_name}.erb")
  FileUtils.touch(absolute_path)
  absolute_path
end

#find(template_name) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/touch_erb/template_dir.rb', line 13

def find(template_name)
  absolute_path = "#{File.join(@root, template_name)}.erb"
  if File.exist?(absolute_path)
    File.read(absolute_path)
  else
    nil
  end
end

#listObject



28
29
30
# File 'lib/touch_erb/template_dir.rb', line 28

def list()
  Dir.glob("*.erb", base: @root).map { |filename| File.basename(filename, '.erb') }
end