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)

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

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

#items_must_be_strings(value) ⇒ TrueClass, FalseClass

Check that value items are all strings and not empty.

Parameters:

  • value (Array)

    with items to check.

Returns:

  • (TrueClass, FalseClass)

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

def items_must_be_strings(value)
  value.all? do |item|
    item.is_a?(String) && !item.empty?
  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)

74
75
76
# File 'lib/sensu/settings/rules.rb', line 74

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)

82
83
84
# File 'lib/sensu/settings/rules.rb', line 82

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

#must_be_a_string(value) ⇒ TrueClass, FalseClass

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)

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

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

Check that a value is an integer.

Parameters:

  • value (Object)

    to check.

Returns:

  • (TrueClass, FalseClass)

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

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)

66
67
68
# File 'lib/sensu/settings/rules.rb', line 66

def must_be_an_integer_if_set(value)
  value.nil? ? true : must_be_an_integer(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)

99
100
101
# File 'lib/sensu/settings/rules.rb', line 99

def must_be_boolean_if_set(value)
  value.nil? ? true : (!!value == 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)

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

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)

149
150
151
# File 'lib/sensu/settings/rules.rb', line 149

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)

127
128
129
130
131
# File 'lib/sensu/settings/rules.rb', line 127

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)

91
92
93
# File 'lib/sensu/settings/rules.rb', line 91

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