Sha256: f106be3cdc6e7ee985a109970bbc0610bad163847daffba8518ccb17c0989aab

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module Templater
  
  class Proxy #:nodoc:
    
    def initialize(generator, name, source, destination, &block)
      @generator, @block, @source, @destination = generator, block, source, destination
      @name = name.to_sym
    end
    
    def source(*source)
      @source = ::File.join(*source)
    end
    
    def destination(*dest)
      @destination = ::File.join(*dest)
    end
    
    def to_template
      instance_eval(&@block) if @block
      Templater::Template.new(@generator, @name, get_source, get_destination)
    end
    
    def to_file
      instance_eval(&@block) if @block
      Templater::File.new(@name, get_source, get_destination)
    end

    def to_empty_directory
      instance_eval(&@block) if @block
      Templater::EmptyDirectory.new(@name, get_destination)
    end
    
    def method_missing(method, *args, &block)
      if @generator
        @generator.send(method, *args, &block)
      else
        super
      end
    end
    
    protected
    
    def get_source
      ::File.expand_path(@source.to_s, @generator.source_root)
    end
    
    def get_destination
      ::File.expand_path(convert_encoded_instructions(@destination.to_s), @generator.destination_root)
    end
    
    def convert_encoded_instructions(filename)
      filename.gsub(/%.*?%/) do |string|
        instruction = string.match(/%(.*?)%/)[1]
        @generator.respond_to?(instruction) ? @generator.send(instruction) : string
      end
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
templater-0.1.2 lib/templater/proxy.rb
templater-0.1.3 lib/templater/proxy.rb