Class: Openapi3Parser::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi3_parser/context.rb,
lib/openapi3_parser/context/pointer.rb,
lib/openapi3_parser/context/location.rb

Overview

Context is a construct used in both the node factories and the nodes themselves. It is used to represent the data, and the source of it, that a node is associated with. It also acts as a bridge between a node/node factory and associated document.

Defined Under Namespace

Classes: Location, Pointer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, document_location:, source_location: nil, referenced_by: nil) ⇒ Context

Returns a new instance of Context.

Parameters:



66
67
68
69
70
71
72
73
74
# File 'lib/openapi3_parser/context.rb', line 66

def initialize(input,
               document_location:,
               source_location: nil,
               referenced_by: nil)
  @input = input
  @document_location = document_location
  @source_location = source_location || document_location
  @referenced_by = referenced_by
end

Instance Attribute Details

#document_locationContext::Location (readonly)

Returns the current value of document_location.

Returns:



15
16
17
# File 'lib/openapi3_parser/context.rb', line 15

def document_location
  @document_location
end

#inputObject (readonly)

Returns the current value of input.

Returns:

  • (Object)

    the current value of input



15
16
17
# File 'lib/openapi3_parser/context.rb', line 15

def input
  @input
end

#referenced_byContext? (readonly)

Returns the current value of referenced_by.

Returns:

  • (Context, nil)

    the current value of referenced_by



15
16
17
# File 'lib/openapi3_parser/context.rb', line 15

def referenced_by
  @referenced_by
end

#source_locationContext::Location? (readonly)

Returns the current value of source_location.

Returns:



15
16
17
# File 'lib/openapi3_parser/context.rb', line 15

def source_location
  @source_location
end

Class Method Details

.next_field(parent_context, field) ⇒ Context

Create a context for a field within the current contexts data eg for a context of:

root = Context.root({ "test" => {} }, source)

we can get the context of “test” with:

test = Context.next_field(root, "test")

Parameters:

  • parent_context (Context)
  • field (String)

Returns:



32
33
34
35
36
37
38
39
# File 'lib/openapi3_parser/context.rb', line 32

def self.next_field(parent_context, field)
  pc = parent_context
  input = pc.input.respond_to?(:[]) ? pc.input[field] : nil
  new(input,
      document_location: Location.next_field(pc.document_location, field),
      source_location: Location.next_field(pc.source_location, field),
      referenced_by: pc.referenced_by)
end

.reference_field(referencer_context, input:, source:, pointer_segments:) ⇒ Context

Creates the context for a field that is referenced by a context. In this scenario the context of the document is the same but we are in a different part of the source file, or even a different source file

Parameters:

  • referencer_context (Context)
  • input
  • source (Source)
  • pointer_segments (::Array)

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/openapi3_parser/context.rb', line 50

def self.reference_field(referencer_context,
                         input:,
                         source:,
                         pointer_segments:)
  new(input,
      document_location: referencer_context.document_location,
      source_location: Location.new(source, pointer_segments),
      referenced_by: referencer_context)
end

.root(input, source) ⇒ Context

Create a context for the root of a document

Returns:



18
19
20
21
# File 'lib/openapi3_parser/context.rb', line 18

def self.root(input, source)
  location = Location.new(source, [])
  new(input, document_location: location)
end

Instance Method Details

#documentDocument

Returns:



77
78
79
# File 'lib/openapi3_parser/context.rb', line 77

def document
  document_location.source.document
end

#inspectObject



96
97
98
99
100
# File 'lib/openapi3_parser/context.rb', line 96

def inspect
  %{#{self.class.name}(document_location: #{document_location}, } +
    %{source_location: #{source_location}), referenced_by: } +
    %{#{referenced_by})}
end

#location_summaryObject



102
103
104
105
106
# File 'lib/openapi3_parser/context.rb', line 102

def location_summary
  summary = document_location.to_s
  summary += " (#{source_location})" if document_location != source_location
  summary
end

#namespaceObject

Deprecated.


92
93
94
# File 'lib/openapi3_parser/context.rb', line 92

def namespace
  document_location.pointer.segments
end

#register_reference(reference, factory) ⇒ Source::ReferenceResolver



87
88
89
# File 'lib/openapi3_parser/context.rb', line 87

def register_reference(reference, factory)
  source.register_reference(reference, factory, self)
end

#sourceSource

Returns:



82
83
84
# File 'lib/openapi3_parser/context.rb', line 82

def source
  source_location.source
end

#to_sObject



108
109
110
# File 'lib/openapi3_parser/context.rb', line 108

def to_s
  location_summary
end