lib/jini.rb in jini-0.0.6 vs lib/jini.rb in jini-0.0.7
- old
+ new
@@ -21,21 +21,22 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# 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
-# ```
class Jini
# When path not valid
class InvalidPath < StandardError; end
+ # Makes new object.
+ # By default it creates an empty path and you can ignore the head parameter.
# @param head [String]
def initialize(head = '')
@head = head
end
@@ -46,11 +47,11 @@
end
# Additional node for xpath.
# @param node [String] node
# @return [Jini] object
- def add_path(node)
+ def add_node(node)
Jini.new("#{@head}/#{node}")
end
# Additional attribute for xpath.
# '[@key="value"]'
@@ -71,11 +72,11 @@
# Xpath with all elements.
# Addition an '*' to tail of xpath
# @return [Jini] object
def all
raise InvalidPath, 'You cannot add all tag after attr!' if @head[-1].eql?(']')
- Jini.new(add_path('*').to_s)
+ Jini.new(add_node('*').to_s)
end
# Xpath with all named elements.
# Addition '//node' to xpath
# @param node [String] name of node
@@ -105,12 +106,14 @@
end
# Removes node by name
# @param node [String] name of node for removal
# @return [Jini] without a node
- def remove_path(node)
- Jini.new(@head.gsub("/#{node}", ''))
+ def remove_node(node)
+ Jini.new(
+ purge("/#{node}")
+ )
end
# Removes attr by name
# before:
# '/parent/child[@k="v"]'
@@ -118,46 +121,47 @@
# '/parent/child'
# @param [String] name of attr
# @return [Jini] without an attr
def remove_attr(name)
Jini.new(
- @head
- .gsub(
- /(\[@?#{name}="[^"]+"(\[\]+|\]))/,
- ''
- )
+ purge(/(\[@?#{name}="[^"]+"(\[\]+|\]))/)
)
end
# Adds '|' to head
- # @param [String] first statement
- # @param [String] second statement
+ # @param [String] alpha statement
+ # @param [String] beta statement
# @return [Jini] with Jini '[first|second]' on tail
- def or(first, second)
- Jini.new("#{@head}[#{first}|#{second}]")
+ def or(alpha, beta)
+ action_between('|', alpha, beta)
end
# Less than.
# Addition '[node < value]' to tail
# @param [String] key name
# @param [Object] value
# @return [Jini]
def lt(key, value)
- sat('<', key, value)
+ action_between('<', key, value)
end
# Greater than.
# Addition '[node > value]' to tail
# @param [String] key name
# @param [Object] value
# @return [Jini]
def gt(key, value)
- sat('>', key, value)
+ action_between('>', key, value)
end
private
- # Some action than.
- def sat(action, key, value)
- Jini.new("#{@head}[#{key} #{action} #{value}]")
+ # Some action between two statements.
+ def action_between(action, alpha, beta)
+ Jini.new("#{@head}[#{alpha} #{action} #{beta}]")
+ end
+
+ # @param [Regexp | String] token to be purged from the head
+ def purge(token)
+ @head.gsub(token, '')
end
end