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. Example:
require 'jini'
xpath = Jini.new('parent')
.add_node('child')
.add_attr('toy', 'plane')
.to_s
> ‘parent/child
Defined Under Namespace
Classes: InvalidPath, UnsupportedOperation
Class Method Summary collapse
-
.from(xpath) ⇒ Jini
From.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#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.
-
#count ⇒ Jini
Just wrap current XPATH into count() function.
-
#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.
48 49 50 |
# File 'lib/jini.rb', line 48 def initialize(head = '') @head = head end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object (private)
331 332 333 |
# File 'lib/jini.rb', line 331 def method_missing(method_name, *_args) raise UnsupportedOperation, "The method #{method_name} was not found!" end |
Class Method Details
.from(xpath) ⇒ Jini
From. Creates new Jini object from XPATH.
69 70 71 72 |
# File 'lib/jini.rb', line 69 def from(xpath) raise InvalidPath, 'XPATH isn\'t valid' unless xpath_match?(xpath) Jini.new(xpath) end |
Instance Method Details
#==(other) ⇒ Object
120 121 122 |
# File 'lib/jini.rb', line 120 def ==(other) self.class == other.class && to_s == other.to_s end |
#add_attr(key, value) ⇒ Jini
Additional attribute for xpath. @example’‘
186 187 188 |
# File 'lib/jini.rb', line 186 def add_attr(key, value) Jini.new("#{@head}[@#{key}=\"#{value}\"]") end |
#add_attrs(key) ⇒ Jini
Adds ‘@key’ to tail.
193 194 195 |
# File 'lib/jini.rb', line 193 def add_attrs(key) Jini.new("#{@head}@#{key}") end |
#add_node(node) ⇒ Jini
Additional node for xpath.
128 129 130 |
# File 'lib/jini.rb', line 128 def add_node(node) Jini.new("#{@head}/#{node}") end |
#add_nodes(node) ⇒ Jini
Xpath with all named elements. Addition ‘//node’ to xpath
244 245 246 |
# File 'lib/jini.rb', line 244 def add_nodes(node) Jini.new("#{@head}//#{node}") end |
#add_property(property) ⇒ Jini
Addition property in tail. Example:
>> Jini.new('node/').property('prop').to_s
=> node/property()
169 170 171 |
# File 'lib/jini.rb', line 169 def add_property(property) Jini.new(add_node("#{property}()").to_s) end |
#all ⇒ Jini
Xpath with all elements. Addition an ‘*’ to tail of xpath
235 236 237 238 |
# File 'lib/jini.rb', line 235 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
253 254 255 256 |
# File 'lib/jini.rb', line 253 def at(position) raise InvalidPath, 'Cant use at after selection' if @head.include? '::' Jini.new("#{@head}[#{position}]") end |
#count ⇒ Jini
Just wrap current XPATH into count() function
199 200 201 |
# File 'lib/jini.rb', line 199 def count Jini.new("count(#{@head})") end |
#gt(alpha, beta) ⇒ Jini
Greater than. Addition ‘[alpha > beta]’ to tail
290 291 292 |
# File 'lib/jini.rb', line 290 def gt(alpha, beta) action_between('>', alpha, beta) end |
#lt(alpha, beta) ⇒ Jini
Less than. Addition ‘[alpha < beta]’ to tail
281 282 283 |
# File 'lib/jini.rb', line 281 def lt(alpha, beta) action_between('<', alpha, beta) end |
#nodes ⇒ Object
All nodes in xpath as array.
155 156 157 158 159 160 |
# File 'lib/jini.rb', line 155 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.
272 273 274 |
# File 'lib/jini.rb', line 272 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’
210 211 212 213 214 |
# File 'lib/jini.rb', line 210 def remove_attr(name) Jini.new( purge_head(/(\[@?#{name}="[^"]+"(\[\]+|\]))/) ) end |
#remove_node(node) ⇒ Jini
Removes node by name.
136 137 138 |
# File 'lib/jini.rb', line 136 def remove_node(node) Jini.new(purge_head("/#{node}")) end |
#remove_property(property) ⇒ Jini
Removes property.
177 178 179 |
# File 'lib/jini.rb', line 177 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”]’
224 225 226 227 228 229 |
# File 'lib/jini.rb', line 224 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.
144 145 146 147 148 149 150 151 |
# File 'lib/jini.rb', line 144 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
263 264 265 266 |
# File 'lib/jini.rb', line 263 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.
56 57 58 59 60 |
# File 'lib/jini.rb', line 56 def to_s copy = @head.split(%r{//|/}) copy.each(&method(:space_check)) @head.to_s end |