Class: Yast::MapClass

Inherits:
Module
  • Object
show all
Defined in:
library/types/src/modules/Map.rb

Instance Method Summary collapse

Instance Method Details

#CheckKeys(map, keys) ⇒ Object

Check if a map contains all needed keys

Parameters:

  • map (Hash)

    to be checked

  • keys (Array)

    needed keys

Returns:

  • true if map kontains all keys


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'library/types/src/modules/Map.rb', line 91

def CheckKeys(map, keys)
  map = deep_copy(map)
  keys = deep_copy(keys)
  return false if map.nil? || keys.nil?

  ret = true
  Builtins.foreach(keys) do |k|
    if k.nil? || !Builtins.haskey(map, k)
      Builtins.y2error("Missing key: %1", k)
      ret = false
    end
  end

  ret
end

#FromString(string) ⇒ Object

Convert string "var=val ..." to map $[val:var, ...]

Parameters:

  • string (String)

    to be converted

Returns:

  • converted string


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'library/types/src/modules/Map.rb', line 124

def FromString(string)
  return {} if string.nil?

  ret = {}
  Builtins.foreach(Builtins.splitstring(string, " ")) do |vals|
    val = Builtins.splitstring(vals, "=")
    if Ops.less_than(Builtins.size(val), 1) ||
        Ops.get_string(val, 0, "") == ""
      next
    end

    key = Ops.get_string(val, 0, "")
    if Ops.greater_than(Builtins.size(val), 1)
      Ops.set(ret, key, Ops.get_string(val, 1, ""))
    else
      Ops.set(ret, key, "")
    end
  end
  deep_copy(ret)
end

#Keys(map) ⇒ Object

Return all keys from the map

Parameters:

  • map (Hash)

Returns:

  • a list of all keys from the map


42
43
44
45
46
47
# File 'library/types/src/modules/Map.rb', line 42

def Keys(map)
  map = deep_copy(map)
  return [] if map.nil? || map == {}

  Builtins.maplist(map) { |var, _val| var }
end

#KeysToLower(map) ⇒ Hash

Switch map keys to lower case

Parameters:

  • map (Hash{String => Object})

Returns:

  • (Hash)

    with keys converted to lower case


62
63
64
65
66
67
68
69
70
71
# File 'library/types/src/modules/Map.rb', line 62

def KeysToLower(map)
  map = deep_copy(map)
  newk = nil
  return {} if map.nil?

  Builtins.mapmap(map) do |k, v|
    newk = Builtins.tolower(k)
    { newk => v }
  end
end

#KeysToUpper(map) ⇒ Hash

Switch map keys to upper case

Parameters:

  • m (Hash{String => Object})

    input map

Returns:

  • (Hash)

    with keys converted to lower case


76
77
78
79
80
81
82
83
84
85
# File 'library/types/src/modules/Map.rb', line 76

def KeysToUpper(map)
  map = deep_copy(map)
  newk = nil
  return {} if map.nil?

  Builtins.mapmap(map) do |k, v|
    newk = Builtins.toupper(k)
    { newk => v }
  end
end

#mainObject


33
34
35
36
37
# File 'library/types/src/modules/Map.rb', line 33

def main
  textdomain "base"

  Yast.import "String"
end

#ToString(map) ⇒ Object

Convert options map $[var:val, ...] to string "var=val ..."

Parameters:

  • map (Hash)

    to be converted

Returns:

  • converted map


110
111
112
113
114
115
116
117
118
119
# File 'library/types/src/modules/Map.rb', line 110

def ToString(map)
  map = deep_copy(map)
  return "" if map.nil?

  ret = ""
  Builtins.foreach(map) do |var, val|
    ret = Ops.add(ret, Builtins.sformat(" %1=%2", var, val))
  end
  String.CutBlanks(ret)
end

#Values(map) ⇒ Object

Return all values from the map

Parameters:

  • map (Hash)

Returns:

  • a list of all values from the map


52
53
54
55
56
57
# File 'library/types/src/modules/Map.rb', line 52

def Values(map)
  map = deep_copy(map)
  return [] if map.nil? || map == {}

  Builtins.maplist(map) { |_var, val| val }
end