Sha256: 9c24c2551ae297a749568c5286da815bff53680de0869372697354e23ce9c927

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

module Vedeu

  module Templating

    # Generic class to loading a template and parsing it via ERb.
    #
    class Template

      # @param (see Vedeu::Templating::Template#new)
      # @return [void]
      def self.parse(object, path, options = {})
        new(object, path, options).parse
      end

      # Returns a new instance of Vedeu::Templating::Template.
      #
      # @param object [Class]
      # @param path [String]
      # @param options [Hash]
      # @option options name [String|Symbol] The name of an interface.
      # @return [Vedeu::Templating::Template]
      def initialize(object, path, options = {})
        @object  = object
        @path    = path.to_s
        @options = options || {}
      end

      # @return [Vedeu::Views::Stream]
      def parse
        ERB.new(load, nil, '-').result(binding)
      end

      protected

      # @!attribute [r] object
      # @return [Class]
      attr_reader :object

      # @!attribute [r] options
      # @return [Hash]
      attr_reader :options

      private

      # @return [String]
      def load
        File.read(path)
      end

      # @raise [Vedeu::Error::MissingRequired] When the path is empty
      #   or does not exist.
      # @return [String]
      def path
        fail Vedeu::Error::MissingRequired,
             'No path to template specified.' if @path.empty?

        unless File.exist?(@path)
          fail Vedeu::Error::MissingRequired,
               "Template file cannot be found. (#{@path})"
        end

        @path
      end

    end # Template

  end # Templating

end # Vedeu

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.6.25 lib/vedeu/templating/template.rb
vedeu-0.6.24 lib/vedeu/templating/template.rb
vedeu-0.6.23 lib/vedeu/templating/template.rb
vedeu-0.6.22 lib/vedeu/templating/template.rb
vedeu-0.6.21 lib/vedeu/templating/template.rb