Class: Jini
- Inherits:
-
Object
- Object
- Jini
- Defined in:
- lib/jini.rb
Overview
The jini.
- Author
-
Ivan Ivanchuk ([email protected])
- Copyright
-
Copyright © 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"]
Defined Under Namespace
Classes: InvalidPath
Instance Method Summary collapse
-
#add_attr(key, value) ⇒ Jini
Additional attribute for xpath.
-
#add_attrs(key) ⇒ Jini
Adds ‘@key’ to tail.
-
#add_node(node) ⇒ Jini
Additional node for xpath.
-
#add_nodes(node) ⇒ Jini
Xpath with all named elements.
-
#add_property(property) ⇒ Jini
Addition property in tail.
-
#all ⇒ Jini
Xpath with all elements.
-
#at(position) ⇒ Jini
Access by index.
-
#gt(alpha, beta) ⇒ Jini
Greater than.
-
#initialize(head = '') ⇒ Jini
constructor
Makes new object.
-
#lt(alpha, beta) ⇒ Jini
Less than.
-
#nodes ⇒ Object
All nodes in xpath as array.
-
#or(alpha, beta) ⇒ Jini
Adds ‘[alpha | beta]’ in tail.
-
#remove_attr(name) ⇒ Jini
Removes attr by name.
-
#remove_node(node) ⇒ Jini
Removes node by name.
-
#remove_property(property) ⇒ Jini
Removes property.
-
#replace_attr_value(name, value) ⇒ Jini
Replaces all attr values by name.
-
#replace_node(origin, new) ⇒ Object
This method replaces all origins to new.
-
#selection ⇒ Jini
Replace all ‘/’ to ‘::’.
-
#to_s ⇒ String
Convert it to a string.
Constructor Details
#initialize(head = '') ⇒ Jini
Makes new object. By default it creates an empty path and you can ignore the head parameter.
47 48 49 |
# File 'lib/jini.rb', line 47 def initialize(head = '') @head = head end |
Instance Method Details
#add_attr(key, value) ⇒ Jini
Additional attribute for xpath. @example’‘
121 122 123 |
# File 'lib/jini.rb', line 121 def add_attr(key, value) Jini.new("#{@head}[@#{key}=\"#{value}\"]") end |
#add_attrs(key) ⇒ Jini
Adds ‘@key’ to tail.
128 129 130 |
# File 'lib/jini.rb', line 128 def add_attrs(key) Jini.new("#{@head}@#{key}") end |
#add_node(node) ⇒ Jini
Additional node for xpath.
65 66 67 |
# File 'lib/jini.rb', line 65 def add_node(node) Jini.new("#{@head}/#{node}") end |
#add_nodes(node) ⇒ Jini
Xpath with all named elements. Addition ‘//node’ to xpath
173 174 175 |
# File 'lib/jini.rb', line 173 def add_nodes(node) Jini.new("#{@head}//#{node}") end |
#add_property(property) ⇒ Jini
Addition property in tail.
104 105 106 |
# File 'lib/jini.rb', line 104 def add_property(property) Jini.new(add_node("#{property}()").to_s) end |
#all ⇒ Jini
Xpath with all elements. Addition an ‘*’ to tail of xpath
164 165 166 167 |
# File 'lib/jini.rb', line 164 def all raise InvalidPath, 'You cannot add all tag after attr!' if @head[-1].eql?(']') Jini.new(add_node('*').to_s) end |
#at(position) ⇒ Jini
Access by index. Addition ‘[index]’ to xpath
182 183 184 185 |
# File 'lib/jini.rb', line 182 def at(position) raise InvalidPath, 'Cant use at after selection' if @head.include? '::' Jini.new("#{@head}[#{position}]") end |
#gt(alpha, beta) ⇒ Jini
Greater than. Addition ‘[alpha > beta]’ to tail
219 220 221 |
# File 'lib/jini.rb', line 219 def gt(alpha, beta) action_between('>', alpha, beta) end |
#lt(alpha, beta) ⇒ Jini
Less than. Addition ‘[alpha < beta]’ to tail
210 211 212 |
# File 'lib/jini.rb', line 210 def lt(alpha, beta) action_between('<', alpha, beta) end |
#nodes ⇒ Object
All nodes in xpath as array.
92 93 94 95 96 97 |
# File 'lib/jini.rb', line 92 def nodes checked = @head .split(%r{(//|/)}) .each(&method(:empty_check)) checked.each { |node| checked.delete node if node.eql?('//') || node.eql?('/') }.to_a end |
#or(alpha, beta) ⇒ Jini
Adds ‘[alpha | beta]’ in tail.
201 202 203 |
# File 'lib/jini.rb', line 201 def or(alpha, beta) action_between('|', alpha, beta) end |
#remove_attr(name) ⇒ Jini
Removes attr by name. before: ‘/parent/child [@k=“v”]’ after: ‘/parent/child’
139 140 141 142 143 |
# File 'lib/jini.rb', line 139 def remove_attr(name) Jini.new( purge_head(/(\[@?#{name}="[^"]+"(\[\]+|\]))/) ) end |
#remove_node(node) ⇒ Jini
Removes node by name.
73 74 75 |
# File 'lib/jini.rb', line 73 def remove_node(node) Jini.new(purge_head("/#{node}")) end |
#remove_property(property) ⇒ Jini
Removes property.
112 113 114 |
# File 'lib/jini.rb', line 112 def remove_property(property) Jini.new(@head.gsub("#{property}()", '')) end |
#replace_attr_value(name, value) ⇒ Jini
Replaces all attr values by name. Before: ‘[@id=“some value”]’ After: ‘[@id=“new value”]’
153 154 155 156 157 158 |
# File 'lib/jini.rb', line 153 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 |
#replace_node(origin, new) ⇒ Object
This method replaces all origins to new.
81 82 83 84 85 86 87 88 |
# File 'lib/jini.rb', line 81 def replace_node(origin, new) Jini.new( @head .split('/') .map! { |node| node.eql?(origin) ? new : node } .join('/') ) end |
#selection ⇒ Jini
Replace all ‘/’ to ‘::’.
If path doesn’t contain invalid symbols for selection
192 193 194 195 |
# File 'lib/jini.rb', line 192 def selection raise InvalidPath, 'Cannot select, path contains bad symbols' if bad_symbols? @head Jini.new(@head.gsub('/', '::').to_s) end |
#to_s ⇒ String
Convert it to a string.
55 56 57 58 59 |
# File 'lib/jini.rb', line 55 def to_s copy = @head.split(%r{//|/}) copy.each(&method(:space_check)) @head.to_s end |