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



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

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

#all_typesObject



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_typeObject



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/openapi_ruby/components/registry.rb', line 90

def to_openapi_hash(scope: nil)
  result = {}
  # Track which components are scope-specific vs multi-scope/shared
  # so scope-specific ones take precedence on name collisions.
  specificity = {}

  @components.each do |type, components|
    type_key = type.to_s
    result[type_key] = {}
    specificity[type_key] = {}

    components.each_value do |klass|
      next if klass._schema_hidden

      if scope
        if klass._component_scopes.empty?
          next unless klass._component_scopes_explicitly_set
        else
          next unless klass._component_scopes.include?(scope)
        end
      end

      name = klass.component_name
      is_specific = klass._component_scopes.length == 1 && klass._component_scopes.include?(scope)

      # Scope-specific components take precedence over shared/multi-scope
      if specificity[type_key][name] && !is_specific
        next
      end

      result[type_key][name] = klass.to_openapi
      specificity[type_key][name] = is_specific
    end
    result.delete(type_key) if result[type_key].empty?
  end
  # Sort component names alphabetically within each type for deterministic output
  # regardless of file system ordering or load order.
  result.each { |type_key, entries| result[type_key] = entries.sort_by { |k, _| k }.to_h }
  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