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


133
134
135
136
# File 'library/types/src/modules/TypeRepository.rb', line 133

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



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

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(s) ⇒ Object

Check, if s is a string.

Parameters:

  • s (Object)

    a value to be validated

Returns:

  • true if s is string



65
66
67
68
# File 'library/types/src/modules/TypeRepository.rb', line 65

def is_string(s)
  s = deep_copy(s)
  Ops.is_string?(s)
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 (“”).



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

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



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

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


124
125
126
# File 'library/types/src/modules/TypeRepository.rb', line 124

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

#TypeRepositoryObject

Constructor, defines the known types.



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

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