Class: Jimmy::Json::Pointer

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(path) ⇒ Pointer

Returns a new instance of Pointer.

Parameters:

  • path (::Array<String>, Pointer, String)

    A string starting with a / and in JSON pointer format, or an array of parts of the 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.

Parameters:

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


81
82
83
# File 'lib/jimmy/json/pointer.rb', line 81

def empty?
  @path.empty?
end

#inspectObject

See Also:

  • Object#inspect


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.

Parameters:

  • other (Pointer, Integer, String, ::Array<String>)

    The pointer to append.

Returns:



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

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>

Parameters:

  • other (String, Pointer, ::Array<String>)


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.

Parameters:

  • count (Integer)

Returns:

Raises:



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

#shiftString

Remove the last part of the pointer.

Returns:

  • (String)

    The part that was removed.



87
88
89
# File 'lib/jimmy/json/pointer.rb', line 87

def shift
  @path.shift
end

#to_aArray<String>

Returns The individual parts of the pointer.

Returns:

  • (Array<String>)

    The individual parts of the pointer.



23
24
25
# File 'lib/jimmy/json/pointer.rb', line 23

def to_a
  @path.dup
end

#to_sString

Get the pointer as a string, either blank, or starting with a /.

Returns:

  • (String)


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