Class: JSONSkooma::JSONPointer

Inherits:
Hana::Pointer
  • Object
show all
Defined in:
lib/json_skooma/json_pointer.rb

Constant Summary collapse

ESC_REGEX =
/[\/^~]/.freeze
ESC2 =
{"^" => "^^", "~" => "~0", "/" => "~1"}.freeze
ESCAPE_REGEX =
/([^ a-zA-Z0-9_.\-~\/!$&'()*+,;=]+)/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JSONPointer

Returns a new instance of JSONPointer.



24
25
26
27
28
29
30
# File 'lib/json_skooma/json_pointer.rb', line 24

def initialize(path)
  if path.is_a?(Array)
    @path = path
  else
    super(CGI.unescape(path))
  end
end

Class Method Details

.new_root(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json_skooma/json_pointer.rb', line 12

def self.new_root(path)
  return [""] if path == ""

  parts = ((path.include?("%") || path.include?("+")) ? CGI.unescape(path) : path).split(/(?<!\^)\//).each { |part|
    part.gsub!(/\^[\/^]|~[01]/) { |m| ESC[m] }
  }

  parts.push("") if path.end_with?("/")

  new(parts)
end

Instance Method Details

#<<(part) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/json_skooma/json_pointer.rb', line 36

def <<(part)
  case part
  when Array
    @path.concat(part.map(&:to_s))
  when JSONPointer
    @path.concat(part.path)
  else
    @path << part.to_s
  end
  self
end

#==(other) ⇒ Object



62
63
64
65
# File 'lib/json_skooma/json_pointer.rb', line 62

def ==(other)
  return super unless other.is_a?(self.class)
  other.path == path
end

#child(key) ⇒ Object



32
33
34
# File 'lib/json_skooma/json_pointer.rb', line 32

def child(key)
  self.class.new(@path.dup.push(key))
end

#to_sObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/json_skooma/json_pointer.rb', line 48

def to_s
  return @to_s if instance_variable_defined?(:@to_s)

  @to_s =
    case @path
    when []
      ""
    when [""]
      "/"
    else
      "/" + @path.map(&method(:escape)).join("/")
    end
end