Class: GrapeOAS::Introspectors::EntityIntrospectorSupport::CycleTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/introspectors/entity_introspector_support/cycle_tracker.rb

Overview

Tracks and handles cyclic references during entity introspection.

Instance Method Summary collapse

Constructor Details

#initialize(entity_class, stack) ⇒ CycleTracker

Returns a new instance of CycleTracker.



8
9
10
11
# File 'lib/grape_oas/introspectors/entity_introspector_support/cycle_tracker.rb', line 8

def initialize(entity_class, stack)
  @entity_class = entity_class
  @stack = stack
end

Instance Method Details

#cyclic_reference?Boolean

Checks if the current entity class is already in the processing stack.

Returns:

  • (Boolean)

    true if a cycle is detected



16
17
18
# File 'lib/grape_oas/introspectors/entity_introspector_support/cycle_tracker.rb', line 16

def cyclic_reference?
  @stack.include?(@entity_class)
end

#handle_cycle(schema) ⇒ ApiModel::Schema

Handles a detected cycle by marking the schema with a description.

Parameters:

Returns:



24
25
26
27
# File 'lib/grape_oas/introspectors/entity_introspector_support/cycle_tracker.rb', line 24

def handle_cycle(schema)
  schema.description ||= "Cycle detected while introspecting"
  schema
end

#with_tracking { ... } ⇒ Object

Executes a block while tracking the current entity in the stack.

Yields:

  • the block to execute while tracking

Returns:

  • the result of the block



33
34
35
36
37
38
# File 'lib/grape_oas/introspectors/entity_introspector_support/cycle_tracker.rb', line 33

def with_tracking
  @stack << @entity_class
  yield
ensure
  @stack.pop
end