Class: Uniword::Assembly::ComponentRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/assembly/component_registry.rb

Overview

Manages component library with caching and wildcard resolution.

Responsibility: Manage component files and provide efficient access. Single Responsibility: Only handles component file management.

The ComponentRegistry:

  • Loads component files from directory

  • Caches loaded components for performance

  • Resolves wildcard patterns (e.g., ‘clauses/*’)

  • Provides unified access to components

Examples:

Basic usage

registry = ComponentRegistry.new('components/')
component = registry.get('cover_page')

Wildcard resolution

registry = ComponentRegistry.new('components/')
components = registry.resolve('clauses/*')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components_dir, cache_enabled: true) ⇒ ComponentRegistry

Initialize registry with components directory.

Examples:

Create registry

registry = ComponentRegistry.new('components/')

Disable caching

registry = ComponentRegistry.new('components/',
  cache_enabled: false
)

Parameters:

  • components_dir (String)

    Path to components directory

  • cache_enabled (Boolean) (defaults to: true)

    Enable component caching



42
43
44
45
46
47
48
# File 'lib/uniword/assembly/component_registry.rb', line 42

def initialize(components_dir, cache_enabled: true)
  @components_dir = File.expand_path(components_dir)
  @cache_enabled = cache_enabled
  @cache = {}

  validate_directory!
end

Instance Attribute Details

#cacheHash (readonly)

Returns Cached components.

Returns:

  • (Hash)

    Cached components



28
29
30
# File 'lib/uniword/assembly/component_registry.rb', line 28

def cache
  @cache
end

#components_dirString (readonly)

Returns Base directory for components.

Returns:

  • (String)

    Base directory for components



25
26
27
# File 'lib/uniword/assembly/component_registry.rb', line 25

def components_dir
  @components_dir
end

Instance Method Details

#cache_statsHash

Get cache statistics.

Returns:

  • (Hash)

    Cache statistics



132
133
134
135
136
137
138
# File 'lib/uniword/assembly/component_registry.rb', line 132

def cache_stats
  {
    enabled: @cache_enabled,
    size: @cache.size,
    components: @cache.keys,
  }
end

#clear_cachevoid

This method returns an undefined value.

Clear component cache.



125
126
127
# File 'lib/uniword/assembly/component_registry.rb', line 125

def clear_cache
  @cache.clear
end

#exists?(name) ⇒ Boolean

Check if component exists.

Parameters:

  • name (String)

    Component name

Returns:

  • (Boolean)

    True if component exists



96
97
98
# File 'lib/uniword/assembly/component_registry.rb', line 96

def exists?(name)
  find_component_path(name) != nil
end

#get(name) ⇒ Document

Get component by name.

Examples:

Get component

component = registry.get('cover_page')

Parameters:

  • name (String)

    Component name

Returns:

  • (Document)

    Loaded component document



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/assembly/component_registry.rb', line 57

def get(name)
  # Check cache first
  return @cache[name] if @cache_enabled && @cache.key?(name)

  # Load component
  component = load_component(name)

  # Cache if enabled
  @cache[name] = component if @cache_enabled

  component
end

#list(pattern = nil) ⇒ Array<String>

List all available components.

Examples:

List all components

names = registry.list

List with pattern

names = registry.list('clauses/*')

Parameters:

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

    Optional glob pattern

Returns:

  • (Array<String>)

    List of component names



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/uniword/assembly/component_registry.rb', line 110

def list(pattern = nil)
  if pattern
    resolve_pattern_to_paths(pattern).map do |path|
      extract_component_name(path)
    end
  else
    find_all_components.map do |path|
      extract_component_name(path)
    end
  end
end

#resolve(pattern, order: nil) ⇒ Array<Hash>

Resolve wildcard pattern to list of components.

Examples:

Resolve wildcard

components = registry.resolve('clauses/*')

With ordering

components = registry.resolve('clauses/*',
  order: 'alphabetical'
)

Parameters:

  • pattern (String)

    Component pattern (e.g., ‘clauses/*’)

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

    Sort order (‘alphabetical’, ‘numeric’, nil)

Returns:

  • (Array<Hash>)

    List of component info hashes



83
84
85
86
87
88
89
90
# File 'lib/uniword/assembly/component_registry.rb', line 83

def resolve(pattern, order: nil)
  if pattern.include?("*")
    resolve_wildcard(pattern, order: order)
  else
    # Single component
    [{ name: pattern, document: get(pattern) }]
  end
end