Sha256: df76ee4b282e8535ca7b8d626e21e1c28900d8a5778a4fc3af0ab59ef29ba5bb

Contents?: true

Size: 1.09 KB

Versions: 87

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8

require 'erb'

module Backup
  class Template

    # Holds a binding object. Nil if not provided.
    attr_accessor :binding

    ##
    # Creates a new instance of the Backup::Template class
    # and optionally takes an argument that can be either a binding object, a Hash or nil
    def initialize(object = nil)
      if object.is_a?(Binding)
        @binding = object
      elsif object.is_a?(Hash)
        @binding = Backup::Binder.new(object).get_binding
      else
        @binding = nil
      end
    end

    ##
    # Renders the provided file (in the context of the binding if any) to the console
    def render(file)
      puts result(file)
    end

    ##
    # Returns a String object containing the contents of the file (in the context of the binding if any)
    def result(file)
      ERB.new(file_contents(file), nil, '<>').result(binding)
    end

  private

    ##
    # Reads and returns the contents of the provided file path,
    # relative from the Backup::TEMPLATE_PATH
    def file_contents(file)
      File.read(File.join(Backup::TEMPLATE_PATH, file))
    end

  end
end

Version data entries

87 entries across 87 versions & 10 rubygems

Version Path
backup-ssh-4.4.5 lib/backup/template.rb
backup-remote-0.0.19 lib/backup/template.rb
cm-backup-1.0.0 lib/backup/template.rb
backup-ssh-4.4.4 lib/backup/template.rb
backup-ssh-4.4.3 lib/backup/template.rb
backup-4.4.1 lib/backup/template.rb
backup-remote-0.0.18 lib/backup/template.rb
backup-remote-0.0.17 lib/backup/template.rb
backup-remote-0.0.16 lib/backup/template.rb
backup-remote-0.0.15 lib/backup/template.rb
backup-ssh-4.4.2 lib/backup/template.rb
backup-ssh-4.4.1 lib/backup/template.rb
backup-remote-0.0.14 lib/backup/template.rb
backup-remote-0.0.13 lib/backup/template.rb
backup-remote-0.0.12 lib/backup/template.rb
backup-remote-0.0.11 lib/backup/template.rb
backup-ssh-4.4.0 lib/backup/template.rb
backup-remote-0.0.10 lib/backup/template.rb
backup-remote-0.0.9 lib/backup/template.rb
backup-remote-0.0.8 lib/backup/template.rb