Sha256: a919535025b0920ed3cb11421b2be849ad6284206ec8645009429b62c98b8be9

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

module Wellcar
  module Templates
    class Base
      include ERB::Util

      def initialize(file_path)
        @file_path = file_path
        @before_write = []
      end

      def write
        @before_write.each {|procedure| procedure.call }

        File.open(file_path, 'w') {|file| file.write render }
      end

      def exist?
        File.exist? file_path
      end

      def render
        ERB.new(template).result(binding)
      end

      private
      
      def file_path
        raise NotImplementedError, "You have not assigned a value to @file_path" if @file_path.nil?
        @file_path
      end

      def template
        raise NotImplementedError, "You have not set the template filename. Use with_template in your concrete implementation." if @template_filename.empty?
        
        @template ||= File.read(File.join(File.dirname(__FILE__), @template_filename))
      end

      def with_template(filename)
        @template_filename = filename
      end
      
      def with_attributes(attributes)
        attributes.each do |pair|
          name = pair.first
          value = pair.last
          self.define_singleton_method(name.to_sym) { return value }
        end
      end

      def within(path)
        @before_write << Proc.new do
          @file_path = File.join path, @file_path

          next if Dir.exist? path

          FileUtils.mkdir_p path
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wellcar-0.0.3 lib/wellcar/templates/base.rb
wellcar-0.0.2 lib/wellcar/templates/base.rb
wellcar-0.0.1 lib/wellcar/templates/base.rb