Sha256: 90855e7579ca2a2c3c34d030b6422989e76c1456ec4e78f66d6fcc2f928d1321

Contents?: true

Size: 1.16 KB

Versions: 22

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, :filename, :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

      @filename     ||= @template
      @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

    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

22 entries across 22 versions & 1 rubygems

Version Path
gem-release-0.2.0 lib/gem_release/template.rb
gem-release-0.1.3 lib/gem_release/template.rb