Sha256: 83835b0a9fc95323b335d960fc2c2c3667c7c3a4256f3b592b84b293c47eecd2

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'contracts'
require 'tilt/erubis'

module QuickStart
  # Templating for QuickStart
  class Template
    include ::Contracts::Core
    include ::Contracts::Builtin

    attr_accessor :path

    Contract String => Any
    def initialize(name)
      @name = name
      fail LoadError, "#{path} does not exist" unless Dir.exist? path
    end

    Contract None => String
    def path
      "#{root}/#{@name}"
    end

    Contract None => String
    def root
      File.expand_path ENV.fetch('QUICKSTART_TEMPLATES', './templates')
    end

    Contract None => ArrayOf[String]
    def directories
      Dir.glob("#{path}/**/*/").map do |directory|
        directory.tap { |d| d.slice! %r{^#{Regexp.quote path}/} }
      end
    end

    Contract None => ArrayOf[String]
    def files
      Dir.glob("#{path}/**/*").map do |file|
        file.tap { |f| f.slice! %r{^#{Regexp.quote path}/} }
      end
    end

    Contract String, Maybe[Hash] => String
    def render(name, overrides = {})
      template = "#{path}/#{name}"
      Tilt::ErubisTemplate.new(template).render(Object.new, overrides)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quickstart-0.8.11 lib/quickstart/template.rb
quickstart-0.8.2 lib/quickstart/template.rb