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
- #as_h ⇒ Object
- #at_pointer(pointer) ⇒ Object
-
#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 = nil) ⇒ Schema
constructor
A new instance of Schema.
- #navigate(*spointer) ⇒ Object
Constructor Details
#initialize(raw, pointer = nil) ⇒ Schema
Returns a new instance of Schema.
9 10 11 12 |
# File 'lib/openapi_contracts/doc/schema.rb', line 9 def initialize(raw, pointer = nil) @raw = raw @pointer = pointer.freeze end |
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
#as_h ⇒ Object
35 36 37 38 39 |
# File 'lib/openapi_contracts/doc/schema.rb', line 35 def as_h return @raw if pointer.nil? || pointer.empty? @raw.dig(*pointer) end |
#at_pointer(pointer) ⇒ Object
31 32 33 |
# File 'lib/openapi_contracts/doc/schema.rb', line 31 def at_pointer(pointer) self.class.new(raw, pointer) 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.
16 17 18 19 20 21 22 |
# File 'lib/openapi_contracts/doc/schema.rb', line 16 def follow_refs if (ref = as_h['$ref']) at_pointer(ref.split('/')[1..]) else self end end |
#fragment ⇒ Object
Generates a fragment pointer for the current schema path
25 26 27 |
# File 'lib/openapi_contracts/doc/schema.rb', line 25 def fragment pointer.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" } end |
#navigate(*spointer) ⇒ Object
41 42 43 |
# File 'lib/openapi_contracts/doc/schema.rb', line 41 def navigate(*spointer) self.class.new(@raw, (pointer + Array.wrap(spointer))) end |