Class: Yast::TypeRepositoryClass

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

Instance Method Summary collapse

Instance Method Details

#enum_validator(values, value) ⇒ Object

Generic enumerated type validator.

@param [Array] values a list of possible values @param [String] value the value to be matched @return true if successful


130
131
132
133
# File 'library/types/src/modules/TypeRepository.rb', line 130

def enum_validator(values, value)
  values = deep_copy(values)
  Builtins.contains(values, value)
end

#is_a(value, type) ⇒ Object

Validate, that the given value is of given type.

Parameters:

  • value (Object)

    value to be validated

  • type (String)

    type against which to validate

Returns:

  • true, if the value can be considered to be of a given type


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

def is_a(value, type)
  value = deep_copy(value)
  validator = Ops.get(@types, type)
  return validator.call(value) if validator

  Builtins.y2error("Request to validate unknown type %1", type)
  false
end

#is_string(value) ⇒ Object

Check, if s is a string.

Parameters:

  • value (Object)

    a value to be validated

Returns:

  • true if s is string


63
64
65
# File 'library/types/src/modules/TypeRepository.rb', line 63

def is_string(value)
  Ops.is_string?(value)
end

#IsEmpty(value) ⇒ Object

Checks if given argument is empty.

For other types than string, list, map returns true when value is nil. For list and map checks if value is nil or doesn't contain any item ( [] resp $[] ). For string checks if value is nil or equal to string without any chars ("").


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'library/types/src/modules/TypeRepository.rb', line 97

def IsEmpty(value)
  value = deep_copy(value)
  return true if value.nil?

  ret = false

  ret = Builtins.isempty(Convert.to_string(value)) if Ops.is_string?(value)

  ret = Builtins.isempty(Convert.to_list(value)) if Ops.is_list?(value)

  ret = Builtins.isempty(Convert.to_map(value)) if Ops.is_map?(value)

  ret = Builtins.size(Convert.to_term(value)) == 0 if Ops.is_term?(value)

  ret
end

#mainObject


33
34
35
36
37
38
39
40
41
42
43
# File 'library/types/src/modules/TypeRepository.rb', line 33

def main
  Yast.import "Address"
  Yast.import "Hostname"
  Yast.import "IP"
  Yast.import "Netmask"
  Yast.import "URL"

  # Map of known types, empty initially
  @types = {}
  TypeRepository()
end

#regex_validator(regex, value) ⇒ Object

Generic regular expression validator.

@param [String] regex the regular expression to be matched @param [String] value the value to be matched @return true if successful


121
122
123
# File 'library/types/src/modules/TypeRepository.rb', line 121

def regex_validator(regex, value)
  Builtins.regexpmatch(value, regex)
end

#TypeRepositoryObject

Constructor, defines the known types.


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'library/types/src/modules/TypeRepository.rb', line 68

def TypeRepository
  @types = {
    "ip"           => fun_ref(IP.method(:Check), "boolean (string)"),
    "ip4"          => fun_ref(IP.method(:Check4), "boolean (string)"),
    "ip6"          => fun_ref(IP.method(:Check6), "boolean (string)"),
    "netmask"      => fun_ref(Netmask.method(:Check), "boolean (string)"),
    "netmask4"     => fun_ref(Netmask.method(:Check4), "boolean (string)"),
    "netmask6"     => fun_ref(Netmask.method(:Check6), "boolean (string)"),
    "host"         => fun_ref(Address.method(:Check), "boolean (string)"),
    "host4"        => fun_ref(Address.method(:Check4), "boolean (string)"),
    "host6"        => fun_ref(Address.method(:Check6), "boolean (string)"),
    "hostname"     => fun_ref(Hostname.method(:Check), "boolean (string)"),
    "fullhostname" => fun_ref(Hostname.method(:CheckFQ), "boolean (string)"),
    "domain"       => fun_ref(
      Hostname.method(:CheckDomain),
      "boolean (string)"
    ),
    "url"          => fun_ref(URL.method(:Check), "boolean (string)"),
    "string"       => fun_ref(method(:is_string), "boolean (any)")
  }

  nil
end