Class: Uniword::Resource::DocumentElementLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/resource/document_element_loader.rb

Overview

Loads document element templates from data/resources/document_elements/

Constant Summary collapse

DATA_DIR =
File.join(__dir__, "../../../data/resources/document_elements")

Class Method Summary collapse

Class Method Details

.available_categories(locale) ⇒ Array<String>

List available categories for a locale

Parameters:

  • locale (String)

    Locale code

Returns:

  • (Array<String>)

    Category slugs



33
34
35
36
37
38
39
40
# File 'lib/uniword/resource/document_element_loader.rb', line 33

def self.available_categories(locale)
  dir = File.join(DATA_DIR, locale)
  return [] unless Dir.exist?(dir)

  Dir.glob(File.join(dir, "*.yml"))
    .map { |p| File.basename(p, ".yml") }
    .sort
end

.available_localesArray<String>

List all available locales

Returns:

  • (Array<String>)

    Locale codes



45
46
47
48
49
50
51
# File 'lib/uniword/resource/document_element_loader.rb', line 45

def self.available_locales
  return [] unless Dir.exist?(DATA_DIR)

  Dir.glob(File.join(DATA_DIR, "*/"))
    .map { |p| File.basename(p) }
    .sort
end

.load(locale, category) ⇒ DocumentElementTemplate

Load templates for a specific locale and category

Parameters:

  • locale (String)

    Locale code (e.g., “en”, “ja”, “zh-CN”)

  • category (String)

    Category slug (e.g., “cover_pages”, “headers”)

Returns:

Raises:

  • (ArgumentError)

    if locale/category not found



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/uniword/resource/document_element_loader.rb', line 17

def self.load(locale, category)
  path = File.join(DATA_DIR, locale, "#{category}.yml")
  unless File.exist?(path)
    raise ArgumentError,
          "Document elements not found: #{locale}/#{category}. " \
          "Available locales: #{available_locales.join(', ')}"
  end

  data = YAML.load_file(path)
  DocumentElementTemplate.from_hash(data)
end