Class: OpenapiContracts::Doc::Schema
- Inherits:
-
Object
- Object
- OpenapiContracts::Doc::Schema
- Defined in:
- lib/openapi_contracts/doc/schema.rb
Overview
Represents a part or the whole schema Generally even parts of the schema contain the whole schema, but store the pointer to their position in the overall schema. This allows even small sub-schemas to resolve links to any other part of the schema
Instance Attribute Summary collapse
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #at_pointer(pointer) ⇒ Object
-
#each ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#follow_refs ⇒ Object
Resolves Schema ref pointers links like “$ref: #/some/path” and returns new sub-schema at the target if the current schema is only a ref link.
-
#fragment ⇒ Object
Generates a fragment pointer for the current schema path.
-
#initialize(raw, pointer = Doc::Pointer[]) ⇒ Schema
constructor
A new instance of Schema.
-
#inspect ⇒ Object
:nocov:.
- #navigate(*segments) ⇒ Object
- #openapi_version ⇒ Object
- #presence ⇒ Object
-
#resolve ⇒ Object
Returns the actual sub-specification contents at the pointer of this Specification.
Constructor Details
Instance Attribute Details
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
7 8 9 |
# File 'lib/openapi_contracts/doc/schema.rb', line 7 def pointer @pointer end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
7 8 9 |
# File 'lib/openapi_contracts/doc/schema.rb', line 7 def raw @raw end |
Instance Method Details
#at_pointer(pointer) ⇒ Object
63 64 65 |
# File 'lib/openapi_contracts/doc/schema.rb', line 63 def at_pointer(pointer) self.class.new(raw, pointer) end |
#each ⇒ Object
rubocop:disable Metrics/MethodLength
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/openapi_contracts/doc/schema.rb', line 16 def each # rubocop:disable Metrics/MethodLength data = resolve case data when Array enum = data.each_with_index Enumerator.new(enum.size) do |yielder| loop do _item, index = enum.next yielder << navigate(index.to_s) end end when Hash enum = data.each_key Enumerator.new(enum.size) do |yielder| loop do key = enum.next yielder << [key, navigate(key)] end end end end |
#follow_refs ⇒ Object
Resolves Schema ref pointers links like “$ref: #/some/path” and returns new sub-schema at the target if the current schema is only a ref link.
46 47 48 49 50 51 52 53 |
# File 'lib/openapi_contracts/doc/schema.rb', line 46 def follow_refs data = resolve if data.is_a?(Hash) && data.key?('$ref') at_pointer Doc::Pointer.from_json_pointer(data['$ref']) else self end end |
#fragment ⇒ Object
Generates a fragment pointer for the current schema path
56 57 58 |
# File 'lib/openapi_contracts/doc/schema.rb', line 56 def fragment pointer.to_json_schemer_pointer end |
#inspect ⇒ Object
:nocov:
39 40 41 |
# File 'lib/openapi_contracts/doc/schema.rb', line 39 def inspect "<#{self.class.name} @pointer=#{@pointer.inspect}>" end |
#navigate(*segments) ⇒ Object
80 81 82 |
# File 'lib/openapi_contracts/doc/schema.rb', line 80 def navigate(*segments) self.class.new(@raw, pointer.navigate(segments)).follow_refs end |
#openapi_version ⇒ Object
67 68 69 |
# File 'lib/openapi_contracts/doc/schema.rb', line 67 def openapi_version @raw['openapi']&.then { |v| Gem::Version.new(v) } end |
#presence ⇒ Object
71 72 73 |
# File 'lib/openapi_contracts/doc/schema.rb', line 71 def presence resolve.present? ? self : nil end |
#resolve ⇒ Object
Returns the actual sub-specification contents at the pointer of this Specification
76 77 78 |
# File 'lib/openapi_contracts/doc/schema.rb', line 76 def resolve @pointer.walk(@raw) end |