Class: Uniword::Schema::SchemaLoader
- Inherits:
-
Object
- Object
- Uniword::Schema::SchemaLoader
- 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.
Instance Method Summary collapse
-
#available_schemas ⇒ Array<String>
Get all available schemas.
-
#clear_cache! ⇒ void
Clear cached schemas (useful for testing).
-
#element_definition(schema_name, element_name) ⇒ Hash?
Get element definition from a schema.
-
#element_names(schema_name) ⇒ Array<String>
Get all element names in a schema.
-
#initialize ⇒ SchemaLoader
constructor
A new instance of SchemaLoader.
-
#load_schema(schema_name) ⇒ Hash
Load a schema by name.
-
#namespace(schema_name) ⇒ Hash
Get namespace information for a schema.
Constructor Details
#initialize ⇒ SchemaLoader
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_schemas ⇒ Array<String>
Get all available schemas
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
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
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
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
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 |