Class: Uniword::Schema::SchemaLoader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/uniword/schema/schema_loader.rb

Overview

SchemaLoader loads and caches OOXML schema definitions from YAML files

This class follows the Singleton pattern to ensure schemas are loaded once and reused throughout the application lifecycle.

Examples:

Load a schema

loader = SchemaLoader.instance
wordprocessingml = loader.load_schema('wordprocessingml')
element_def = loader.element_definition('wordprocessingml', 'p')

Instance Method Summary collapse

Constructor Details

#initializeSchemaLoader

Returns a new instance of SchemaLoader.



20
21
22
23
# File 'lib/uniword/schema/schema_loader.rb', line 20

def initialize
  @schemas = {}
  @schema_path = File.join(__dir__, "../../../config/ooxml/schemas")
end

Instance Method Details

#available_schemasArray<String>

Get all available schemas

Returns:

  • (Array<String>)

    Schema names



80
81
82
83
84
# File 'lib/uniword/schema/schema_loader.rb', line 80

def available_schemas
  Dir.glob(File.join(@schema_path, "*.yml")).map do |file|
    File.basename(file, ".yml")
  end
end

#clear_cache!void

This method returns an undefined value.

Clear cached schemas (useful for testing)



73
74
75
# File 'lib/uniword/schema/schema_loader.rb', line 73

def clear_cache!
  @schemas.clear
end

#element_definition(schema_name, element_name) ⇒ Hash?

Get element definition from a schema

Parameters:

  • schema_name (String)

    Schema name

  • element_name (String)

    Element XML tag name

Returns:

  • (Hash, nil)

    Element definition or nil if not found



47
48
49
50
# File 'lib/uniword/schema/schema_loader.rb', line 47

def element_definition(schema_name, element_name)
  schema = load_schema(schema_name)
  schema.dig("elements", element_name)
end

#element_names(schema_name) ⇒ Array<String>

Get all element names in a schema

Parameters:

  • schema_name (String)

    Schema name

Returns:

  • (Array<String>)

    Element names



65
66
67
68
# File 'lib/uniword/schema/schema_loader.rb', line 65

def element_names(schema_name)
  schema = load_schema(schema_name)
  schema["elements"]&.keys || []
end

#load_schema(schema_name) ⇒ Hash

Load a schema by name

Parameters:

  • schema_name (String)

    Name of the schema file (without .yml extension)

Returns:

  • (Hash)

    Parsed schema definition

Raises:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/uniword/schema/schema_loader.rb', line 30

def load_schema(schema_name)
  return @schemas[schema_name] if @schemas.key?(schema_name)

  schema_file = File.join(@schema_path, "#{schema_name}.yml")
  unless File.exist?(schema_file)
    raise SchemaNotFoundError,
          "Schema file not found: #{schema_file}"
  end

  @schemas[schema_name] = YAML.load_file(schema_file)
end

#namespace(schema_name) ⇒ Hash

Get namespace information for a schema

Parameters:

  • schema_name (String)

    Schema name

Returns:

  • (Hash)

    Namespace definition with :uri, :prefix, :description



56
57
58
59
# File 'lib/uniword/schema/schema_loader.rb', line 56

def namespace(schema_name)
  schema = load_schema(schema_name)
  schema["namespace"]
end