Class: Uniword::Ooxml::Schema::OoxmlSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/ooxml/schema/ooxml_schema.rb

Overview

OOXML Schema loaded from external YAML configuration.

Responsibility: Load and provide access to OOXML element definitions. Single Responsibility - schema management only.

Follows “Configuration over Convention” - schema is defined in external YAML files following ISO/IEC 29500 specification.

Examples:

Load schema

schema = OoxmlSchema.load('config/ooxml/schema_main.yml')
definition = schema.definition_for(Paragraph)

Get element definition

para_def = schema.element('paragraph')
puts para_def.tag  # => 'w:p'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OoxmlSchema

Initialize schema from configuration hash

Parameters:

  • config (Hash)

    Parsed YAML configuration



61
62
63
64
65
66
67
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 61

def initialize(config)
  validate_schema_config(config)

  @version = config.dig(:schema, :version)
  @namespace = config.dig(:schema, :namespace)
  @elements = build_element_definitions(config[:elements] || {})
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



22
23
24
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 22

def elements
  @elements
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



22
23
24
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 22

def namespace
  @namespace
end

#versionObject (readonly)

Returns the value of attribute version.



22
23
24
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 22

def version
  @version
end

Class Method Details

.aggregate_schema_files(loader_config, loader_file) ⇒ Hash

Aggregate multiple schema files into single schema

Parameters:

  • loader_config (Hash)

    Loader configuration

  • loader_file (String)

    Path to loader file

Returns:

  • (Hash)

    Combined schema configuration



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 191

def self.aggregate_schema_files(loader_config, loader_file)
  base_path = File.dirname(loader_file)
  all_elements = {}

  # Load each schema file and merge elements
  loader_config[:schema_files].each do |schema_file|
    file_path = File.join(base_path, schema_file)

    next unless File.exist?(file_path)

    file_config = Configuration::ConfigurationLoader.load_file(file_path)

    # Merge elements from this file
    all_elements.merge!(file_config[:elements]) if file_config[:elements]
  end

  # Build combined schema configuration
  {
    schema: {
      version: loader_config[:version],
      namespace: loader_config[:namespaces]&.dig(:w),
    },
    elements: all_elements,
  }
end

.default_loader_pathString

Get default loader path

Returns:

  • (String)

    Path to default schema loader



220
221
222
223
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 220

def self.default_loader_path
  File.expand_path("../../../../config/ooxml/schema_loader.yml",
                   __dir__)
end

.load(schema_file = nil) ⇒ OoxmlSchema

Load schema from YAML file or multi-file loader

Examples:

Load main schema (legacy single file)

schema = OoxmlSchema.load('config/ooxml/schema_main.yml')

Load multi-file schema (default)

schema = OoxmlSchema.load

Parameters:

  • schema_file (String, nil) (defaults to: nil)

    Path to schema YAML file or nil for default

Returns:

Raises:

  • (ConfigurationError)

    if schema file invalid



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 35

def self.load(schema_file = nil)
  schema_file ||= default_loader_path

  # Check if this is a loader configuration or direct schema file
  if loader_config?(schema_file)
    load_from_loader(schema_file)
  else
    # Legacy single-file loading
    config = Configuration::ConfigurationLoader.load_file(schema_file)
    new(config)
  end
end

.load_from_loader(loader_file) ⇒ OoxmlSchema

Load schema from multi-file loader configuration

Parameters:

  • loader_file (String)

    Path to schema loader YAML

Returns:



52
53
54
55
56
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 52

def self.load_from_loader(loader_file)
  loader_config = Configuration::ConfigurationLoader.load_file(loader_file)
  combined_schema = aggregate_schema_files(loader_config, loader_file)
  new(combined_schema)
end

.loader_config?(file_path) ⇒ Boolean

Check if file is a loader configuration

Parameters:

  • file_path (String)

    Path to file

Returns:

  • (Boolean)

    true if loader config



182
183
184
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 182

def self.loader_config?(file_path)
  file_path.end_with?("schema_loader.yml")
end

Instance Method Details

#definition_for(element_class_or_name) ⇒ ElementDefinition

Get element definition by element class or name

Examples:

Get by class

definition = schema.definition_for(Paragraph)

Get by name

definition = schema.definition_for(:paragraph)

Parameters:

  • element_class_or_name (Class, Symbol, String)

    Element class or name

Returns:

Raises:

  • (ArgumentError)

    if element not found in schema



80
81
82
83
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 80

def definition_for(element_class_or_name)
  name = normalize_name(element_class_or_name)
  @elements[name] || raise_element_not_found(name)
end

#element(name) ⇒ ElementDefinition

Get element definition by name (alias for definition_for)

Parameters:

  • name (Symbol)

    Element name

Returns:



89
90
91
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 89

def element(name)
  @elements[name.to_sym]
end

#element_countInteger

Get count of defined elements

Returns:

  • (Integer)

    Number of elements in schema



112
113
114
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 112

def element_count
  @elements.count
end

#element_namesArray<Symbol>

Get all element names

Returns:

  • (Array<Symbol>)

    Array of element names



105
106
107
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 105

def element_names
  @elements.keys
end

#has_element?(element_class_or_name) ⇒ Boolean

Check if schema has definition for element

Parameters:

  • element_class_or_name (Class, Symbol)

    Element class or name

Returns:

  • (Boolean)

    true if defined



97
98
99
100
# File 'lib/uniword/ooxml/schema/ooxml_schema.rb', line 97

def has_element?(element_class_or_name)
  name = normalize_name(element_class_or_name)
  @elements.key?(name)
end