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, is_reference: false) ⇒ Context

Returns a new instance of Context.

Parameters:



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/openapi3_parser/context.rb', line 77

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

Instance Attribute Details

#document_locationContext::Location (readonly)

Returns the current value of document_location.

Returns:



13
14
15
# File 'lib/openapi3_parser/context.rb', line 13

def document_location
  @document_location
end

#inputObject (readonly)

Returns the current value of input.

Returns:

  • (Object)

    the current value of input



13
14
15
# File 'lib/openapi3_parser/context.rb', line 13

def input
  @input
end

#referenced_byContext? (readonly)

Returns the current value of referenced_by.

Returns:

  • (Context, nil)

    the current value of referenced_by



13
14
15
# File 'lib/openapi3_parser/context.rb', line 13

def referenced_by
  @referenced_by
end

#source_locationContext::Location? (readonly)

Returns the current value of source_location.

Returns:



13
14
15
# File 'lib/openapi3_parser/context.rb', line 13

def source_location
  @source_location
end

Class Method Details

.as_reference(current_context) ⇒ Context

Convert a context into one that knows that is a reference

Parameters:

Returns:



43
44
45
46
47
48
49
# File 'lib/openapi3_parser/context.rb', line 43

def self.as_reference(current_context)
  new(current_context.input,
      document_location: current_context.document_location,
      source_location: current_context.source_location,
      referenced_by: current_context.referenced_by,
      is_reference: true)
end

.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:



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

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:



60
61
62
63
64
65
66
67
68
# File 'lib/openapi3_parser/context.rb', line 60

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:



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

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

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def ==(other)
  input == other.input &&
    document_location == other.document_location &&
    source_location == other.source_location &&
    referenced_by == other.referenced_by
end

#documentDocument

Returns:



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

def document
  document_location.source.document
end

#inspectObject



122
123
124
125
126
# File 'lib/openapi3_parser/context.rb', line 122

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

#location_summaryObject



128
129
130
131
132
# File 'lib/openapi3_parser/context.rb', line 128

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

#namespaceObject

Deprecated.


113
114
115
# File 'lib/openapi3_parser/context.rb', line 113

def namespace
  document_location.pointer.segments
end

#nodeObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/openapi3_parser/context.rb', line 150

def node
  # The created node for a reference is at the segment before the
  # reference
  pointer = if reference?
              document_location.pointer.segments[0...-1]
            else
              document_location.pointer
            end

  document.node_at(pointer)
end

#reference?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/openapi3_parser/context.rb', line 118

def reference?
  @is_reference
end

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



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

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

#resolved_inputObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openapi3_parser/context.rb', line 138

def resolved_input
  # The resolved input for a reference is at the segment before the
  # reference
  pointer = if reference?
              document_location.pointer.segments[0...-1]
            else
              document_location.pointer
            end

  document.resolved_input_at(pointer)
end

#sourceSource

Returns:



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

def source
  source_location.source
end

#to_sObject



134
135
136
# File 'lib/openapi3_parser/context.rb', line 134

def to_s
  location_summary
end