Class: Olyx::Guardrails::EnumValue

Inherits:
Object
  • Object
show all
Defined in:
lib/olyx/guardrails/enum_value.rb

Overview

Callable validator for a closed public option set.

Instance Method Summary collapse

Constructor Details

#initialize(allowed:, error:) ⇒ EnumValue

Returns a new instance of EnumValue.



7
8
9
10
# File 'lib/olyx/guardrails/enum_value.rb', line 7

def initialize(allowed:, error:)
  @allowed = allowed.freeze
  @error = error
end

Instance Method Details

#call(value) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/olyx/guardrails/enum_value.rb', line 12

def call(value)
  normalized = value.to_sym if value.respond_to?(:to_sym)
  return normalized if @allowed.include?(normalized)

  raise ArgumentError, @error
end