Class: Uniword::Assembly::ComponentRegistry
- Inherits:
-
Object
- Object
- Uniword::Assembly::ComponentRegistry
- 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
Instance Attribute Summary collapse
-
#cache ⇒ Hash
readonly
Cached components.
-
#components_dir ⇒ String
readonly
Base directory for components.
Instance Method Summary collapse
-
#cache_stats ⇒ Hash
Get cache statistics.
-
#clear_cache ⇒ void
Clear component cache.
-
#exists?(name) ⇒ Boolean
Check if component exists.
-
#get(name) ⇒ Document
Get component by name.
-
#initialize(components_dir, cache_enabled: true) ⇒ ComponentRegistry
constructor
Initialize registry with components directory.
-
#list(pattern = nil) ⇒ Array<String>
List all available components.
-
#resolve(pattern, order: nil) ⇒ Array<Hash>
Resolve wildcard pattern to list of components.
Constructor Details
#initialize(components_dir, cache_enabled: true) ⇒ ComponentRegistry
Initialize registry with components directory.
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.(components_dir) @cache_enabled = cache_enabled @cache = {} validate_directory! end |
Instance Attribute Details
#cache ⇒ Hash (readonly)
Returns Cached components.
28 29 30 |
# File 'lib/uniword/assembly/component_registry.rb', line 28 def cache @cache end |
#components_dir ⇒ String (readonly)
Returns 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_stats ⇒ Hash
Get 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_cache ⇒ void
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.
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.
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.
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.
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 |