Class: Risu::Base::TemplateManager
- Inherits:
-
Object
- Object
- Risu::Base::TemplateManager
- Defined in:
- lib/risu/base/template_manager.rb
Instance Attribute Summary collapse
-
#registered_templates ⇒ Object
Returns the value of attribute registered_templates.
Instance Method Summary collapse
-
#display_templates ⇒ Object
Displays a list of all the templates to STDOUT.
-
#find_template_by_name(name) ⇒ Object
Finds a template by its name.
-
#initialize(path) ⇒ Object
constructor
Creates new instance of TemplateManager.
-
#load_templates(path, recursive = true) ⇒ Object
Loads templates from a specific path.
-
#validate(template) ⇒ Boolean
Validates that a template is a valid template.
Constructor Details
#initialize(path) ⇒ Object
Creates new instance of TemplateManager
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.(USER_TEMPLATES_DIR)) if File.exist?(File.(USER_TEMPLATES_DIR)) && File.directory?(File.(USER_TEMPLATES_DIR)) end |
Instance Attribute Details
#registered_templates ⇒ Object
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_templates ⇒ Object
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
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
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
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 |