Skip to content

Commit ef5df10

Browse files
committed
add class and method documentation to JSON::Schema::Pointer class
1 parent 5784dbf commit ef5df10

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/json-schema/pointer.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module JSON
44
class Schema
5+
# a JSON Pointer, as described by RFC 6901 https://tools.ietf.org/html/rfc6901
56
class Pointer
67
class Error < JSON::Schema::SchemaError
78
end
@@ -10,6 +11,15 @@ class PointerSyntaxError < Error
1011
class ReferenceError < Error
1112
end
1213

14+
# parse a fragment to an array of reference tokens
15+
#
16+
# #/foo/bar
17+
#
18+
# => ['foo', 'bar']
19+
#
20+
# #/foo%20bar
21+
#
22+
# => ['foo bar']
1323
def self.parse_fragment(fragment)
1424
fragment = Addressable::URI.unescape(fragment)
1525
match = fragment.match(/\A#/)
@@ -20,6 +30,15 @@ def self.parse_fragment(fragment)
2030
end
2131
end
2232

33+
# parse a pointer to an array of reference tokens
34+
#
35+
# /foo
36+
#
37+
# => ['foo']
38+
#
39+
# /foo~0bar/baz~1qux
40+
#
41+
# => ['foo~bar', 'baz/qux']
2342
def self.parse_pointer(pointer_string)
2443
tokens = pointer_string.split('/', -1).map! do |piece|
2544
piece.gsub('~1', '/').gsub('~0', '~')
@@ -31,6 +50,13 @@ def self.parse_pointer(pointer_string)
3150
end
3251
end
3352

53+
# initializes a JSON::Schema::Pointer from the given representation.
54+
#
55+
# type may be one of:
56+
#
57+
# - :fragment - the representation is a fragment containing a pointer (starting with #)
58+
# - :pointer - the representation is a pointer (starting with /)
59+
# - :reference_tokens - the representation is an array of tokens referencing a path in a document
3460
def initialize(type, representation)
3561
@type = type
3662
if type == :reference_tokens
@@ -47,6 +73,8 @@ def initialize(type, representation)
4773

4874
attr_reader :reference_tokens
4975

76+
# takes a root json document and evaluates this pointer through the document, returning the value
77+
# pointed to by this pointer.
5078
def evaluate(document)
5179
reference_tokens.each.inject(document) do |value, token|
5280
if value.is_a?(Array)

0 commit comments

Comments
 (0)