Class: Jaspion::Miya::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/jaspion/miya/template.rb

Overview

Represents a result file template that can be manipulated

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, path = nil) ⇒ Template

Initializes an Template object

> If path == nil, it tries to figure out the path by Object class name

Parameters:

  • object (Object) (defaults to: nil)

    Object object related to this template

  • path (String) (defaults to: nil)

    Source - template path



31
32
33
34
# File 'lib/jaspion/miya/template.rb', line 31

def initialize(object = nil, path = nil)
  @object    = object
  @file_path = (path || default_file_path)
end

Instance Attribute Details

#file_pathObject

Template file path



14
15
16
# File 'lib/jaspion/miya/template.rb', line 14

def file_path
  @file_path
end

#objectObject

Object object that template is related to



11
12
13
# File 'lib/jaspion/miya/template.rb', line 11

def object
  @object
end

Class Method Details

.dir_pathString

Returns all available classes

Returns:

  • (String)

    Directory path of /template



20
21
22
# File 'lib/jaspion/miya/template.rb', line 20

def dir_path
  File.expand_path(File.join(__FILE__, '..', '..', '..', 'templates'))
end

Instance Method Details

#default_file_pathObject



36
37
38
39
40
41
42
# File 'lib/jaspion/miya/template.rb', line 36

def default_file_path
  unless object.nil?
    path_components = object.class.name.split('::')[1..-1]
    path_components[0] = self.class.dir_path
    return File.join(*path_components.map(&:downcase)) + '.erb'
  end
end

#save(path) ⇒ Object



53
54
# File 'lib/jaspion/miya/template.rb', line 53

def save(path)
end

#source_codeString

Returns the parsed template code using ERUBIS The template file should be in /template directory

Returns:



48
49
50
51
# File 'lib/jaspion/miya/template.rb', line 48

def source_code
  return nil if @file_path.nil? || !File.file?(@file_path)
  Erubis::Eruby.new(File.read(@file_path)).evaluate(object)
end