Class: OpenActive::Validators::EnumValidator

Inherits:
BaseValidator show all
Defined in:
lib/openactive/validators/enum_validator.rb

Instance Method Summary collapse

Methods inherited from BaseValidator

get_validator

Constructor Details

#initialize(classname) ⇒ EnumValidator

Returns a new instance of EnumValidator.



4
5
6
# File 'lib/openactive/validators/enum_validator.rb', line 4

def initialize(classname)
  @classname = classname
end

Instance Method Details

#coerce(value) ⇒ int

Coerce enum url to the enum instance

Parameters:

  • value (mixed)

    Unum instance or URL

Returns:

  • (int)

    The coerced enum



12
13
14
15
16
# File 'lib/openactive/validators/enum_validator.rb', line 12

def coerce(value)
  return value if value.is_a?(classname)

  classname.find_by_value(value)
end

#run(value) ⇒ Boolean

Run validation on the given value.

Parameters:

  • value (mixed)

    The value to validate.

Returns:

  • (Boolean)

    Whether validation passes or not.



22
23
24
25
26
# File 'lib/openactive/validators/enum_validator.rb', line 22

def run(value)
  return true if value.is_a?(classname)

  classname.find_by_value(value)
end