Class: Openapi3Parser::NodeFactory::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi3_parser/node_factory/context.rb

Overview

This class is used to specify the data and source information for a NodeFactory. The same NodeFactory can be used multiple times if the object is referenced so it is limited in data about it’s location within the document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, source_location:, reference_locations: []) ⇒ Context

Returns a new instance of Context.

Parameters:



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

def initialize(input,
               source_location:,
               reference_locations: [])
  @input = input
  @source_location = source_location
  @reference_locations = reference_locations
end

Instance Attribute Details

#inputAny (readonly)

Returns the current value of input.

Returns:

  • (Any)

    the current value of input



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

def input
  @input
end

#reference_locationsObject (readonly)

Returns the value of attribute reference_locations.



57
58
59
# File 'lib/openapi3_parser/node_factory/context.rb', line 57

def reference_locations
  @reference_locations
end

#refernce_locationsArray<Source::Location> (readonly)

Returns the current value of refernce_locations.

Returns:



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

def refernce_locations
  @refernce_locations
end

#source_locationSource::Location (readonly)

Returns the current value of source_location.

Returns:



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

def source_location
  @source_location
end

Class Method Details

.next_field(parent_context, field) ⇒ Context

Create a factory 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:



33
34
35
36
37
38
39
40
# File 'lib/openapi3_parser/node_factory/context.rb', line 33

def self.next_field(parent_context, field)
  pc = parent_context
  input = pc.input.respond_to?(:[]) ? pc.input[field] : nil
  source_location = Source::Location.next_field(pc.source_location, field)
  new(input,
      source_location: source_location,
      reference_locations: pc.reference_locations)
end

.resolved_reference(reference_context, source_location:) ⇒ Context

Creates the context for a field that references another field

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/openapi3_parser/node_factory/context.rb', line 47

def self.resolved_reference(reference_context, source_location:)
  reference_locations = [reference_context.source_location] +
                        reference_context.reference_locations

  data = source_location.data if source_location.source_available?
  new(data,
      source_location: source_location,
      reference_locations: reference_locations)
end

.root(input, source) ⇒ Context

Create a context for the root of a document

Parameters:

Returns:



20
21
22
# File 'lib/openapi3_parser/node_factory/context.rb', line 20

def self.root(input, source)
  new(input, source_location: Source::Location.new(source, []))
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/openapi3_parser/node_factory/context.rb', line 71

def ==(other)
  input == other.input &&
    source_location == other.source_location &&
    reference_locations == other.reference_locations
end

#inspectObject



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

def inspect
  %{#{self.class.name}(source_location: #{source_location}, } +
    %{referenced_by: #{reference_locations.map(&:to_s).join(', ')})}
end

#location_summaryObject



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

def location_summary
  source_location.to_s
end

#resolve_reference(reference, factory, recursive: false) ⇒ Source::ResolvedReference

Parameters:

  • reference (String)
  • factory (Object, Map, Array)
  • recursive (Boolean) (defaults to: false)

Returns:



86
87
88
# File 'lib/openapi3_parser/node_factory/context.rb', line 86

def resolve_reference(reference, factory, recursive: false)
  source.resolve_reference(reference, factory, self, recursive: recursive)
end

#self_referencing?Boolean

Used to show when an recursive reference loop has begun

Returns:

  • (Boolean)


93
94
95
# File 'lib/openapi3_parser/node_factory/context.rb', line 93

def self_referencing?
  reference_locations.include?(source_location)
end

#sourceSource

Returns:



78
79
80
# File 'lib/openapi3_parser/node_factory/context.rb', line 78

def source
  source_location.source
end

#to_sObject



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

def to_s
  location_summary
end