Class: Metanorma::Html::ComponentRegistry
- Inherits:
-
Object
- Object
- Metanorma::Html::ComponentRegistry
- Defined in:
- lib/metanorma/html/component_registry.rb
Instance Method Summary collapse
- #component_for(renderer, model_class) ⇒ Object
-
#initialize ⇒ ComponentRegistry
constructor
A new instance of ComponentRegistry.
- #lookup(model_class) ⇒ Object
- #register(model_class, component_class) ⇒ Object
- #register_all(component_module) ⇒ Object
Constructor Details
#initialize ⇒ ComponentRegistry
Returns a new instance of ComponentRegistry.
6 7 8 |
# File 'lib/metanorma/html/component_registry.rb', line 6 def initialize @map = {} end |
Instance Method Details
#component_for(renderer, model_class) ⇒ Object
23 24 25 26 |
# File 'lib/metanorma/html/component_registry.rb', line 23 def component_for(renderer, model_class) component_class = lookup(model_class) component_class&.new(renderer) end |
#lookup(model_class) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/metanorma/html/component_registry.rb', line 14 def lookup(model_class) @map[model_class] || model_class.ancestors .drop(1) .filter_map { |a| @map[a] } .first || nil end |
#register(model_class, component_class) ⇒ Object
10 11 12 |
# File 'lib/metanorma/html/component_registry.rb', line 10 def register(model_class, component_class) @map[model_class] = component_class end |
#register_all(component_module) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/metanorma/html/component_registry.rb', line 28 def register_all(component_module) component_module.constants.each do |const| klass = component_module.const_get(const) next unless klass.is_a?(Class) && klass < Component::Base klass.register_in(self) end end |