Skip to content

Commit

Permalink
JSI::Ptr renamed from JSI::JSON::Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Mar 23, 2021
1 parent 9c2bf1d commit 61463a9
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 103 deletions.
2 changes: 1 addition & 1 deletion lib/jsi.rb
Expand Up @@ -24,7 +24,7 @@ class Bug < NotImplementedError
ROOT_PATH = Pathname.new(__FILE__).dirname.parent.expand_path
RESOURCES_PATH = ROOT_PATH.join('resources')

autoload :JSON, 'jsi/json'
autoload :Ptr, 'jsi/ptr'
autoload :PathedNode, 'jsi/pathed_node'
autoload :Typelike, 'jsi/typelike_modules'
autoload :Hashlike, 'jsi/typelike_modules'
Expand Down
8 changes: 4 additions & 4 deletions lib/jsi/base.rb
Expand Up @@ -111,7 +111,7 @@ def name
end

# NOINSTANCE is a magic value passed to #initialize when instantiating a JSI
# from a document and JSON Pointer.
# from a document and pointer.
#
# @private
NOINSTANCE = Object.new
Expand All @@ -127,7 +127,7 @@ def name
# node in the `jsi_document` param, pointed to by `jsi_ptr`. the param `instance`
# MUST be `NOINSTANCE` to use the jsi_document + jsi_ptr form. `jsi_document` MUST
# NOT be passed if `instance` is anything other than `NOINSTANCE`.
# @param jsi_ptr [JSI::JSON::Pointer] for internal use. a JSON pointer specifying
# @param jsi_ptr [JSI::Ptr] for internal use. a pointer specifying
# the path of this instance in the `jsi_document` param. `jsi_ptr` must be passed
# iff `jsi_document` is passed, i.e. when `instance` is `NOINSTANCE`
# @param jsi_root_node [JSI::Base] for internal use, specifies the JSI at the root of the document
Expand Down Expand Up @@ -168,7 +168,7 @@ def initialize(instance,
else
raise(Bug, 'incorrect usage') if jsi_document || jsi_ptr || jsi_root_node
@jsi_document = instance
@jsi_ptr = JSI::JSON::Pointer[]
@jsi_ptr = Ptr[]
@jsi_root_node = self
end

Expand All @@ -186,7 +186,7 @@ def initialize(instance,
# document containing the instance of this JSI
attr_reader :jsi_document

# JSI::JSON::Pointer pointing to this JSI's instance within the jsi_document
# JSI::Ptr pointing to this JSI's instance within the jsi_document
attr_reader :jsi_ptr

# the JSI at the root of this JSI's document
Expand Down
2 changes: 1 addition & 1 deletion lib/jsi/json-schema-fragments.rb
Expand Up @@ -45,7 +45,7 @@ def initialize(schema_data, data, opts={})
def schema_from_fragment(base_schema, fragment)
schema_uri = base_schema.uri

pointer = JSI::JSON::Pointer.from_fragment(fragment)
pointer = JSI::Ptr.from_fragment(fragment)

base_schema = JSON::Schema.new(pointer.evaluate(base_schema.schema), schema_uri, @options[:version])

Expand Down
7 changes: 0 additions & 7 deletions lib/jsi/json.rb

This file was deleted.

14 changes: 7 additions & 7 deletions lib/jsi/metaschema_node.rb
Expand Up @@ -24,19 +24,19 @@ class MetaschemaNode < Base
autoload :BootstrapSchema, 'jsi/metaschema_node/bootstrap_schema'

# @param jsi_document the document containing the metaschema
# @param jsi_ptr [JSI::JSON::Pointer] ptr to this MetaschemaNode in jsi_document
# @param jsi_ptr [JSI::Ptr] ptr to this MetaschemaNode in jsi_document
# @param metaschema_instance_modules [Set<Module>] modules which implement the functionality of the
# schema, to be applied to every schema which is an instance of the metaschema. this must include
# JSI::Schema directly or indirectly. these are the {Schema#jsi_schema_instance_modules} of the
# metaschema.
# @param metaschema_root_ptr [JSI::JSON::Pointer] ptr to the root of the metaschema in the jsi_document
# @param root_schema_ptr [JSI::JSON::Pointer] ptr to the schema describing the root of the jsi_document
# @param metaschema_root_ptr [JSI::Ptr] ptr to the root of the metaschema in the jsi_document
# @param root_schema_ptr [JSI::Ptr] ptr to the schema describing the root of the jsi_document
def initialize(
jsi_document,
jsi_ptr: JSI::JSON::Pointer[],
jsi_ptr: Ptr[],
metaschema_instance_modules: ,
metaschema_root_ptr: JSI::JSON::Pointer[],
root_schema_ptr: JSI::JSON::Pointer[],
metaschema_root_ptr: Ptr[],
root_schema_ptr: Ptr[],
jsi_schema_base_uri: nil
)
jsi_initialize_memos
Expand Down Expand Up @@ -140,7 +140,7 @@ def jsi_root_node
self
else
new_node(
jsi_ptr: JSI::JSON::Pointer[],
jsi_ptr: Ptr[],
jsi_schema_base_uri: nil,
)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/jsi/metaschema_node/bootstrap_schema.rb
Expand Up @@ -29,11 +29,11 @@ def inspect
alias_method :to_s, :inspect
end

# @param jsi_ptr [JSI::JSON::Pointer] pointer to the schema in the document
# @param jsi_ptr [JSI::Ptr] pointer to the schema in the document
# @param jsi_document [#to_hash, #to_ary, Boolean, Object] document containing the schema
def initialize(
jsi_document,
jsi_ptr: JSI::JSON::Pointer[],
jsi_ptr: Ptr[],
jsi_schema_base_uri: nil
)
unless respond_to?(:metaschema_instance_modules)
Expand All @@ -50,7 +50,7 @@ def initialize(
# document containing the schema content
attr_reader :jsi_document

# JSI::JSON::Pointer pointing to this schema within the document
# JSI::Ptr pointing to this schema within the document
attr_reader :jsi_ptr

def jsi_node_content
Expand Down
2 changes: 1 addition & 1 deletion lib/jsi/pathed_node.rb
Expand Up @@ -3,7 +3,7 @@
module JSI
# including class MUST define
# - #jsi_document [Object] returning the document
# - #jsi_ptr [JSI::JSON::Pointer] returning a pointer for the node path in the document
# - #jsi_ptr [JSI::Ptr] returning a pointer for the node path in the document
# - #jsi_root_node [JSI::PathedNode] returning a PathedNode pointing at the document root
# - #jsi_parent_node [JSI::PathedNode] returning the parent node of this PathedNode
#
Expand Down
92 changes: 45 additions & 47 deletions lib/jsi/json/pointer.rb → lib/jsi/ptr.rb
@@ -1,73 +1,72 @@
# frozen_string_literal: true

module JSI
module JSON
# a representation to work with JSON Pointer, as described by RFC 6901 https://tools.ietf.org/html/rfc6901
class Pointer
class Ptr
class Error < StandardError
end
class PointerSyntaxError < Error
end
class ReferenceError < Error
end

# @param ary_ptr [#to_ary, JSI::JSON::Pointer] an array of reference tokens, or a pointer
# @return [JSI::JSON::Pointer] a pointer with the given reference tokens, or the given pointer
# @param ary_ptr [#to_ary, JSI::Ptr] an array of reference tokens, or a pointer
# @return [JSI::Ptr] a pointer with the given reference tokens, or the given pointer
def self.ary_ptr(ary_ptr)
if ary_ptr.is_a?(Pointer)
if ary_ptr.is_a?(Ptr)
ary_ptr
else
new(ary_ptr)
end
end

# instantiates a Pointer from the given reference tokens.
# instantiates a pointer from the given reference tokens.
#
# JSI::JSON::Pointer[]
# JSI::Ptr[]
#
# instantes a root pointer.
#
# JSI::JSON::Pointer['a', 'b']
# JSI::JSON::Pointer['a']['b']
# JSI::Ptr['a', 'b']
# JSI::Ptr['a']['b']
#
# are both ways to instantiate a pointer with reference tokens ['a', 'b']. the latter example chains the
# class .[] method with the instance #[] method.
#
# @param reference_tokens any number of reference tokens
# @return [JSI::JSON::Pointer]
# @return [JSI::Ptr]
def self.[](*reference_tokens)
new(reference_tokens)
end

# parse a URI-escaped fragment and instantiate as a JSI::JSON::Pointer
# parse a URI-escaped fragment and instantiate as a JSI::Ptr
#
# JSI::JSON::Pointer.from_fragment('/foo/bar')
# => JSI::JSON::Pointer["foo", "bar"]
# JSI::Ptr.from_fragment('/foo/bar')
# => JSI::Ptr["foo", "bar"]
#
# with URI escaping:
#
# JSI::JSON::Pointer.from_fragment('/foo%20bar')
# => JSI::JSON::Pointer["foo bar"]
# JSI::Ptr.from_fragment('/foo%20bar')
# => JSI::Ptr["foo bar"]
#
# @param fragment [String] a fragment containing a pointer
# @return [JSI::JSON::Pointer]
# @raise [JSI::JSON::Pointer::PointerSyntaxError] when the fragment does not contain a pointer with
# @return [JSI::Ptr]
# @raise [JSI::Ptr::PointerSyntaxError] when the fragment does not contain a pointer with
# valid pointer syntax
def self.from_fragment(fragment)
from_pointer(Addressable::URI.unescape(fragment))
end

# parse a pointer string and instantiate as a JSI::JSON::Pointer
# parse a pointer string and instantiate as a JSI::Ptr
#
# JSI::JSON::Pointer.from_pointer('/foo')
# => JSI::JSON::Pointer["foo"]
# JSI::Ptr.from_pointer('/foo')
# => JSI::Ptr["foo"]
#
# JSI::JSON::Pointer.from_pointer('/foo~0bar/baz~1qux')
# => JSI::JSON::Pointer["foo~bar", "baz/qux"]
# JSI::Ptr.from_pointer('/foo~0bar/baz~1qux')
# => JSI::Ptr["foo~bar", "baz/qux"]
#
# @param pointer_string [String] a pointer string
# @return [JSI::JSON::Pointer]
# @raise [JSI::JSON::Pointer::PointerSyntaxError] when the pointer_string does not have valid pointer syntax
# @return [JSI::Ptr]
# @raise [JSI::Ptr::PointerSyntaxError] when the pointer_string does not have valid pointer syntax
def self.from_pointer(pointer_string)
tokens = pointer_string.split('/', -1).map! do |piece|
piece.gsub('~1', '/').gsub('~0', '~')
Expand All @@ -81,7 +80,7 @@ def self.from_pointer(pointer_string)
end
end

# initializes a JSI::JSON::Pointer from the given reference_tokens.
# initializes a JSI::Ptr from the given reference_tokens.
#
# @param reference_tokens [Array<Object>]
def initialize(reference_tokens)
Expand All @@ -98,7 +97,7 @@ def initialize(reference_tokens)
#
# @param document [#to_ary, #to_hash] the document against which we will evaluate this pointer
# @return [Object] the content of the document pointed to by this pointer
# @raise [JSI::JSON::Pointer::ReferenceError] the document does not contain the path this pointer references
# @raise [JSI::Ptr::ReferenceError] the document does not contain the path this pointer references
def evaluate(document)
res = reference_tokens.inject(document) do |value, token|
if value.respond_to?(:to_ary)
Expand Down Expand Up @@ -128,12 +127,12 @@ def evaluate(document)
res
end

# @return [String] the pointer string representation of this Pointer
# @return [String] the pointer string representation of this pointer
def pointer
reference_tokens.map { |t| '/' + t.to_s.gsub('~', '~0').gsub('/', '~1') }.join('')
end

# @return [String] the fragment string representation of this Pointer
# @return [String] the fragment string representation of this pointer
def fragment
Addressable::URI.escape(pointer)
end
Expand All @@ -152,13 +151,13 @@ def empty?
# @return [Boolean] whether this is a root pointer, indicated by an empty array of reference_tokens
alias_method :root?, :empty?

# @return [JSI::JSON::Pointer] pointer to the parent of where this pointer points
# @raise [JSI::JSON::Pointer::ReferenceError] if this pointer has no parent (points to the root)
# @return [JSI::Ptr] pointer to the parent of where this pointer points
# @raise [JSI::Ptr::ReferenceError] if this pointer has no parent (points to the root)
def parent
if root?
raise(ReferenceError, "cannot access parent of root pointer: #{pretty_inspect.chomp}")
else
Pointer.new(reference_tokens[0...-1])
Ptr.new(reference_tokens[0...-1])
end
end

Expand All @@ -168,44 +167,44 @@ def contains?(other_ptr)
self.reference_tokens == other_ptr.reference_tokens[0...self.reference_tokens.size]
end

# @return [JSI::JSON::Pointer] returns this pointer relative to the given ancestor_ptr
# @raise [JSI::JSON::Pointer::ReferenceError] if the given ancestor_ptr is not an ancestor of this pointer
# @return [JSI::Ptr] returns this pointer relative to the given ancestor_ptr
# @raise [JSI::Ptr::ReferenceError] if the given ancestor_ptr is not an ancestor of this pointer
def ptr_relative_to(ancestor_ptr)
unless ancestor_ptr.contains?(self)
raise(ReferenceError, "ancestor_ptr #{ancestor_ptr.inspect} is not ancestor of #{inspect}")
end
Pointer.new(reference_tokens[ancestor_ptr.reference_tokens.size..-1])
Ptr.new(reference_tokens[ancestor_ptr.reference_tokens.size..-1])
end

# @param ptr [JSI::JSON::Pointer, #to_ary]
# @return [JSI::JSON::Pointer] a pointer with the reference tokens of this one plus the given ptr's.
# @param ptr [JSI::Ptr, #to_ary]
# @return [JSI::Ptr] a pointer with the reference tokens of this one plus the given ptr's.
def +(ptr)
if ptr.is_a?(JSI::JSON::Pointer)
if ptr.is_a?(JSI::Ptr)
ptr_reference_tokens = ptr.reference_tokens
elsif ptr.respond_to?(:to_ary)
ptr_reference_tokens = ptr
else
raise(TypeError, "ptr must be a JSI::JSON::Pointer or Array of reference_tokens; got: #{ptr.inspect}")
raise(TypeError, "ptr must be a JSI::Ptr or Array of reference_tokens; got: #{ptr.inspect}")
end
Pointer.new(self.reference_tokens + ptr_reference_tokens)
Ptr.new(self.reference_tokens + ptr_reference_tokens)
end

# @param n [Integer]
# @return [JSI::JSON::Pointer] a Pointer consisting of the first n of our reference_tokens
# @return [JSI::Ptr] a pointer consisting of the first n of our reference_tokens
# @raise [ArgumentError] if n is not between 0 and the size of our reference_tokens
def take(n)
unless (0..reference_tokens.size).include?(n)
raise(ArgumentError, "n not in range (0..#{reference_tokens.size}): #{n.inspect}")
end
Pointer.new(reference_tokens.take(n))
Ptr.new(reference_tokens.take(n))
end

# appends the given token to this Pointer's reference tokens and returns the result
# appends the given token to this pointer's reference tokens and returns the result
#
# @param token [Object]
# @return [JSI::JSON::Pointer] pointer to a child node of this pointer with the given token
# @return [JSI::Ptr] pointer to a child node of this pointer with the given token
def [](token)
Pointer.new(reference_tokens + [token])
Ptr.new(reference_tokens + [token])
end

# takes a document and a block. the block is yielded the content of the given document at this
Expand Down Expand Up @@ -267,7 +266,7 @@ def modified_document_copy(document, &block)
modified_document
end

# @return [String] a string representation of this Pointer
# @return [String] a string representation of this pointer
def inspect
"#{self.class.name}[#{reference_tokens.map(&:inspect).join(", ")}]"
end
Expand All @@ -276,9 +275,8 @@ def inspect

# pointers are equal if the reference_tokens are equal
def jsi_fingerprint
{class: JSI::JSON::Pointer, reference_tokens: reference_tokens}
{class: JSI::Ptr, reference_tokens: reference_tokens}
end
include Util::FingerprintHash
end
end
end
12 changes: 6 additions & 6 deletions lib/jsi/schema.rb
Expand Up @@ -259,13 +259,13 @@ def schema_id
parent_auri = Addressable::URI.parse(parent_id)
if parent_auri.fragment
# add onto the fragment
parent_id_path = JSI::JSON::Pointer.from_fragment(parent_auri.fragment).reference_tokens
parent_id_path = Ptr.from_fragment(parent_auri.fragment).reference_tokens
path_from_id_node = parent_id_path + path_from_id_node
parent_auri.fragment = nil
#else: no fragment so parent_id good as is
end

schema_id = parent_auri.merge(fragment: JSI::JSON::Pointer.new(path_from_id_node).fragment).to_s
schema_id = parent_auri.merge(fragment: Ptr.new(path_from_id_node).fragment).to_s

schema_id
else
Expand Down Expand Up @@ -342,10 +342,10 @@ def schema_resource_root?

# returns a subschema of this Schema
#
# @param subptr [JSI::JSON::Pointer, #to_ary] a relative pointer, or array of tokens, pointing to the subschema
# @param subptr [JSI::Ptr, #to_ary] a relative pointer, or array of tokens, pointing to the subschema
# @return [JSI::Schema] the subschema at the location indicated by subptr. self if subptr is empty.
def subschema(subptr)
subschema_map[JSI::JSON::Pointer.ary_ptr(subptr)]
subschema_map[Ptr.ary_ptr(subptr)]
end

private
Expand All @@ -371,10 +371,10 @@ def subschema_map
# returns a schema in the same schema resource as this one (see #schema_resource_root) at the given
# pointer relative to the root of the schema resource.
#
# @param ptr [JSI::JSON::Pointer, #to_ary] a pointer to a schema from our schema resource root
# @param ptr [JSI::Ptr, #to_ary] a pointer to a schema from our schema resource root
# @return [JSI::Schema] the schema pointed to by ptr
def resource_root_subschema(ptr)
resource_root_subschema_map[JSI::JSON::Pointer.ary_ptr(ptr)]
resource_root_subschema_map[Ptr.ary_ptr(ptr)]
end

private
Expand Down

0 comments on commit 61463a9

Please sign in to comment.