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_registered_classes ⇒ Object
- #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_registered_classes ⇒ Object
42 43 44 |
# File 'lib/openapi_ruby/components/registry.rb', line 42 def all_registered_classes @components.values.flat_map(&:values) end |
#all_types ⇒ Object
38 39 40 |
# File 'lib/openapi_ruby/components/registry.rb', line 38 def all_types @components.keys end |
#clear! ⇒ Object
50 51 52 |
# File 'lib/openapi_ruby/components/registry.rb', line 50 def clear! @components = {} end |
#components_for(type) ⇒ Object
34 35 36 |
# File 'lib/openapi_ruby/components/registry.rb', line 34 def components_for(type) @components[type] || {} end |
#grouped_by_type ⇒ Object
46 47 48 |
# File 'lib/openapi_ruby/components/registry.rb', line 46 def grouped_by_type @components.dup end |
#register(component_class) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/openapi_ruby/components/registry.rb', line 14 def register(component_class) type = component_class._component_type name = component_class.name || "Anonymous" @components[type] ||= {} check_for_duplicate!(component_class, type) # Use the full class name as key to avoid collisions between # same-named components in different scopes (e.g., Internal::V1::Schemas::PaginatedCollection # vs Mobile::V1::Schemas::PaginatedCollection). Scope filtering happens in to_openapi_hash. @components[type][name] = component_class end |
#to_openapi_hash(scope: nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openapi_ruby/components/registry.rb', line 90 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
28 29 30 31 32 |
# File 'lib/openapi_ruby/components/registry.rb', line 28 def unregister(component_class) type = component_class._component_type name = component_class.name || "Anonymous" @components[type]&.delete(name) end |