Class: Coradoc::Html::TemplateLocator
- Inherits:
-
Object
- Object
- Coradoc::Html::TemplateLocator
- Defined in:
- lib/coradoc/html/template_locator.rb
Overview
Locates Liquid templates from filesystem with fallback support
This class implements a template lookup algorithm:
-
Check user’s template directories in order (first match wins)
-
Fall back to default templates if not found
-
Return nil if no template found (caller decides fallback)
Constant Summary collapse
- CORE_MODEL_DIR =
Default template subdirectory within each template root
'core_model'
Instance Attribute Summary collapse
-
#default_dir ⇒ Object
readonly
Returns the value of attribute default_dir.
-
#user_dirs ⇒ Object
readonly
Returns the value of attribute user_dirs.
Instance Method Summary collapse
-
#available_templates ⇒ Array<String>
Get all available template types.
-
#clear_cache ⇒ Object
Clear the template cache (useful when template directories change).
-
#default_template_dir ⇒ Pathname
Get the default template directory path.
-
#exists?(type_name) ⇒ Boolean
Check if a template exists (without loading it).
-
#find(type_name) ⇒ Pathname?
Find a template by type name.
-
#initialize(user_dirs: [], default_dir: nil) ⇒ TemplateLocator
constructor
Initialize the locator.
Constructor Details
#initialize(user_dirs: [], default_dir: nil) ⇒ TemplateLocator
Initialize the locator
32 33 34 35 36 |
# File 'lib/coradoc/html/template_locator.rb', line 32 def initialize(user_dirs: [], default_dir: nil) @user_dirs = Array(user_dirs).map { |d| Pathname.new(d) } @default_dir = default_dir ? Pathname.new(default_dir) : default_template_dir @cache = {} end |
Instance Attribute Details
#default_dir ⇒ Object (readonly)
Returns the value of attribute default_dir.
26 27 28 |
# File 'lib/coradoc/html/template_locator.rb', line 26 def default_dir @default_dir end |
#user_dirs ⇒ Object (readonly)
Returns the value of attribute user_dirs.
26 27 28 |
# File 'lib/coradoc/html/template_locator.rb', line 26 def user_dirs @user_dirs end |
Instance Method Details
#available_templates ⇒ Array<String>
Get all available template types
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/coradoc/html/template_locator.rb', line 78 def available_templates types = Set.new # Collect from user directories @user_dirs.each do |dir| core_model_path = dir / CORE_MODEL_DIR next unless core_model_path.exist? && core_model_path.directory? core_model_path.glob('*.liquid') do |f| types.add(f.basename('.liquid').to_s) end end # Collect from default directory if @default_dir&.exist? && @default_dir.directory? @default_dir.glob('*.liquid') do |f| types.add(f.basename('.liquid').to_s) end end types.to_a.sort end |
#clear_cache ⇒ Object
Clear the template cache (useful when template directories change)
109 110 111 |
# File 'lib/coradoc/html/template_locator.rb', line 109 def clear_cache @cache.clear end |
#default_template_dir ⇒ Pathname
Get the default template directory path
104 105 106 |
# File 'lib/coradoc/html/template_locator.rb', line 104 def default_template_dir Pathname.new(File.join(File.dirname(__FILE__), 'templates', CORE_MODEL_DIR)) end |
#exists?(type_name) ⇒ Boolean
Check if a template exists (without loading it)
71 72 73 |
# File 'lib/coradoc/html/template_locator.rb', line 71 def exists?(type_name) !find(type_name).nil? end |
#find(type_name) ⇒ Pathname?
Find a template by type name
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/coradoc/html/template_locator.rb', line 42 def find(type_name) return @cache[type_name] if @cache.key?(type_name) # First check user directories @user_dirs.each do |dir| template_path = dir / CORE_MODEL_DIR / "#{type_name}.liquid" if template_path.exist? @cache[type_name] = template_path return template_path end end # Then check default directory if @default_dir template_path = @default_dir / "#{type_name}.liquid" if template_path.exist? @cache[type_name] = template_path return template_path end end @cache[type_name] = nil nil end |