Class: Yast::Path

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
src/ruby/yast/path.rb,
src/ruby/yast/yast.rb

Overview

Represents paths like it is in ycp. It is path elements separated by dot. Elements can be simple or complex. Simple can contain only ascii characters [a-zA-Z0-9]. Complex elements are enclosed by " and can contain all characters. Immutable class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Path

any element starts or ends with dash like ".-etc", ".etc-" or ".e.t-.c"

Parameters:

  • value (String)

    string representation of path

Raises:

  • RuntimeError if invalid path is passed. Invalid path is one where



12
13
14
15
16
17
18
19
# File 'src/ruby/yast/path.rb', line 12

def initialize(value)
  if !value.is_a?(::String)
    raise ArgumentError, "Yast::Path constructor has to get ::String as " \
      "argument instead of '#{value.inspect}'"
  end
  @components = []
  load_components value
end

Class Method Details

.from_string(string) ⇒ Object

Creates path from generic string



22
23
24
# File 'src/ruby/yast/path.rb', line 22

def self.from_string(string)
  new ".\"#{string}\""
end

Instance Method Details

#+(other) ⇒ Object

concats path



31
32
33
34
35
36
# File 'src/ruby/yast/path.rb', line 31

def +(other)
  other = self.class.from_string(other) unless other.is_a? Yast::Path
  return other.clone if components.empty?
  return clone if other.empty?
  Path.new(to_s + other.to_s)
end

#<=>(other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'src/ruby/yast/path.rb', line 52

def <=>(other)
  return nil unless other.is_a? self.class
  0.upto(size - 1) do |i|
    return 1 unless other.send(:components)[i]
    # we strip enclosing quotes for complex expression
    our_component = components[i].sub(/\A"(.*)"\Z/, "\\1")
    other_component = other.send(:components)[i].sub(/\A"(.*)"\Z/, "\\1")
    res = our_component <=> other_component
    return res if res != 0
  end
  size <=> other.size
end

#cloneObject



26
27
28
# File 'src/ruby/yast/path.rb', line 26

def clone
  self
end

#empty?Boolean

Detect if there is no elements

Returns:

  • (Boolean)


48
49
50
# File 'src/ruby/yast/path.rb', line 48

def empty?
  components.empty?
end

#sizeObject

gets number of elements



43
44
45
# File 'src/ruby/yast/path.rb', line 43

def size
  components.size
end

#to_sObject



38
39
40
# File 'src/ruby/yast/path.rb', line 38

def to_s
  "." + components.join(".")
end