Class: Risu::Base::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/risu/base/template_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object

Creates new instance of TemplateManager

Parameters:

  • path

    Path relative to the base_dir of risu



33
34
35
36
37
38
39
40
41
42
# File 'lib/risu/base/template_manager.rb', line 33

def initialize path
	@registered_templates = Array.new
	@templates = Array.new

	base_dir = __FILE__.gsub("risu/base/template_manager.rb", "")

	load_templates(base_dir + path)
	load_templates(Dir.pwd, false)
	load_templates(File.expand_path(USER_TEMPLATES_DIR)) if File.exist?(File.expand_path(USER_TEMPLATES_DIR)) && File.directory?(File.expand_path(USER_TEMPLATES_DIR))
end

Instance Attribute Details

#registered_templatesObject

Returns the value of attribute registered_templates.



26
27
28
# File 'lib/risu/base/template_manager.rb', line 26

def registered_templates
  @registered_templates
end

Instance Method Details

#display_templatesObject

Displays a list of all the templates to STDOUT



101
102
103
104
105
106
107
108
# File 'lib/risu/base/template_manager.rb', line 101

def display_templates
	puts "Available Templates"

	@registered_templates.each do |x|
		p = x.new
		puts "\t#{p.template_info[:name]} - #{p.template_info[:description]}\n"
	end
end

#find_template_by_name(name) ⇒ Object

Finds a template by its name

Parameters:

  • name

    Name of the template to find

Returns:

  • the instance of the template or nil if not found



89
90
91
92
93
94
95
96
97
98
# File 'lib/risu/base/template_manager.rb', line 89

def find_template_by_name name
	@registered_templates.each do |template|
		t = template.new
		if t.template_info[:name] == name
			return t
		end
	end

	return nil
end

#load_templates(path, recursive = true) ⇒ Object

Loads templates from a specific path

Parameters:

  • path

    Path to templates to load



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/risu/base/template_manager.rb', line 47

def load_templates path, recursive=true
	begin
		search_path = "#{path}/**/*.rb" if recursive == true
		search_path = "#{path}/*.rb" if recursive == false

		Dir[search_path].each do |x|
			begin
				require x
			rescue
				next
			end
		end

		TemplateBase.possible_templates.each do |p|
			if validate(p) ==  true
				@registered_templates << p if @registered_templates.include?(p) == false
			end
		end
	rescue
		puts "[!] Invalid template path"
	end
end

#validate(template) ⇒ Boolean

Validates that a template is a valid template

Parameters:

  • template

    The template to validate

Returns:

  • (Boolean)

    If the template is valid



77
78
79
80
81
82
# File 'lib/risu/base/template_manager.rb', line 77

def validate template
  t = template.new

	return false if t == nil
  return t.respond_to?(:render)
end