Sha256: e928e2cb95384a9462e874b1717140ffcd000d44847a6423f4f3a8257ea543c2

Contents?: true

Size: 1.6 KB

Versions: 16

Compression:

Stored size: 1.6 KB

Contents

module Mccloud
  class Template
    attr_accessor :file
    attr_accessor :erb
    attr_accessor :params
    attr_accessor :author
    attr_accessor :bootstrap

    attr_accessor :name
    attr_accessor :env

    def initialize(name,env)
      @name=name
      @env=env

      @erb=true
      @file=nil
      @params=Hash.new
      @author="No author specified"
      @bootstrap=nil
    end

    def path
      File.join(@env.config.templates.path,@name)
    end

    def exists?
      File.directory?(self.path)
    end

    # Bindings in ERB http://www.stuartellis.eu/articles/erb/
    # Links:
    # * Trimming whitespace in ERB
    # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/242656
    # * Appending to ERB output
    # http://blog.jayfields.com/2007/01/appending-to-erb-output-from-block.html
    def comment(flag)
      cur_pos=@output.length
      yield
      new_pos=@output.length
      if flag

        # Extraxt the block
        block_text=@output[cur_pos..new_pos]

        # Remove the block
        @output[cur_pos..new_pos]=''

        # Comment the block, with leading spaces into account
        block_text.gsub!(/^(\s)*/,'\1# ')

        # Re-insert the block
        @output=@output.insert cur_pos, block_text
      end

    end

    def to_s
      "Template #{name}"
    end

    def to_template(vm_name="noname")
      result=""
      filename=@file
      env.logger.info "Opening template file #{@file}"
      if File.exists?(filename)
        template=File.new(filename).read
        result=ERB.new(template,nil,"-","@output").result(binding)
      end
      return result
    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mccloud-0.1.1 lib/mccloud/template.rb
mccloud-0.0.28 lib/mccloud/template.rb
mccloud-0.0.27 lib/mccloud/template.rb
mccloud-0.0.26 lib/mccloud/template.rb
mccloud-0.0.25 lib/mccloud/template.rb
mccloud-0.0.24 lib/mccloud/template.rb
mccloud-0.0.23 lib/mccloud/template.rb
mccloud-0.0.22 lib/mccloud/template.rb
mccloud-0.0.21 lib/mccloud/template.rb
mccloud-0.0.20 lib/mccloud/template.rb
mccloud-0.0.19 lib/mccloud/template.rb
mccloud-0.0.18 lib/mccloud/template.rb
mccloud-0.0.17 lib/mccloud/template.rb
mccloud-0.0.16 lib/mccloud/template.rb
mccloud-0.0.15 lib/mccloud/template.rb
mccloud-0.0.14 lib/mccloud/template.rb