class Jimmy::Json::Pointer
Represents a JSON pointer per tools.ietf.org/html/rfc6901
Constants
- ESCAPE
- UNESCAPE
Public Class Methods
@param [::Array<String>, Pointer
, String] path A string starting with
a +/+ and in JSON pointer format, or an array of parts of the pointer.
# File lib/jimmy/json/pointer.rb, line 12 def initialize(path) @path = case path when ::Array, Pointer then path.to_a when String then parse(path) else raise Error::WrongType, "Unexpected #{path.class}" end end
Public Instance Methods
Returns true if other
has the same string value as self. @param [Pointer] other @return [true, false]
# File lib/jimmy/json/pointer.rb, line 70 def ==(other) other.is_a?(self.class) && @path == other.to_a end
Returns true if the pointer has no parts. @return [true, false]
# File lib/jimmy/json/pointer.rb, line 81 def empty? @path.empty? end
@see ::Object#inspect
# File lib/jimmy/json/pointer.rb, line 75 def inspect "#<#{self.class} #{self}>" end
Make a new pointer by appending other
to self. @param [Pointer, Integer, String, ::Array<String>] other The pointer to
append.
@return [Pointer]
# File lib/jimmy/json/pointer.rb, line 31 def join(other) if other.is_a? Integer return shed(-other) if other.negative? other = other.to_s end other = '/' + other if other.is_a?(String) && other[0] != '/' self.class.new(@path + self.class.new(other).to_a) end
Return a new pointer with just the part of self that is not included in other
.
Jimmy::Json::Pointer.new('/foo/bar/baz').remove_prefix('/foo') # => #<Jimmy::Json::Pointer /bar/baz>
@param [String, Pointer
, ::Array<String>] other
# File lib/jimmy/json/pointer.rb, line 97 def remove_prefix(other) tail = dup Pointer.new(other).to_a.each do |segment| return nil unless tail.shift == segment end tail end
Make a new pointer by removing count
parts from the end of self. @param [Integer] count @return [Pointer]
# File lib/jimmy/json/pointer.rb, line 47 def shed(count) unless count.is_a?(Integer) && !count.negative? raise Error::BadArgument, 'Expected a non-negative integer' end return dup if count.zero? raise Error::BadArgument, 'Out of range' if count > @path.length self.class.new @path[0..(-count - 1)] end
Remove the last part of the pointer. @return [String] The part that was removed.
# File lib/jimmy/json/pointer.rb, line 87 def shift @path.shift end
@return [Array<String>] The individual parts of the pointer.
# File lib/jimmy/json/pointer.rb, line 23 def to_a @path.dup end
Get the pointer as a string, either blank, or starting with a /
. @return [String]
# File lib/jimmy/json/pointer.rb, line 61 def to_s return '' if @path.empty? @path.map { |str| '/' + str.gsub(*ESCAPE) }.join end
Private Instance Methods
# File lib/jimmy/json/pointer.rb, line 107 def parse(path) return [] if path == '' return [''] if path == '/' unless path[0] == '/' raise Error::BadArgument, 'JSON pointers should start with /' end path[1..].split('/').map { |str| str.gsub *UNESCAPE } end