Class: Jini

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(head = '') ⇒ Jini

Makes new object. By default it creates an empty path and you can ignore the head parameter.

Parameters:

  • head (String) (defaults to: '')

Since:

  • 0.0.1



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.

Parameters:

  • xpath (String)

Returns:

Raises:



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’

Parameters:

  • key (String)

    name of attr

  • value (String)

    value of attr

Returns:

  • (Jini)

    with additional attr on tail



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.

Parameters:

  • key (String)

    the key

Returns:

  • (Jini)

    with additional value on 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.

Parameters:

  • node (String)

    the node

Returns:

  • (Jini)

    object with additional node

Since:

  • 0.0.1



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

Parameters:

  • node (String)

    the node

Returns:

  • (Jini)

    with additional nodes



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()

Parameters:

  • property (String)

    to add

Returns:

  • (Jini)

    with property on tail

Since:

  • 0.0.1



169
170
171
# File 'lib/jini.rb', line 169

def add_property(property)
  Jini.new(add_node("#{property}()").to_s)
end

#allJini

Xpath with all elements. Addition an ‘*’ to tail of xpath

Returns:

Raises:



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

Parameters:

  • position (Integer)

    number

Returns:

  • (Jini)

    with selected index

Raises:



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

#countJini

Just wrap current XPATH into count() function

Returns:

  • (Jini)

    wrapped



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

Parameters:

  • alpha (String)

    the alpha statement

  • beta (Object)

    the beta statement

Returns:

  • (Jini)

    with condition on 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

Parameters:

  • alpha (String)

    the alpha statement

  • beta (Object)

    the beta statement

Returns:

  • (Jini)

    with condition on tail



281
282
283
# File 'lib/jini.rb', line 281

def lt(alpha, beta)
  action_between('<', alpha, beta)
end

#nodesObject

All nodes in xpath as array.

Returns:

  • nodes 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.

Parameters:

  • alpha (String)

    the alpha statement

  • beta (String)

    the beta statement

Returns:

  • (Jini)

    with condition on 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’

Parameters:

  • name (String)

    of attr

Returns:

  • (Jini)

    without an attr



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.

Parameters:

  • node (String)

    the node for removal

Returns:

  • (Jini)

    without a node

Since:

  • 0.0.7



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.

Parameters:

  • property (String)

    to remove

Returns:

  • (Jini)

    without property on tail

Since:

  • 0.1.3



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”]’

Parameters:

  • name (String)

    of attr

  • value (String)

    upd value

Returns:

  • (Jini)

    with replaced attr 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.

Parameters:

  • origin (String)

    origin node

  • new (String)

    new one

Since:

  • 0.1.0



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

#selectionJini

Replace all ‘/’ to ‘::’.

If path doesn’t contain invalid symbols for selection

Returns:

  • (Jini)

    with selection

Raises:

  • (InvalidPath)

    when path can’t present with select



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_sString

Convert it to a string.

Returns:

  • (String)

    xpath as string

Raises:

Since:

  • 0.0.1



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