Class: Openapi3Parser::Context
- Inherits:
-
Object
- Object
- Openapi3Parser::Context
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.
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
Returns the current value of document_location.
13
14
15
|
# File 'lib/openapi3_parser/context.rb', line 13
def document_location
@document_location
end
|
Returns the current value of input.
13
14
15
|
# File 'lib/openapi3_parser/context.rb', line 13
def input
@input
end
|
#referenced_by ⇒ Context?
Returns the current value of referenced_by.
13
14
15
|
# File 'lib/openapi3_parser/context.rb', line 13
def referenced_by
@referenced_by
end
|
Returns the current value of source_location.
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
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")
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
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
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
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
|
98
99
100
|
# File 'lib/openapi3_parser/context.rb', line 98
def document
document_location.source.document
end
|
#inspect ⇒ Object
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_summary ⇒ Object
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
|
#namespace ⇒ Object
113
114
115
|
# File 'lib/openapi3_parser/context.rb', line 113
def namespace
document_location.pointer.segments
end
|
#node ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/openapi3_parser/context.rb', line 150
def node
pointer = if reference?
document_location.pointer.segments[0...-1]
else
document_location.pointer
end
document.node_at(pointer)
end
|
#reference? ⇒ Boolean
118
119
120
|
# File 'lib/openapi3_parser/context.rb', line 118
def reference?
@is_reference
end
|
108
109
110
|
# File 'lib/openapi3_parser/context.rb', line 108
def register_reference(reference, factory)
source.register_reference(reference, factory, self)
end
|
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/openapi3_parser/context.rb', line 138
def resolved_input
pointer = if reference?
document_location.pointer.segments[0...-1]
else
document_location.pointer
end
document.resolved_input_at(pointer)
end
|
103
104
105
|
# File 'lib/openapi3_parser/context.rb', line 103
def source
source_location.source
end
|
#to_s ⇒ Object
134
135
136
|
# File 'lib/openapi3_parser/context.rb', line 134
def to_s
location_summary
end
|