class Risu::Base::TemplateManager

Attributes

registered_templates[RW]

Public Class Methods

new(path) click to toggle source

Creates new instance of TemplateManager

@param path Path relative to the base_dir of risu

@return New instance of the template manager with templates loaded.

# 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

Public Instance Methods

display_templates() click to toggle source

Displays a list of all the templates to STDOUT

# 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) click to toggle source

Finds a template by its name

@param name Name of the template to find

@return the instance of the template or nil if not found

# 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) click to toggle source

Loads templates from a specific path

@param path Path to templates to load

# 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) click to toggle source

Validates that a template is a valid template

@TODO look at refactoring this to valid?(template)

@param template The template to validate

@return [Boolean] If the template is valid

# 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