Class: OpenapiContracts::Doc::Pointer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments) ⇒ Pointer

Returns a new instance of Pointer.



17
18
19
# File 'lib/openapi_contracts/doc/pointer.rb', line 17

def initialize(segments)
  @segments = segments
end

Class Method Details

.[](*segments) ⇒ Object



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

def self.[](*segments)
  new Array.wrap(segments).flatten
end

.from_json_pointer(str) ⇒ Object

Raises:

  • (ArguementError)


7
8
9
10
11
# File 'lib/openapi_contracts/doc/pointer.rb', line 7

def self.from_json_pointer(str)
  raise ArguementError unless %r{^#/(?<pointer>.*)} =~ str

  new(pointer.split('/').map { |s| s.gsub('~1', '/') })
end

.from_path(pathname) ⇒ Object



13
14
15
# File 'lib/openapi_contracts/doc/pointer.rb', line 13

def self.from_path(pathname)
  new pathname.to_s.split('/')
end

Instance Method Details

#==(other) ⇒ Object



63
64
65
# File 'lib/openapi_contracts/doc/pointer.rb', line 63

def ==(other)
  to_a == other.to_a
end

#inspectObject



21
22
23
# File 'lib/openapi_contracts/doc/pointer.rb', line 21

def inspect
  "<#{self.class.name}#{to_a}>"
end


27
28
29
# File 'lib/openapi_contracts/doc/pointer.rb', line 27

def navigate(*segments)
  self.class[to_a + segments]
end

#parentObject



31
32
33
# File 'lib/openapi_contracts/doc/pointer.rb', line 31

def parent
  self.class[to_a[0..-2]]
end

#to_aObject



35
36
37
# File 'lib/openapi_contracts/doc/pointer.rb', line 35

def to_a
  @segments
end

#to_json_pointerObject



39
40
41
# File 'lib/openapi_contracts/doc/pointer.rb', line 39

def to_json_pointer
  escaped_segments.join('/').then { |s| "#/#{s}" }
end

#to_json_schemer_pointerObject



43
44
45
# File 'lib/openapi_contracts/doc/pointer.rb', line 43

def to_json_schemer_pointer
  www_escaped_segments.join('/').then { |s| "#/#{s}" }
end

#walk(object) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openapi_contracts/doc/pointer.rb', line 47

def walk(object)
  return object if empty?

  @segments.inject(object) do |obj, key|
    return nil unless obj

    if obj.is_a?(Array)
      raise ArgumentError unless /^\d+$/ =~ key

      key = key.to_i
    end

    obj[key]
  end
end