Class: Openapi3Parser::Node::Context

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Context.

Parameters:



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_locationSource::Location (readonly)

The location in the root source of this node

Returns:



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

def document_location
  @document_location
end

#inputAny (readonly)

The raw data that was used to build the node

Returns:

  • (Any)

    the current value of input



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

def input
  @input
end

#source_locationSource::Location (readonly)

The location in a source file of this

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:

  • (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

#documentDocument

The OpenAPI document associated with this context

Returns:



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

def document
  document_location.source.document
end

#inspectString

Returns:

  • (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_summaryString

A string representing the location of the node

Returns:

  • (String)


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

#nodeNode::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_nodeNode::Object, ...

Return the node that is the parent node for the node at this context

Returns:



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)

Parameters:

Returns:

  • anything



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_inputAny

Used to return the data at this document location with all references resolved and optional fields populated with defaults

Returns:

  • (Any)


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

Parameters:

Returns:

  • (Boolean)


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

#sourceSource

The source file used to provide the data for this node

Returns:



94
95
96
# File 'lib/openapi3_parser/node/context.rb', line 94

def source
  source_location.source
end

#to_sString

A string representing the location of the node

Returns:

  • (String)


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

def to_s
  location_summary
end