lib/jini.rb in jini-0.0.8 vs lib/jini.rb in jini-0.0.9
- old
+ new
@@ -24,13 +24,13 @@
# It's a simple XPATH builder.
#
# require 'jini'
# xpath = Jini.new('parent')
-# .add_path(node: 'child')
-# .add_path(node: 'toy')
-# .to_s // body/child/toy
+# .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.
@@ -43,26 +43,43 @@
# Convert it to a string.
# @return [String] xpath as string
# @raise [InvalidPath] if contain spaces in simple nodes
def to_s
copy = @head.split(%r{//|/})
- copy.each do |node|
- if !node.match(/[|]|@|=|>|</) && node.include?(' ')
- raise InvalidPath, "Nodes can't contain spaces: #{node} – contain space."
- end
- end
+ copy.each(&method(:space_check))
@head.to_s
end
# Additional node for xpath.
# @param node [String] node
# @return [Jini] object
def add_node(node)
Jini.new("#{@head}/#{node}")
end
- # Addition property in tail
+ # Removes node by name
+ # @param node [String] name of node for removal
+ # @return [Jini] without a node
+ def remove_node(node)
+ Jini.new(
+ purge_head("/#{node}")
+ )
+ end
+
+ # This method replaces *all* origins to new
+ # @param [String] origin node
+ # @param [String] new node
+ def replace_node(origin, new)
+ Jini.new(
+ @head
+ .split('/')
+ .map! { |node| node.eql?(origin) ? new : node }
+ .join('/')
+ )
+ end
+
+ # Addition property in tail.
# Before: '../child'
# After: '../child/property()'
# @param property [String]
# @return [Jini] object
def add_property(property)
@@ -76,11 +93,11 @@
# @return [Jini] object
def add_attr(key, value)
Jini.new("#{@head}[@#{key}=\"#{value}\"]")
end
- # Adds '@value' to tail
+ # Adds '@value' to tail.
# @param value [String] with value attr
# @return [Jini] object
def add_attrs(value)
Jini.new("#{@head}@#{value}")
end
@@ -110,40 +127,29 @@
def at(position)
raise InvalidPath, 'Cant use at after selection' if @head.include? '::'
Jini.new("#{@head}[#{position}]")
end
- # Replace all '/' to '::' symbols
+ # Replace all '/' to '::' symbols.
# if path doesn't contain invalid symbols for selection
# @return [Jini] selection
# @raise [InvalidPath] when path can't present with select
def selection
- if @head.include?('[') || @head.include?(']') || @head.include?('@') || @head.include?('//')
- raise InvalidPath, 'Cannot select, path contains bad symbols'
- end
+ raise InvalidPath, 'Cannot select, path contains bad symbols' if bad_symbols? @head
Jini.new(@head.gsub('/', '::').to_s)
end
- # Removes node by name
- # @param node [String] name of node for removal
- # @return [Jini] without a node
- def remove_node(node)
- Jini.new(
- purge("/#{node}")
- )
- end
-
- # Removes attr by name
+ # Removes attr by name.
# before:
- # '/parent/child[@k="v"]'
+ # '/parent/child [@k="v"]'
# after:
# '/parent/child'
# @param [String] name of attr
# @return [Jini] without an attr
def remove_attr(name)
Jini.new(
- purge(/(\[@?#{name}="[^"]+"(\[\]+|\]))/)
+ purge_head(/(\[@?#{name}="[^"]+"(\[\]+|\]))/)
)
end
# Adds '[alpha | beta]' in tail.
# @param [String] alpha statement
@@ -171,15 +177,35 @@
action_between('>', key, value)
end
private
+ # @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 '//'
+ # @param node [String]
+ # @return node [Boolean] matching regex
+ def bad_symbols?(node)
+ node.match %r{[|]|@|//}
+ end
+
+ # regex: '[' or ']' or '@' or '=' or '<' or '>'
+ # @param node [String] node for check
+ # @return node [Boolean] matching regex
+ def valid?(node)
+ !node.match(/[|]|@|=|>|</) && node.include?(' ')
+ end
+
# Some action between two statements.
def action_between(action, alpha, beta)
Jini.new("#{@head}[#{alpha} #{action} #{beta}]")
end
+ # Purging head from token.
# @param [Regexp | String] token to be purged from the head
- def purge(token)
+ def purge_head(token)
@head.gsub(token, '')
end
end