Class: OpenapiRuby::Components::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/openapi_ruby/components/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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_classesObject



41
42
43
# File 'lib/openapi_ruby/components/registry.rb', line 41

def all_registered_classes
  @components.values.flat_map(&:values)
end

#all_typesObject



37
38
39
# File 'lib/openapi_ruby/components/registry.rb', line 37

def all_types
  @components.keys
end

#clear!Object



49
50
51
# File 'lib/openapi_ruby/components/registry.rb', line 49

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_typeObject



45
46
47
# File 'lib/openapi_ruby/components/registry.rb', line 45

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
  key = component_class.registry_key

  @components[type] ||= {}

  if @components[type].key?(key) && @components[type][key] != component_class
    raise DuplicateComponentError, "Component '#{component_class.component_name}' already registered under #{type}"
  end

  @components[type][key] = component_class
end

#to_openapi_hash(scope: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openapi_ruby/components/registry.rb', line 53

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
  key = component_class.registry_key
  @components[type]&.delete(key)
end