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.

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

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



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’

Parameters:

  • key (String)

    name of attr

  • value (String)

    value of attr

Returns:

  • (Jini)

    with additional attr on tail



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.

Parameters:

  • key (String)

    the key

Returns:

  • (Jini)

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

Parameters:

  • node (String)

    the node

Returns:

  • (Jini)

    object with additional node

Since:

  • 0.0.1



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

Parameters:

  • node (String)

    the node

Returns:

  • (Jini)

    with additional nodes



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.

Examples:

before: ‘../child’, after: ‘../child/property()’

Parameters:

  • property (String)

    to add

Returns:

  • (Jini)

    with property on tail

Since:

  • 0.0.1



104
105
106
# File 'lib/jini.rb', line 104

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:



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

Parameters:

  • position (Integer)

    number

Returns:

  • (Jini)

    with selected index

Raises:



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

Parameters:

  • alpha (String)

    the alpha statement

  • beta (Object)

    the beta statement

Returns:

  • (Jini)

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

Parameters:

  • alpha (String)

    the alpha statement

  • beta (Object)

    the beta statement

Returns:

  • (Jini)

    with condition on tail



210
211
212
# File 'lib/jini.rb', line 210

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

#nodesObject

All nodes in xpath as array.

Returns:

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

Parameters:

  • alpha (String)

    the alpha statement

  • beta (String)

    the beta statement

Returns:

  • (Jini)

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

Parameters:

  • name (String)

    of attr

Returns:

  • (Jini)

    without an attr



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.

Parameters:

  • node (String)

    the node for removal

Returns:

  • (Jini)

    without a node

Since:

  • 0.0.7



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.

Parameters:

  • property (String)

    to remove

Returns:

  • (Jini)

    without property on tail

Since:

  • 0.1.3



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

Parameters:

  • name (String)

    of attr

  • value (String)

    upd value

Returns:

  • (Jini)

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

Parameters:

  • origin (String)

    origin node

  • new (String)

    new one

Since:

  • 0.1.0



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

#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



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_sString

Convert it to a string.

Returns:

  • (String)

    xpath as string

Raises:

Since:

  • 0.0.1



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