Class: Openapi3Parser::Node::Context
- Inherits:
-
Object
- Object
- Openapi3Parser::Node::Context
- Defined in:
- lib/openapi3_parser/node/context.rb
Overview
This class is used to specify the data and source information for a Node, for every node there is a different context to represent it’s place within the document.
Instance Attribute Summary collapse
-
#document_location ⇒ Source::Location
readonly
The location in the root source of this node.
-
#input ⇒ Any
readonly
The raw data that was used to build the node.
-
#source_location ⇒ Source::Location
readonly
The location in a source file of this.
Class Method Summary collapse
-
.next_field(parent_context, field, factory_context) ⇒ Node::Context
Create a context for the child of a previous context.
-
.resolved_reference(current_context, reference_factory_context) ⇒ Node::Context
Create a context for a the a field that is the result of a reference.
-
.root(factory_context) ⇒ Node::Context
Create a context for the root of a document.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#document ⇒ Document
The OpenAPI document associated with this context.
-
#initialize(input, document_location:, source_location:) ⇒ Context
constructor
A new instance of Context.
- #inspect ⇒ String
-
#location_summary ⇒ String
A string representing the location of the node.
-
#node ⇒ Node::Object, ...
Return the node for this context.
-
#parent_node ⇒ Node::Object, ...
Return the node that is the parent node for the node at this context.
-
#relative_node(pointer) ⇒ Object
Look up a node at a particular location in the OpenAPI docuemnt based on the relative position in the document of this context.
-
#resolved_input ⇒ Any
Used to return the data at this document location with all references resolved and optional fields populated with defaults.
-
#same_data_and_source?(other) ⇒ Boolean
Check that contexts are the same without concern for document location.
-
#source ⇒ Source
The source file used to provide the data for this node.
-
#to_s ⇒ String
A string representing the location of the node.
Constructor Details
#initialize(input, document_location:, source_location:) ⇒ Context
Returns a new instance of Context.
62 63 64 65 66 |
# File 'lib/openapi3_parser/node/context.rb', line 62 def initialize(input, document_location:, source_location:) @input = input @document_location = document_location @source_location = source_location end |
Instance Attribute Details
#document_location ⇒ Source::Location (readonly)
The location in the root source of this node
17 18 19 |
# File 'lib/openapi3_parser/node/context.rb', line 17 def document_location @document_location end |
#input ⇒ Any (readonly)
The raw data that was used to build the node
17 18 19 |
# File 'lib/openapi3_parser/node/context.rb', line 17 def input @input end |
#source_location ⇒ Source::Location (readonly)
The location in a source file of this
17 18 19 |
# File 'lib/openapi3_parser/node/context.rb', line 17 def source_location @source_location end |
Class Method Details
.next_field(parent_context, field, factory_context) ⇒ Node::Context
Create a context for the child of a previous context
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/openapi3_parser/node/context.rb', line 35 def self.next_field(parent_context, field, factory_context) document_location = Source::Location.next_field( parent_context.document_location, field ) new(factory_context.input, document_location:, source_location: factory_context.source_location) end |
.resolved_reference(current_context, reference_factory_context) ⇒ Node::Context
Create a context for a the a field that is the result of a reference
51 52 53 54 55 |
# File 'lib/openapi3_parser/node/context.rb', line 51 def self.resolved_reference(current_context, reference_factory_context) new(reference_factory_context.input, document_location: current_context.document_location, source_location: reference_factory_context.source_location) end |
.root(factory_context) ⇒ Node::Context
Create a context for the root of a document
22 23 24 25 26 27 |
# File 'lib/openapi3_parser/node/context.rb', line 22 def self.root(factory_context) location = Source::Location.new(factory_context.source, []) new(factory_context.input, document_location: location, source_location: factory_context.source_location) end |
Instance Method Details
#==(other) ⇒ Boolean
70 71 72 73 |
# File 'lib/openapi3_parser/node/context.rb', line 70 def ==(other) document_location == other.document_location && same_data_and_source?(other) end |
#document ⇒ Document
The OpenAPI document associated with this context
87 88 89 |
# File 'lib/openapi3_parser/node/context.rb', line 87 def document document_location.source.document end |
#inspect ⇒ String
99 100 101 102 |
# File 'lib/openapi3_parser/node/context.rb', line 99 def inspect %{#{self.class.name}(document_location: #{document_location}, } + %{source_location: #{source_location})} end |
#location_summary ⇒ String
A string representing the location of the node
107 108 109 110 111 112 113 |
# File 'lib/openapi3_parser/node/context.rb', line 107 def location_summary summary = document_location.to_s summary += " (#{source_location})" if document_location != source_location summary end |
#node ⇒ Node::Object, ...
Return the node for this context
131 132 133 |
# File 'lib/openapi3_parser/node/context.rb', line 131 def node document.node_at(document_location.pointer) end |
#parent_node ⇒ Node::Object, ...
Return the node that is the parent node for the node at this context
152 153 154 155 156 |
# File 'lib/openapi3_parser/node/context.rb', line 152 def parent_node return if document_location.root? relative_node("#..") end |
#relative_node(pointer) ⇒ Object
Look up a node at a particular location in the OpenAPI docuemnt based on the relative position in the document of this context
Examples:
context.relative_node(“#schemas”) context.relative_node(%w)
145 146 147 |
# File 'lib/openapi3_parser/node/context.rb', line 145 def relative_node(pointer) document.node_at(pointer, document_location.pointer) end |
#resolved_input ⇒ Any
Used to return the data at this document location with all references resolved and optional fields populated with defaults
124 125 126 |
# File 'lib/openapi3_parser/node/context.rb', line 124 def resolved_input document.resolved_input_at(document_location.pointer) end |
#same_data_and_source?(other) ⇒ Boolean
Check that contexts are the same without concern for document location
79 80 81 82 |
# File 'lib/openapi3_parser/node/context.rb', line 79 def same_data_and_source?(other) input == other.input && source_location == other.source_location end |
#source ⇒ Source
The source file used to provide the data for this node
94 95 96 |
# File 'lib/openapi3_parser/node/context.rb', line 94 def source source_location.source end |
#to_s ⇒ String
A string representing the location of the node
116 117 118 |
# File 'lib/openapi3_parser/node/context.rb', line 116 def to_s location_summary end |