lib/jini.rb in jini-0.1.4 vs lib/jini.rb in jini-0.1.5

- old
+ new

@@ -26,11 +26,12 @@ # Author:: Ivan Ivanchuk (clicker.heroes.acg@gmail.com) # Copyright:: Copyright (c) 2022 Ivan Ivanchuck # License:: MIT # # It's a simple XPATH builder. -# <p></p> +# Class is thread safe. +# # require 'jini' # xpath = Jini.new('parent') # .add_node('child') # .add_attr('toy', 'plane') # .to_s // parent/child[@toy="plane"] @@ -63,19 +64,19 @@ # @since 0.0.1 def add_node(node) Jini.new("#{@head}/#{node}") end - # Removes node by name + # Removes node by name. # @param node [String] the node for removal # @return [Jini] without a node # @since 0.0.7 def remove_node(node) Jini.new(purge_head("/#{node}")) end - # This method replaces *all* origins to new + # This method replaces *all* origins to new. # @param origin [String] origin node # @param new [String] new one # @since 0.1.0 def replace_node(origin, new) Jini.new( @@ -84,10 +85,19 @@ .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)) + checked.each { |node| checked.delete node if node.eql?('//') || node.eql?('/') }.to_a + end + # Addition property in tail. # @example before: '../child', after: '../child/property()' # @param property [String] to add # @return [Jini] with property on tail # @since 0.0.1 @@ -138,11 +148,11 @@ # After: # '[@id="new value"]' # @param name [String] of attr # @param value [String] upd value # @return [Jini] with replaced attr value - def new_attr_value(name, value) + def replace_attr_value(name, value) n_rxp = /(\[@?#{name}="[^"]+"(\[\]+|\]))/ attr = @head[n_rxp] attr[/"(.*?)"/] = "\"#{value}\"" Jini.new(@head.gsub(n_rxp, attr)) unless attr.nil? end @@ -211,21 +221,26 @@ end private # @param node [String] node for check + def empty_check(node) + raise InvalidPath, 'Invalid path, empty node' if node.length.eql? 0 + end + + # @param node [String] node for check def space_check(node) raise InvalidPath, "Nodes can't contain spaces: #{node} – contain space." if valid? node end - # regex: '[' or ']' or '@' or '//' + # Regex: '[' or ']' or '@' or '//'. # @param node [String] # @return [Boolean] matching regex def bad_symbols?(node) !!node.match(%r{[|]|@|//}) unless node.nil? end - # regex: '[' or ']' or '@' or '=' or '<' or '>' + # Regex: '[' or ']' or '@' or '=' or '<' or '>'. # @param node [String] node for check # @return [Boolean] matching regex def valid?(node) !node.match(/[|]|@|=|>|</) && node.include?(' ') end