Class: JSONP3::JSONPointer
- Inherits:
-
Object
- Object
- JSONP3::JSONPointer
- 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
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
-
.encode(tokens) ⇒ String
Encode an array of strings and integers into a JSON Pointer.
Instance Method Summary collapse
-
#exist?(value) ⇒ Boolean
Return true if this pointer can be resolved against value, even if the resolved value is false or nil.
-
#initialize(pointer) ⇒ JSONPointer
constructor
A new instance of JSONPointer.
- #join(*parts) ⇒ JSONPointer
-
#parent ⇒ Object
Return this pointer's parent as a new pointer.
-
#relative_to?(pointer) ⇒ bool
Return true if this pointer is relative to pointer.
-
#resolve(value, default: UNDEFINED) ⇒ Object
Resolve this pointer against JSON-like data value.
-
#resolve_with_parent(value) ⇒ Array<Object>
Resolve this pointer against value, returning the resolved object and its parent object.
-
#to(rel) ⇒ JSONPointer
Return a new pointer relative to this pointer using Relative JSON Pointer syntax.
- #to_s ⇒ Object
Constructor Details
#initialize(pointer) ⇒ JSONPointer
Returns a new instance of JSONPointer.
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
#tokens ⇒ Object (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.
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.
85 86 87 |
# File 'lib/json_p3/pointer.rb', line 85 def exist?(value) resolve(value) != UNDEFINED end |
#join(*parts) ⇒ JSONPointer
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 |
#parent ⇒ Object
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.
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.
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.
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.
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_s ⇒ Object
105 106 107 |
# File 'lib/json_p3/pointer.rb', line 105 def to_s @pointer end |