Skip to content

Commit

Permalink
Ptr#ancestor_of? replaces #contains? which is confusingly different t…
Browse files Browse the repository at this point in the history
…han Array#contains?
  • Loading branch information
notEthan committed Apr 17, 2024
1 parent 3e24b6c commit 35a2288
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/jsi/ptr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,25 @@ def parent
tokens.size == 1 ? EMPTY : Ptr.new(tokens[0...-1].freeze)
end

# whether this pointer contains the other_ptr - that is, whether this pointer is an ancestor
# of `other_ptr`, a descendent pointer. `contains?` is inclusive; a pointer does contain itself.
# whether this pointer is an ancestor of `other_ptr`, a descendent pointer.
# `ancestor_of?` is inclusive; a pointer is an ancestor of itself.
#
# @return [Boolean]
def contains?(other_ptr)
def ancestor_of?(other_ptr)
tokens == other_ptr.tokens[0...tokens.size]
end

# @deprecated
def contains?(other_ptr)
ancestor_of?(other_ptr)
end

# part of this pointer relative to the given ancestor_ptr
# @return [JSI::Ptr]
# @raise [JSI::Ptr::Error] if the given ancestor_ptr is not an ancestor of this pointer
def relative_to(ancestor_ptr)
return self if ancestor_ptr.empty?
unless ancestor_ptr.contains?(self)
unless ancestor_ptr.ancestor_of?(self)
raise(Error, "ancestor_ptr #{ancestor_ptr.inspect} is not ancestor of #{inspect}")
end
ancestor_ptr.tokens.size == tokens.size ? EMPTY : Ptr.new(tokens[ancestor_ptr.tokens.size..-1].freeze)
Expand Down
2 changes: 1 addition & 1 deletion lib/jsi/schema/schema_ancestor_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def jsi_schema_resource_ancestors=(jsi_schema_resource_ancestors)
#chkbug end
#chkbug if anc.jsi_ptr == jsi_ptr
#chkbug raise(Bug, "ancestor is self")
#chkbug elsif !anc.jsi_ptr.contains?(jsi_ptr)
#chkbug elsif !anc.jsi_ptr.ancestor_of?(jsi_ptr)
#chkbug raise(Bug, "ancestor does not contain self")
#chkbug end
#chkbug last_anc_ptr = anc.jsi_ptr
Expand Down
2 changes: 1 addition & 1 deletion test/metaschema_node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def assert_metaschema_behaves
metaschema.instance_validate(metaschema),
]
metaschema.jsi_each_descendent_node do |node|
if node.jsi_ptr.contains?(JSI::Ptr['title'])
if node.jsi_ptr.ancestor_of?(JSI::Ptr['title'])
results << node.jsi_validate
else
assert(node.jsi_valid?)
Expand Down

0 comments on commit 35a2288

Please sign in to comment.