lib/jini.rb in jini-0.1.5 vs lib/jini.rb in jini-1.1.5
- old
+ new
@@ -27,17 +27,17 @@
# Copyright:: Copyright (c) 2022 Ivan Ivanchuck
# License:: MIT
#
# It's a simple XPATH builder.
# Class is thread safe.
-#
-# require 'jini'
-# xpath = Jini.new('parent')
-# .add_node('child')
-# .add_attr('toy', 'plane')
-# .to_s // parent/child[@toy="plane"]
-#
+# Example:
+# require 'jini'
+# xpath = Jini.new('parent')
+# .add_node('child')
+# .add_attr('toy', 'plane')
+# .to_s
+# => 'parent/child[@toy="plane"]
class Jini
# When path not valid
class InvalidPath < StandardError; end
# Makes new object.
@@ -79,27 +79,29 @@
# @param new [String] new one
# @since 0.1.0
def replace_node(origin, new)
Jini.new(
@head
- .split('/')
- .map! { |node| node.eql?(origin) ? new : node }
- .join('/')
+ .split('/')
+ .map! { |node| node.eql?(origin) ? new : node }
+ .join('/')
)
end
# All nodes in xpath as array.
# @return nodes as [Array]
def nodes
checked = @head
- .split(%r{(//|/)})
- .each(&method(:empty_check))
+ .split(%r{(//|/)})
+ .each(&method(:empty_check))
checked.each { |node| checked.delete node if node.eql?('//') || node.eql?('/') }.to_a
end
# Addition property in tail.
- # @example before: '../child', after: '../child/property()'
+ # Example:
+ # >> Jini.new('node/').property('prop').to_s
+ # => node/property()
# @param property [String] to add
# @return [Jini] with property on tail
# @since 0.0.1
def add_property(property)
Jini.new(add_node("#{property}()").to_s)
@@ -125,9 +127,15 @@
# Adds '@key' to tail.
# @param key [String] the key
# @return [Jini] with additional value on tail
def add_attrs(key)
Jini.new("#{@head}@#{key}")
+ end
+
+ # Just wrap current XPATH into count() function
+ # @return [Jini] wrapped
+ def count
+ Jini.new("count(#{@head})")
end
# Removes attr by name.
# before:
# '/parent/child [@k="v"]'