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



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

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



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

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

Instance Method Details

#+(other) ⇒ Object

concats path



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

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



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

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



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

def clone
  self
end

#empty?Boolean

Detect if there is no elements

Returns:

  • (Boolean)


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

def empty?
  components.empty?
end

#sizeObject

gets number of elements



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

def size
  components.size
end

#to_sObject



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

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