Class: OpenapiContracts::Doc::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_contracts/doc/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, path = nil) ⇒ Schema

Returns a new instance of Schema.



5
6
7
8
# File 'lib/openapi_contracts/doc/schema.rb', line 5

def initialize(schema, path = nil)
  @schema = schema
  @path = path.freeze
end

Instance Attribute Details

#path ⇒ Object (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/openapi_contracts/doc/schema.rb', line 3

def path
  @path
end

#schema ⇒ Object (readonly)

Returns the value of attribute schema.



3
4
5
# File 'lib/openapi_contracts/doc/schema.rb', line 3

def schema
  @schema
end

Instance Method Details

#at_path(path) ⇒ Object



24
25
26
# File 'lib/openapi_contracts/doc/schema.rb', line 24

def at_path(path)
  self.class.new(schema, path)
end

#follow_refs ⇒ Object



10
11
12
13
14
15
16
# File 'lib/openapi_contracts/doc/schema.rb', line 10

def follow_refs
  if (ref = dig('$ref'))
    at_path(ref.split('/')[1..])
  else
    self
  end
end

#fragment ⇒ Object



18
19
20
# File 'lib/openapi_contracts/doc/schema.rb', line 18

def fragment
  path.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" }
end


34
35
36
# File 'lib/openapi_contracts/doc/schema.rb', line 34

def navigate(*sub_path)
  self.class.new(schema, (path + Array.wrap(sub_path)))
end

#to_h ⇒ Object



28
29
30
31
32
# File 'lib/openapi_contracts/doc/schema.rb', line 28

def to_h
  return @schema if path.nil? || path.empty?

  @schema.dig(*path)
end