Class: Vedeu::Templating::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/templating/template.rb

Overview

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

Direct Known Subclasses

ViewTemplate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, path, options = {}) ⇒ Vedeu::Templating::Template

Returns a new instance of Vedeu::Templating::Template.

Parameters:

  • object (Class)
  • path (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • name (String|Symbol)

    The name of an interface.



24
25
26
27
28
# File 'lib/vedeu/templating/template.rb', line 24

def initialize(object, path, options = {})
  @object  = object
  @path    = path.to_s
  @options = options || {}
end

Instance Attribute Details

#objectClass (readonly, protected)

Returns:

  • (Class)


39
40
41
# File 'lib/vedeu/templating/template.rb', line 39

def object
  @object
end

#optionsHash (readonly, protected)

Returns:

  • (Hash)


43
44
45
# File 'lib/vedeu/templating/template.rb', line 43

def options
  @options
end

Class Method Details

.parse(object, path, options = {}) ⇒ Vedeu::Views::Stream

Parameters:

  • object (Class)
  • path (String)
  • options (Hash) (defaults to: {})

Returns:



13
14
15
# File 'lib/vedeu/templating/template.rb', line 13

def self.parse(object, path, options = {})
  new(object, path, options).parse
end

Instance Method Details

#loadString (private)

Returns:

  • (String)


48
49
50
# File 'lib/vedeu/templating/template.rb', line 48

def load
  File.read(path)
end

#parseVedeu::Views::Stream



31
32
33
# File 'lib/vedeu/templating/template.rb', line 31

def parse
  ERB.new(load, nil, '-').result(binding)
end

#pathString (private)

Returns:

  • (String)

Raises:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vedeu/templating/template.rb', line 54

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