Class: LIVR::Rules::String::OneOf

Inherits:
LIVR::Rule show all
Defined in:
lib/livr/rules/string.rb

Instance Method Summary collapse

Constructor Details

#initialize(*allowed_values) ⇒ OneOf

Returns a new instance of OneOf.


35
36
37
# File 'lib/livr/rules/string.rb', line 35

def initialize(*allowed_values)
  @allowed_values = allowed_values.flatten
end

Instance Method Details

#call(value, user_data, field_results) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/livr/rules/string.rb', line 39

def call(value, user_data, field_results)
  return if is_no_value(value)
  return 'FORMAT_ERROR' unless is_primitive(value)

  @allowed_values.each do |allowed_value|
    if allowed_value.to_s == value.to_s
      field_results << allowed_value
      return
    end
  end

  return 'NOT_ALLOWED_VALUE'
end