Class: JsonReference::Reference

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/json_reference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref) ⇒ Reference

Returns a new instance of Reference.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/json_reference.rb', line 15

def initialize(ref)
  # Note that the #to_s of `nil` is an empty string.
  @uri = nil

  # given a simple fragment without '#', resolve as a JSON Pointer only as
  # per spec
  if ref.include?("#")
    uri, @pointer = ref.split('#')
    if uri && !uri.empty?
      @uri = URI.parse(uri)
    end
    @pointer ||= ""
  else
    @pointer = ref
  end

  # normalize pointers by prepending "#" and stripping trailing "/"
  @pointer = "#" + @pointer
  @pointer = @pointer.chomp("/")
end

Instance Attribute Details

#pointerObject

Returns the value of attribute pointer.



12
13
14
# File 'lib/json_reference.rb', line 12

def pointer
  @pointer
end

#uriObject

Returns the value of attribute uri.



13
14
15
# File 'lib/json_reference.rb', line 13

def uri
  @uri
end

Instance Method Details

#<=>(other) ⇒ Object



36
37
38
# File 'lib/json_reference.rb', line 36

def <=>(other)
  to_s <=> other.to_s
end

#inspectObject



40
41
42
# File 'lib/json_reference.rb', line 40

def inspect
  "\#<JsonReference::Reference #{to_s}>"
end

#resolve_pointer(data) ⇒ Object

Given the document addressed by #uri, resolves the JSON Pointer part of the reference.



46
47
48
# File 'lib/json_reference.rb', line 46

def resolve_pointer(data)
  JsonPointer.evaluate(data, @pointer)
end

#to_sObject



50
51
52
53
54
55
56
# File 'lib/json_reference.rb', line 50

def to_s
  if @uri
    "#{@uri.to_s}#{@pointer}"
  else
    @pointer
  end
end