Sha256: f492b1e735dcbe673563ab9cb1148dc2ea81de9d4f7732a5b5523924746e7427

Contents?: true

Size: 1.51 KB

Versions: 11

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Templating

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

      # @param (see #initialize)
      # @return (see #parse)
      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

      # @macro raise_missing_required
      # @return [String]
      def path
        raise Vedeu::Error::MissingRequired,
              'No path to template specified.' if @path.empty?

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

        @path
      end

    end # Template

  end # Templating

end # Vedeu

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vedeu-0.8.23 lib/vedeu/templating/template.rb
vedeu-0.8.22 lib/vedeu/templating/template.rb
vedeu-0.8.21 lib/vedeu/templating/template.rb
vedeu-0.8.20 lib/vedeu/templating/template.rb
vedeu-0.8.19 lib/vedeu/templating/template.rb
vedeu-0.8.18 lib/vedeu/templating/template.rb
vedeu-0.8.17 lib/vedeu/templating/template.rb
vedeu-0.8.16 lib/vedeu/templating/template.rb
vedeu-0.8.15 lib/vedeu/templating/template.rb
vedeu-0.8.14 lib/vedeu/templating/template.rb
vedeu-0.8.13 lib/vedeu/templating/template.rb