Sha256: bb0ade104bf2ab502786d4a9556af7be732fe7ae3ff7906fc81f80a013f73afd

Contents?: true

Size: 1.83 KB

Versions: 24

Compression:

Stored size: 1.83 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
        context = generator.instance_eval 'binding'
        ERB.new(::File.read(source), nil, '-').result(context)
      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!
        callback(:before)
        ::FileUtils.mkdir_p(::File.dirname(destination))
        ::File.open(destination, 'w') {|f| f.write render }
        callback(:after)
      end
    
      # removes the destination file
      def revoke!
        ::FileUtils.rm(destination, :force => true)
      end
      
    end
  end
end

Version data entries

24 entries across 23 versions & 3 rubygems

Version Path
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.9.1/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.13.1 lib/middleman/vendor/gems/ruby/1.8/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.12.2 lib/middleman/vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.12.1 lib/middleman/vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
ginst-2009.12.8 vendor/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.12.0.pre3 lib/middleman/vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.12.0.pre2 lib/middleman/vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.12.0.pre lib/middleman/vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
ginst-2009.11.24 vendor/gems/templater-1.0.0/lib/templater/actions/template.rb
ginst-2009.11.23 vendor/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.10.17 vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.10.16 vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.10.15 vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
middleman-0.10.14 vendor/gems/gems/templater-1.0.0/lib/templater/actions/template.rb
ginst-2.0.1 vendor/gems/templater-1.0.0/lib/templater/actions/template.rb
ginst-2.0.0 vendor/gems/templater-1.0.0/lib/templater/actions/template.rb
templater-1.0.0 lib/templater/actions/template.rb
templater-0.4.1 lib/templater/actions/template.rb
templater-0.5.0 lib/templater/actions/template.rb
templater-0.4.4 lib/templater/actions/template.rb