Class: Coradoc::Registry

Inherits:
Object
  • Object
show all
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.

Examples:

Format registry

registry = Registry.new
registry.register(:html, Coradoc::Html)
registry.get(:html)  # => Coradoc::Html

Processor registry (self-identifying items)

registry = Registry.new(error_label: "input processor")
registry.define(MyProcessor)  # uses MyProcessor.processor_id
registry.for_file("doc.html") # checks processor_match? on each item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_label: nil) ⇒ Registry

Returns a new instance of Registry.

Parameters:

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

    label for error messages in #process



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_labelObject (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

#clearObject

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)

Parameters:

  • item (Object)

    item that responds to #processor_id

  • options (Hash)

    optional per-item configuration



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

Yields:

  • (Symbol, Object)

    name and item

Returns:

  • (Enumerator)


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

Yields:

  • (Symbol)

Returns:

  • (Enumerator)


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

Yields:

  • (Object)

Returns:

  • (Enumerator)


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

Parameters:

  • filename (String)

Returns:

  • (Object, nil)


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

Parameters:

  • name (Symbol, String)

    the item name (strings are coerced to symbols)

Returns:

  • (Object, nil)


58
59
60
# File 'lib/coradoc/registry.rb', line 58

def get(name)
  @items[name.to_sym]
end

#itemsHash<Symbol, Object>

Direct access to the items hash (for backward compatibility)

Returns:

  • (Hash<Symbol, Object>)


88
89
90
# File 'lib/coradoc/registry.rb', line 88

def items
  @items
end

#listArray<Symbol>

List all registered item names

Returns:

  • (Array<Symbol>)


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

Parameters:

  • name (Symbol)

Returns:

  • (Hash, nil)


67
68
69
# File 'lib/coradoc/registry.rb', line 67

def options_for(name)
  @options[name]
end

#process(content, options = {}) ⇒ Object

Resolve and execute: find item by format or filename, call processor_execute

Parameters:

  • content (Object)

    content to process

  • options (Hash) (defaults to: {})

    :format or :filename for resolution

Returns:

  • (Object)

    result of processor_execute

Raises:

  • (ArgumentError)

    if no matching item found



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/coradoc/registry.rb', line 145

def process(content, options = {})
  item = if options[:format]
           get(options[:format])
         elsif options[:filename]
           for_file(options[:filename])
         end

  label = @error_label || "processor"
  raise ArgumentError, "No #{label} found for: #{options}" unless item

  item.processor_execute(content, options)
end

#register(name, item, opts = {}) ⇒ Object

Register an item by explicit name

Parameters:

  • name (Symbol)

    the item name

  • item (Object)

    the item to register

  • opts (Hash) (defaults to: {})

    optional per-item configuration

Raises:

  • (ArgumentError)

    if name is not a Symbol



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

Parameters:

  • name (Symbol)

Returns:

  • (Boolean)


75
76
77
# File 'lib/coradoc/registry.rb', line 75

def registered?(name)
  @items.key?(name)
end

#sizeInteger

Number of registered items

Returns:

  • (Integer)


95
96
97
# File 'lib/coradoc/registry.rb', line 95

def size
  @items.size
end