Class: Jimmy::Json::Pointer
- Inherits:
-
Object
- Object
- Jimmy::Json::Pointer
- Defined in:
- lib/jimmy/json/pointer.rb
Overview
Represents a JSON pointer per tools.ietf.org/html/rfc6901
Constant Summary collapse
- ESCAPE =
[%r{[~/]}, { '~' => '~0', '/' => '~1' }.freeze].freeze
- UNESCAPE =
[/~[01]/, ESCAPE.last.invert.freeze].freeze
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Returns true if
other
has the same string value as self. -
#empty? ⇒ true, false
Returns true if the pointer has no parts.
-
#initialize(path) ⇒ Pointer
constructor
A new instance of Pointer.
- #inspect ⇒ Object
-
#join(other) ⇒ Pointer
(also: #+)
Make a new pointer by appending
other
to self. -
#remove_prefix(other) ⇒ Object
Return a new pointer with just the part of self that is not included in
other
. -
#shed(count) ⇒ Pointer
(also: #-)
Make a new pointer by removing
count
parts from the end of self. -
#shift ⇒ String
Remove the last part of the pointer.
-
#to_a ⇒ Array<String>
The individual parts of the pointer.
-
#to_s ⇒ String
Get the pointer as a string, either blank, or starting with a
/
.
Constructor Details
#initialize(path) ⇒ Pointer
Returns a new instance of Pointer.
12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Method Details
#==(other) ⇒ true, false
Returns true if other
has the same string value as self.
70 71 72 |
# File 'lib/jimmy/json/pointer.rb', line 70 def ==(other) other.is_a?(self.class) && @path == other.to_a end |
#empty? ⇒ true, false
Returns true if the pointer has no parts.
81 82 83 |
# File 'lib/jimmy/json/pointer.rb', line 81 def empty? @path.empty? end |
#inspect ⇒ Object
75 76 77 |
# File 'lib/jimmy/json/pointer.rb', line 75 def inspect "#<#{self.class} #{self}>" end |
#join(other) ⇒ Pointer Also known as: +
Make a new pointer by appending other
to self.
31 32 33 34 35 36 37 38 39 40 |
# 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 |
#remove_prefix(other) ⇒ Object
97 98 99 100 101 102 103 |
# 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 |
#shed(count) ⇒ Pointer Also known as: -
Make a new pointer by removing count
parts from the end of self.
47 48 49 50 51 52 53 54 55 |
# 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 |
#shift ⇒ String
Remove the last part of the pointer.
87 88 89 |
# File 'lib/jimmy/json/pointer.rb', line 87 def shift @path.shift end |
#to_a ⇒ Array<String>
Returns The individual parts of the pointer.
23 24 25 |
# File 'lib/jimmy/json/pointer.rb', line 23 def to_a @path.dup end |
#to_s ⇒ String
Get the pointer as a string, either blank, or starting with a /
.
61 62 63 64 65 |
# File 'lib/jimmy/json/pointer.rb', line 61 def to_s return '' if @path.empty? @path.map { |str| '/' + str.gsub(*ESCAPE) }.join end |