Class: Coradoc::Registry
- Inherits:
-
Object
- Object
- Coradoc::Registry
- Defined in:
- lib/coradoc/registry.rb
Overview
General-purpose named-item registry.
Used by the format registry (Coradoc.registry), Input processors, and Output processors. Each instance stores items keyed by symbol name, with optional per-item options.
Instance Attribute Summary collapse
-
#error_label ⇒ Object
readonly
Returns the value of attribute error_label.
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all registered items.
-
#define(item, **opts) ⇒ void
Register a self-identifying item (extracts name via processor_id).
-
#each {|Symbol, Object| ... } ⇒ Enumerator
Iterate over all items.
-
#each_key {|Symbol| ... } ⇒ Enumerator
Iterate over item names.
-
#each_value {|Object| ... } ⇒ Enumerator
Iterate over item values.
-
#for_file(filename) ⇒ Object?
Find an item whose processor_match? returns true for the given filename.
-
#get(name) ⇒ Object?
(also: #[])
Get a registered item by name.
-
#initialize(error_label: nil) ⇒ Registry
constructor
A new instance of Registry.
-
#items ⇒ Hash<Symbol, Object>
Direct access to the items hash (for backward compatibility).
-
#list ⇒ Array<Symbol>
List all registered item names.
-
#options_for(name) ⇒ Hash?
Get options for a registered item.
-
#process(content, options = {}) ⇒ Object
Resolve and execute: find item by format or filename, call processor_execute.
-
#register(name, item, opts = {}) ⇒ Object
Register an item by explicit name.
-
#registered?(name) ⇒ Boolean
Check if an item is registered.
-
#size ⇒ Integer
Number of registered items.
Constructor Details
#initialize(error_label: nil) ⇒ Registry
Returns a new instance of Registry.
24 25 26 27 28 |
# File 'lib/coradoc/registry.rb', line 24 def initialize(error_label: nil) @items = {} @options = {} @error_label = error_label end |
Instance Attribute Details
#error_label ⇒ Object (readonly)
Returns the value of attribute error_label.
21 22 23 |
# File 'lib/coradoc/registry.rb', line 21 def error_label @error_label end |
Instance Method Details
#clear ⇒ Object
Remove all registered items
100 101 102 103 |
# File 'lib/coradoc/registry.rb', line 100 def clear @items.clear @options.clear end |
#define(item, **opts) ⇒ void
This method returns an undefined value.
Register a self-identifying item (extracts name via processor_id)
48 49 50 51 52 |
# File 'lib/coradoc/registry.rb', line 48 def define(item, **opts) return unless item.respond_to?(:processor_id) register(item.processor_id, item, opts) end |
#each {|Symbol, Object| ... } ⇒ Enumerator
Iterate over all items
109 110 111 |
# File 'lib/coradoc/registry.rb', line 109 def each(&block) @items.each(&block) end |
#each_key {|Symbol| ... } ⇒ Enumerator
Iterate over item names
125 126 127 |
# File 'lib/coradoc/registry.rb', line 125 def each_key(&block) @items.each_key(&block) end |
#each_value {|Object| ... } ⇒ Enumerator
Iterate over item values
117 118 119 |
# File 'lib/coradoc/registry.rb', line 117 def each_value(&block) @items.each_value(&block) end |
#for_file(filename) ⇒ Object?
Find an item whose processor_match? returns true for the given filename
133 134 135 136 137 |
# File 'lib/coradoc/registry.rb', line 133 def for_file(filename) @items.values.find do |item| item.respond_to?(:processor_match?) && item.processor_match?(filename) end end |
#get(name) ⇒ Object? Also known as: []
Get a registered item by name
58 59 60 |
# File 'lib/coradoc/registry.rb', line 58 def get(name) @items[name.to_sym] end |
#items ⇒ Hash<Symbol, Object>
Direct access to the items hash (for backward compatibility)
88 89 90 |
# File 'lib/coradoc/registry.rb', line 88 def items @items end |
#list ⇒ Array<Symbol>
List all registered item names
82 83 84 |
# File 'lib/coradoc/registry.rb', line 82 def list @items.keys end |
#options_for(name) ⇒ Hash?
Get options for a registered item
67 68 69 |
# File 'lib/coradoc/registry.rb', line 67 def (name) @options[name] end |
#process(content, options = {}) ⇒ Object
Resolve and execute: find item by format or filename, call processor_execute
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/coradoc/registry.rb', line 145 def process(content, = {}) item = if [:format] get([:format]) elsif [:filename] for_file([:filename]) end label = @error_label || "processor" raise ArgumentError, "No #{label} found for: #{}" unless item item.processor_execute(content, ) end |
#register(name, item, opts = {}) ⇒ Object
Register an item by explicit name
36 37 38 39 40 41 |
# File 'lib/coradoc/registry.rb', line 36 def register(name, item, opts = {}) raise ArgumentError, "Name must be a Symbol, got #{name.class}" unless name.is_a?(Symbol) @items[name] = item @options[name] = opts end |
#registered?(name) ⇒ Boolean
Check if an item is registered
75 76 77 |
# File 'lib/coradoc/registry.rb', line 75 def registered?(name) @items.key?(name) end |
#size ⇒ Integer
Number of registered items
95 96 97 |
# File 'lib/coradoc/registry.rb', line 95 def size @items.size end |