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.

Constant Summary collapse

UNDEFINED =
Class.new

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:



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

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.



64
65
66
# File 'lib/openapi3_parser/node_factory/context.rb', line 64

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, given_input = UNDEFINED) ⇒ 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)
  • input (Any)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openapi3_parser/node_factory/context.rb', line 36

def self.next_field(parent_context, field, given_input = UNDEFINED)
  pc = parent_context
  input = if given_input == UNDEFINED
            pc.input.respond_to?(:[]) ? pc.input[field] : nil
          else
            given_input
          end
  source_location = Source::Location.next_field(pc.source_location, field)
  new(input,
      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:



54
55
56
57
58
59
60
61
62
# File 'lib/openapi3_parser/node_factory/context.rb', line 54

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:,
      reference_locations:)
end

.root(input, source) ⇒ Context

Create a context for the root of a document

Parameters:

Returns:



22
23
24
# File 'lib/openapi3_parser/node_factory/context.rb', line 22

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

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#location_summaryObject



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

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:



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

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

#self_referencing?Boolean

Used to show when an recursive reference loop has begun

Returns:

  • (Boolean)


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

def self_referencing?
  reference_locations.include?(source_location)
end

#sourceSource

Returns:



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

def source
  source_location.source
end

#to_sObject



113
114
115
# File 'lib/openapi3_parser/node_factory/context.rb', line 113

def to_s
  location_summary
end