Sha256: aaa95c71d2824224e028169a66e0228f9c653d916a922b013f7fb6b3e66b2aaf

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require "forwardable"

module Openapi3Parser
  module Node
    # An array within a OpenAPI document.
    # Very similar to a normal Ruby array, however this is read only and knows
    # the context of where it sits in an OpenAPI document
    #
    # The contents of the data will be dependent on where this document is in
    # the document hierachy.
    class Array
      extend Forwardable
      include Enumerable

      def_delegators :node_data, :empty?, :length, :size
      attr_reader :node_data, :node_context

      # @param [::Array] data     data used to populate this node
      # @param [Context] context  The context of this node in the document
      def initialize(data, context)
        @node_data = data
        @node_context = context
      end

      def [](index)
        Placeholder.resolve(node_data[index])
      end

      # Iterates through the data of this node, used by Enumerable
      #
      # @return [Object]
      def each(&block)
        Placeholder.each(node_data, &block)
      end

      # @param [Any]  other
      #
      # @return [Boolean]
      def ==(other)
        other.instance_of?(self.class) &&
          node_context.same_data_and_source?(other.node_context)
      end

      # Used to access a node relative to this node
      # @param  [Source::Pointer, ::Array, ::String] pointer_like
      # @return anything
      def node_at(pointer_like)
        current_pointer = node_context.document_location.pointer
        node_context.document.node_at(pointer_like, current_pointer)
      end

      # @return [String]
      def inspect
        fragment = node_context.document_location.pointer.fragment
        %{#{self.class.name}(#{fragment})}
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
openapi3_parser-0.9.2 lib/openapi3_parser/node/array.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/openapi3_parser-0.9.1/lib/openapi3_parser/node/array.rb
openapi3_parser-0.9.1 lib/openapi3_parser/node/array.rb
openapi3_parser-0.9.0 lib/openapi3_parser/node/array.rb
openapi3_parser-0.8.2 lib/openapi3_parser/node/array.rb
openapi3_parser-0.8.1 lib/openapi3_parser/node/array.rb
openapi3_parser-0.8.0 lib/openapi3_parser/node/array.rb