Module: Sensu::Settings::Rules

Included in:
Validator
Defined in:
lib/sensu/settings/rules.rb

Instance Method Summary collapse

Instance Method Details

#either_are_set?(*values) ⇒ TrueClass, FalseClass

Check if either of the values are set (not nil).

Parameters:

  • values (Array<Object>)

    to check if not nil.

Returns:

  • (TrueClass, FalseClass)

129
130
131
132
133
# File 'lib/sensu/settings/rules.rb', line 129

def either_are_set?(*values)
  values.any? do |value|
    !value.nil?
  end
end

#items_must_be_strings(value, regex = nil) ⇒ TrueClass, FalseClass

Check that value items are all strings and not empty.

Parameters:

  • value (Array)

    with items to check.

  • regex (Regexp) (defaults to: nil)

    to validate string items with.

Returns:

  • (TrueClass, FalseClass)

118
119
120
121
122
123
# File 'lib/sensu/settings/rules.rb', line 118

def items_must_be_strings(value, regex=nil)
  value.all? do |item|
    item.is_a?(String) && !item.empty? &&
      (regex.nil? || item =~ regex)
  end
end

#must_be_a_hash(value) ⇒ TrueClass, FalseClass Also known as: is_a_hash?

Check that a value is a hash.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

8
9
10
# File 'lib/sensu/settings/rules.rb', line 8

def must_be_a_hash(value)
  value.is_a?(Hash)
end

#must_be_a_hash_if_set(value) ⇒ TrueClass, FalseClass

Check that a value is a hash, if set (not nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

17
18
19
# File 'lib/sensu/settings/rules.rb', line 17

def must_be_a_hash_if_set(value)
  value.nil? ? true : must_be_a_hash(value)
end

#must_be_a_numeric(value) ⇒ TrueClass, FalseClass

Check that a value is numeric.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

76
77
78
# File 'lib/sensu/settings/rules.rb', line 76

def must_be_a_numeric(value)
  value.is_a?(Numeric)
end

#must_be_a_numeric_if_set(value) ⇒ TrueClass, FalseClass

Check that a value is numeric, if set (not nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

84
85
86
# File 'lib/sensu/settings/rules.rb', line 84

def must_be_a_numeric_if_set(value)
  value.nil? ? true : must_be_a_numeric(value)
end

#must_be_a_string(value) ⇒ TrueClass, FalseClass Also known as: is_a_string?

Check that a value is a string.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

42
43
44
# File 'lib/sensu/settings/rules.rb', line 42

def must_be_a_string(value)
  value.is_a?(String)
end

#must_be_a_string_if_set(value) ⇒ TrueClass, FalseClass

Check that a value is a string, if set (not nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

51
52
53
# File 'lib/sensu/settings/rules.rb', line 51

def must_be_a_string_if_set(value)
  value.nil? ? true : must_be_a_string(value)
end

#must_be_an_array(value) ⇒ TrueClass, FalseClass Also known as: is_an_array?

Check that a value is an array.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

25
26
27
# File 'lib/sensu/settings/rules.rb', line 25

def must_be_an_array(value)
  value.is_a?(Array)
end

#must_be_an_array_if_set(value) ⇒ TrueClass, FalseClass

Check that a value is an array, if set (not nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

34
35
36
# File 'lib/sensu/settings/rules.rb', line 34

def must_be_an_array_if_set(value)
  value.nil? ? true : must_be_an_array(value)
end

#must_be_an_integer(value) ⇒ TrueClass, FalseClass Also known as: is_an_integer?

Check that a value is an integer.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

59
60
61
# File 'lib/sensu/settings/rules.rb', line 59

def must_be_an_integer(value)
  value.is_a?(Integer)
end

#must_be_an_integer_if_set(value) ⇒ TrueClass, FalseClass

Check that a value is an integer, if set (not nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

68
69
70
# File 'lib/sensu/settings/rules.rb', line 68

def must_be_an_integer_if_set(value)
  value.nil? ? true : must_be_an_integer(value)
end

#must_be_boolean(value) ⇒ TrueClass, FalseClass

Check if a value is boolean.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

101
102
103
# File 'lib/sensu/settings/rules.rb', line 101

def must_be_boolean(value)
  !!value == value
end

#must_be_boolean_if_set(value) ⇒ TrueClass, FalseClass

Check if a value is boolean, if set (no nil).

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

109
110
111
# File 'lib/sensu/settings/rules.rb', line 109

def must_be_boolean_if_set(value)
  value.nil? ? true : must_be_boolean(value)
end

#must_be_either(allowed, *values) ⇒ TrueClass, FalseClass

Check if values are allowed.

Parameters:

  • allowed (Array<Object>)

    allowed values.

  • values (Array<Object>)

    to check if allowed.

Returns:

  • (TrueClass, FalseClass)

150
151
152
153
154
# File 'lib/sensu/settings/rules.rb', line 150

def must_be_either(allowed, *values)
  values.flatten.all? do |value|
    allowed.include?(value)
  end
end

#must_be_either_if_set(allowed, *values) ⇒ TrueClass, FalseClass

Check if values are allowed, if set (not nil).

Parameters:

  • allowed (Array<Object>)

    allowed values.

  • values (Array<Object>)

    to check if allowed.

Returns:

  • (TrueClass, FalseClass)

161
162
163
# File 'lib/sensu/settings/rules.rb', line 161

def must_be_either_if_set(allowed, *values)
  values[0].nil? ? true : must_be_either(allowed, values)
end

#must_be_time(*values) ⇒ TrueClass, FalseClass

Check if values are valid times (can be parsed).

Parameters:

  • values (Array<Object>)

    to check if valid time.

Returns:

  • (TrueClass, FalseClass)

139
140
141
142
143
# File 'lib/sensu/settings/rules.rb', line 139

def must_be_time(*values)
  values.all? do |value|
    Time.parse(value) rescue false
  end
end

#must_match_regex(regex, value) ⇒ TrueClass, FalseClass

Check that a value matches a regular expression.

Parameters:

  • regex (Regexp)

    pattern to compare with value.

  • value (Object)

    to check if matches pattern.

Returns:

  • (TrueClass, FalseClass)

93
94
95
# File 'lib/sensu/settings/rules.rb', line 93

def must_match_regex(regex, value)
  (value =~ regex) == 0
end