Skip to content

Commit

Permalink
Base#/
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Feb 11, 2023
1 parent 1c9cc7c commit af89c0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/jsi/base.rb
Expand Up @@ -318,6 +318,24 @@ def jsi_descendent_node(ptr)
descendent
end

# A shorthand alias for {#jsi_descendent_node}.
#
# Note that, though more convenient to type, using an operator whose meaning may not be intuitive
# to a reader could impair readability of code.
#
# examples:
#
# my_jsi / ['foo', 'bar']
# my_jsi / %w(foo bar)
# my_schema / JSI::Ptr['additionalProperties']
# my_schema / %w(properties foo items additionalProperties)
#
# @param (see #jsi_descendent_node)
# @return (see #jsi_descendent_node)
def /(ptr)
jsi_descendent_node(ptr)
end

# yields each token (array index or hash key) identifying a child node.
# yields nothing if this node is not complex or has no children.
#
Expand Down
13 changes: 13 additions & 0 deletions test/base_test.rb
Expand Up @@ -229,6 +229,19 @@ module Contact
end
end
end

describe '#jsi_descendent_node, #/' do
let(:instance) { {'foo' => {'a' => {'b' => ['c']}}} }
it 'returns the descendent node' do
tokens = ['foo', 'a', 'b', 0]
ptr = JSI::Ptr.new(tokens)
assert_equal(ptr, subject.jsi_descendent_node(tokens).jsi_ptr)
assert_equal(ptr, subject.jsi_descendent_node(ptr).jsi_ptr)
assert_equal(ptr, (subject / tokens).jsi_ptr)
assert_equal(ptr, (subject / ptr).jsi_ptr)
end
end

describe '#each, Enumerable methods' do
let(:instance) { 'a string' }
it "raises NoMethodError calling each or Enumerable methods" do
Expand Down

0 comments on commit af89c0a

Please sign in to comment.