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:



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

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:



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

.as_reference(current_context) ⇒ Context

Convert a context into one that knows that is a reference

Parameters:

Returns:



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

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:



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:



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

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

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#documentDocument

Returns:



100
101
102
# File 'lib/openapi3_parser/context.rb', line 100

def document
  document_location.source.document
end

#inspectObject



124
125
126
127
128
# File 'lib/openapi3_parser/context.rb', line 124

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

#location_summaryObject



130
131
132
133
134
# File 'lib/openapi3_parser/context.rb', line 130

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

#namespaceObject

Deprecated.


115
116
117
# File 'lib/openapi3_parser/context.rb', line 115

def namespace
  document_location.pointer.segments
end

#nodeObject



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

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)


120
121
122
# File 'lib/openapi3_parser/context.rb', line 120

def reference?
  @is_reference
end

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



110
111
112
# File 'lib/openapi3_parser/context.rb', line 110

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

#resolved_inputObject



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

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:



105
106
107
# File 'lib/openapi3_parser/context.rb', line 105

def source
  source_location.source
end

#to_sObject



136
137
138
# File 'lib/openapi3_parser/context.rb', line 136

def to_s
  location_summary
end