Sha256: 8ab578a7f20352fe555ab8e1aae6975ffc99ccd4f1a4754cfd6d8584bc4d5fbe

Contents?: true

Size: 1.04 KB

Versions: 13

Compression:

Stored size: 1.04 KB

Contents

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)
      @binding =
        if object.is_a?(Binding)
          object
        elsif object.is_a?(Hash)
          Backup::Binder.new(object).get_binding
        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

13 entries across 13 versions & 4 rubygems

Version Path
backup-5.0.0.beta.3 lib/backup/template.rb
backedup-5.0.0.beta.5 lib/backup/template.rb
backedup-5.0.0.beta.4 lib/backup/template.rb
backedup-5.0.0.beta.3 lib/backup/template.rb
ext_backup-5.0.0.beta.2.1 lib/backup/template.rb
ext_backup-5.0.0.beta.2.ext.0.1.2 lib/backup/template.rb
ext_backup-5.0.0.beta.2.ext.0.1.1 lib/backup/template.rb
ext_backup-5.0.0.beta.2.ext.0.1.0 lib/backup/template.rb
backup-5.0.0.beta.2 lib/backup/template.rb
backup-5.0.0.beta.1 lib/backup/template.rb
backup-bouchard-4.4.4 lib/backup/template.rb
backup-bouchard-4.4.2 lib/backup/template.rb
backup-bouchard-4.4.1 lib/backup/template.rb