Sha256: 3d56b7d3f298f577d62d76b9729c1a046346a5003b6678ec69b789740f5c158a

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'erb'
require 'fileutils'
require 'core_ext/string/camelize'

module GemRelease
  class Template
    include GemRelease::Helpers

    attr_reader :template, :name, :module_names, :module_path

    def initialize(template, options = {})
      @template = template

      options.each do |key, value|
        instance_variable_set(:"@#{key}", value)
        meta_class.send(:attr_reader, key)
      end

      @name         ||= gem_name_from_directory
      @module_path  ||= name
      @module_names ||= module_names_from_path(module_path)
    end

    def write
      FileUtils.mkdir_p(File.dirname(filename))
      File.open(filename, 'w+') { |f| f.write(render) }
    end

    def filename
      template
    end

    protected

      def module_names_from_path(path)
        names = []
        path.split('-').each do |segment|
          names << segment.camelize
        end
        names
      end

      def render
        ERB.new(read_template, nil, "%").result(binding)
      end

      def read_template
        File.new(File.expand_path("../templates/#{template}", __FILE__)).read
      end

      def meta_class
        class << self; self; end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gem-release-0.1.2 lib/gem_release/template.rb
gem-release-0.1.1 lib/gem_release/template.rb
gem-release-0.1.0 lib/gem_release/template.rb