Class: JSONP3::JSONPointer

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

Overview

Identify a single value in JSON-like data, as per RFC 6901.

Constant Summary collapse

RE_INT =
/\A(0|[1-9][0-9]*)\z/
UNDEFINED =
:__undefined

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ JSONPointer

Returns a new instance of JSONPointer.

Parameters:

  • pointer (String)


27
28
29
30
# File 'lib/json_p3/pointer.rb', line 27

def initialize(pointer)
  @tokens = parse(pointer)
  @pointer = JSONPointer.encode(@tokens)
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



11
12
13
# File 'lib/json_p3/pointer.rb', line 11

def tokens
  @tokens
end

Class Method Details

.encode(tokens) ⇒ String

Encode an array of strings and integers into a JSON Pointer.

Parameters:

  • tokens (Array<String | Integer> | nil)

Returns:

  • (String)


16
17
18
19
20
21
22
23
24
# File 'lib/json_p3/pointer.rb', line 16

def self.encode(tokens)
  return "" if tokens.nil? || tokens.empty?

  encoded = tokens.map do |token|
    token.is_a?(Integer) ? token.to_s : token.gsub("~", "~0").gsub("/", "~1")
  end

  "/#{encoded.join("/")}"
end

Instance Method Details

#exist?(value) ⇒ Boolean

Return true if this pointer can be resolved against value, even if the resolved value is false or nil.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


85
86
87
# File 'lib/json_p3/pointer.rb', line 85

def exist?(value)
  resolve(value) != UNDEFINED
end

#join(*parts) ⇒ JSONPointer

Parameters:

  • parts (String)

Returns:



74
75
76
77
78
79
80
# File 'lib/json_p3/pointer.rb', line 74

def join(*parts)
  pointer = self
  parts.each do |part|
    pointer = pointer._join(part)
  end
  pointer
end

#parentObject

Return this pointer's parent as a new pointer. If this pointer points to the document root, self is returned.



91
92
93
94
95
# File 'lib/json_p3/pointer.rb', line 91

def parent
  return self if @tokens.empty?

  JSONPointer.new(JSONPointer.encode((@tokens[...-1] || raise)))
end

#relative_to?(pointer) ⇒ bool

Return true if this pointer is relative to pointer.

Parameters:

Returns:

  • (bool)


68
69
70
# File 'lib/json_p3/pointer.rb', line 68

def relative_to?(pointer)
  pointer.tokens.length < @tokens.length && @tokens[...pointer.tokens.length] == pointer.tokens
end

#resolve(value, default: UNDEFINED) ⇒ Object

Resolve this pointer against JSON-like data value.

Parameters:

  • value (Object)
  • default (Object) (defaults to: UNDEFINED)

    the value to return if this pointer can not be resolved against value.



36
37
38
39
40
41
42
43
44
45
# File 'lib/json_p3/pointer.rb', line 36

def resolve(value, default: UNDEFINED)
  item = value

  @tokens.each do |token|
    item = get_item(item, token)
    return default if item == UNDEFINED
  end

  item
end

#resolve_with_parent(value) ⇒ Array<Object>

Resolve this pointer against value, returning the resolved object and its parent object.

Parameters:

  • value (Object)

Returns:

  • (Array<Object>)

    an array with exactly two elements, one or both of which could be undefined.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json_p3/pointer.rb', line 53

def resolve_with_parent(value)
  return [UNDEFINED, resolve(value)] if @tokens.empty?

  parent = value
  (@tokens[...-1] || raise).each do |token|
    parent = get_item(parent, token)
    break if parent == UNDEFINED
  end

  [parent, get_item(parent, @tokens.last)]
end

#to(rel) ⇒ JSONPointer

Return a new pointer relative to this pointer using Relative JSON Pointer syntax.

Parameters:

Returns:



100
101
102
103
# File 'lib/json_p3/pointer.rb', line 100

def to(rel)
  p = rel.is_a?(String) ? RelativeJSONPointer.new(rel) : rel
  p.to(self)
end

#to_sObject



105
106
107
# File 'lib/json_p3/pointer.rb', line 105

def to_s
  @pointer
end