Sha256: 41e9d0c34d5c1fa11a2f897d0a89143243ef9af091429f06a503dace26abf664

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Templater
  module Actions
    class Template < Action
  
      # Builds a new template.
      #
      # === Parameters
      # generator<Object>:: Context for rendering
      # name<Symbol>:: The name of this template
      # source<String>:: Full path to the source of this template
      # destination<String>:: Full path to the destination of this template
      # options<Hash{Symbol=>Symbol}:: Options, including callbacks.
      def initialize(generator, name, source, destination, options={})
        self.generator = generator
        self.name = name
        self.source = source
        self.destination = destination
        self.options = options
      end
  
      # Renders the template using ERB and returns the result as a String.
      #
      # === Returns
      # String:: The rendered template.
      def render
        ERB.new(::File.read(source), nil, '-').result(generator.send(:binding))
      end

      # Checks if the destination file already exists.
      #
      # === Returns
      # Boolean:: true if the file exists, false otherwise.
      def exists?
        ::File.exists?(destination)
      end
  
      # Checks if the content of the file at the destination is identical to the rendered result.
      # 
      # === Returns
      # Boolean:: true if it is identical, false otherwise.
      def identical?
        ::File.read(destination) == render if ::File.exists?(destination)
      end
  
      # Renders the template and copies it to the destination.
      def invoke!
        @generator.send(@options[:before], self) if @options[:before]
        ::FileUtils.mkdir_p(::File.dirname(destination))
        ::File.open(destination, 'w') {|f| f.write render }
        @generator.send(@options[:after], self) if @options[:after]
      end
    
      # removes the destination file
      def revoke!
        ::FileUtils.rm(destination, :force => true)
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
templater-0.2 lib/templater/actions/template.rb