Class: OpenapiRuby::Components::Registry
- Inherits:
-
Object
- Object
- OpenapiRuby::Components::Registry
- Includes:
- Singleton
- Defined in:
- lib/openapi_ruby/components/registry.rb
Instance Method Summary collapse
- #all_types ⇒ Object
- #clear! ⇒ Object
- #components_for(type) ⇒ Object
- #grouped_by_type ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #register(component_class) ⇒ Object
- #to_openapi_hash(scope: nil) ⇒ Object
- #unregister(component_class) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
10 11 12 |
# File 'lib/openapi_ruby/components/registry.rb', line 10 def initialize @components = {} end |
Instance Method Details
#all_types ⇒ Object
37 38 39 |
# File 'lib/openapi_ruby/components/registry.rb', line 37 def all_types @components.keys end |
#clear! ⇒ Object
45 46 47 |
# File 'lib/openapi_ruby/components/registry.rb', line 45 def clear! @components = {} end |
#components_for(type) ⇒ Object
33 34 35 |
# File 'lib/openapi_ruby/components/registry.rb', line 33 def components_for(type) @components[type] || {} end |
#grouped_by_type ⇒ Object
41 42 43 |
# File 'lib/openapi_ruby/components/registry.rb', line 41 def grouped_by_type @components.dup end |
#register(component_class) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/openapi_ruby/components/registry.rb', line 14 def register(component_class) type = component_class._component_type name = component_class.component_name @components[type] ||= {} if @components[type].key?(name) && @components[type][name] != component_class raise DuplicateComponentError, "Component '#{name}' already registered under #{type}" end @components[type][name] = component_class end |
#to_openapi_hash(scope: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/openapi_ruby/components/registry.rb', line 49 def to_openapi_hash(scope: nil) result = {} @components.each do |type, components| type_key = type.to_s result[type_key] = {} components.each_value do |klass| next if klass._schema_hidden next if scope && !klass._component_scopes.empty? && !klass._component_scopes.include?(scope) result[type_key][klass.component_name] = klass.to_openapi end result.delete(type_key) if result[type_key].empty? end result end |
#unregister(component_class) ⇒ Object
27 28 29 30 31 |
# File 'lib/openapi_ruby/components/registry.rb', line 27 def unregister(component_class) type = component_class._component_type name = component_class.component_name @components[type]&.delete(name) end |