class Hermod::Validators::AllowedValues

Checks the given value is in a predefined list of allowed values

Attributes

allowed_values[R]

Public Class Methods

new(allowed_values) click to toggle source

Sets up the validator with the list of allowed values

# File lib/hermod/validators/allowed_values.rb, line 10
def initialize(allowed_values)
  @allowed_values = allowed_values
end

Private Instance Methods

message(value, attributes) click to toggle source
# File lib/hermod/validators/allowed_values.rb, line 20
def message(value, attributes)
  list_of_values = allowed_values.to_sentence(last_word_connector: ", or ", two_words_connector: " or ")
  "must be one of #{list_of_values}, not #{value}"
end
test(value, attributes) click to toggle source
# File lib/hermod/validators/allowed_values.rb, line 16
def test(value, attributes)
  value.blank? || allowed_values.include?(value)
end