Class: Coradoc::Html::TemplateConfig
- Inherits:
-
Object
- Object
- Coradoc::Html::TemplateConfig
- Defined in:
- lib/coradoc/html/template_config.rb
Overview
Configuration for the Liquid template system
This class manages template directories and provides utilities for template discovery and customization.
Constant Summary collapse
- DEFAULT_TEMPLATE_DIR =
Default template directory within the gem
Pathname.new(File.join( File.dirname(__FILE__), 'templates', 'core_model' )).freeze
Instance Attribute Summary collapse
-
#template_dirs ⇒ Array<Pathname>
List of user-provided template directories.
Class Method Summary collapse
-
.available_templates ⇒ Array<Symbol>
List all available default templates.
-
.template_path_for(name) ⇒ Pathname?
Get the path to a specific default template.
Instance Method Summary collapse
-
#all_template_dirs ⇒ Array<Pathname>
Get all template directories (user + default).
-
#find_template(name) ⇒ Pathname?
Find a template by name.
-
#initialize(template_dirs: []) ⇒ TemplateConfig
constructor
Initialize a new configuration.
-
#reset! ⇒ void
Reset configuration to defaults.
-
#template_exists?(name) ⇒ Boolean
Check if a template exists.
-
#with_dirs(additional_dirs) ⇒ TemplateConfig
Create a copy of this configuration with additional directories.
Constructor Details
#initialize(template_dirs: []) ⇒ TemplateConfig
Initialize a new configuration
32 33 34 |
# File 'lib/coradoc/html/template_config.rb', line 32 def initialize(template_dirs: []) @template_dirs = Array(template_dirs).map { |dir| Pathname.new(dir) } end |
Instance Attribute Details
#template_dirs ⇒ Array<Pathname>
Returns List of user-provided template directories.
27 28 29 |
# File 'lib/coradoc/html/template_config.rb', line 27 def template_dirs @template_dirs end |
Class Method Details
.available_templates ⇒ Array<Symbol>
List all available default templates
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/coradoc/html/template_config.rb', line 46 def self.available_templates @available_templates ||= begin return [] unless DEFAULT_TEMPLATE_DIR.exist? DEFAULT_TEMPLATE_DIR .glob('*.liquid') .map { |f| f.basename('.liquid').to_s.to_sym } .sort end end |
.template_path_for(name) ⇒ Pathname?
Get the path to a specific default template
61 62 63 64 |
# File 'lib/coradoc/html/template_config.rb', line 61 def self.template_path_for(name) path = DEFAULT_TEMPLATE_DIR.join("#{name}.liquid") path.exist? ? path : nil end |
Instance Method Details
#all_template_dirs ⇒ Array<Pathname>
Get all template directories (user + default)
39 40 41 |
# File 'lib/coradoc/html/template_config.rb', line 39 def all_template_dirs @template_dirs + [DEFAULT_TEMPLATE_DIR] end |
#find_template(name) ⇒ Pathname?
Find a template by name
80 81 82 83 84 85 86 |
# File 'lib/coradoc/html/template_config.rb', line 80 def find_template(name) all_template_dirs.each do |dir| path = dir.join("#{name}.liquid") return path if path.exist? end nil end |
#reset! ⇒ void
This method returns an undefined value.
Reset configuration to defaults
91 92 93 |
# File 'lib/coradoc/html/template_config.rb', line 91 def reset! @template_dirs = [] end |
#template_exists?(name) ⇒ Boolean
Check if a template exists
70 71 72 73 74 |
# File 'lib/coradoc/html/template_config.rb', line 70 def template_exists?(name) all_template_dirs.any? do |dir| dir.join("#{name}.liquid").exist? end end |
#with_dirs(additional_dirs) ⇒ TemplateConfig
Create a copy of this configuration with additional directories
99 100 101 102 103 |
# File 'lib/coradoc/html/template_config.rb', line 99 def with_dirs(additional_dirs) self.class.new( template_dirs: @template_dirs + Array(additional_dirs).map { |d| Pathname.new(d) } ) end |