Sha256: 74c9070980c7bd2bcc54fa9363475c883391f09755a125fc40fb5e00c88e4778

Contents?: true

Size: 770 Bytes

Versions: 6

Compression:

Stored size: 770 Bytes

Contents

require 'fileutils'

module TouchErb
  class TemplateDir
    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

    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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
touch_erb-0.4.3 lib/touch_erb/template_dir.rb
touch_erb-0.4.2 lib/touch_erb/template_dir.rb
touch_erb-0.4.1 lib/touch_erb/template_dir.rb
touch_erb-0.4.0 lib/touch_erb/template_dir.rb
touch_erb-0.3.1 lib/touch_erb/template_dir.rb
touch_erb-0.3.0 lib/touch_erb/template_dir.rb