Module: OpenActive::Concerns::TypeChecker
- Included in:
- BaseModel
- Defined in:
- lib/openactive/concerns/type_checker.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#check_types(value, types:) ⇒ Boolean
Check if the given value is of at least one of the given types.
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/openactive/concerns/type_checker.rb', line 4 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#check_types(value, types:) ⇒ Boolean
Check if the given value is of at least one of the given types.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/openactive/concerns/type_checker.rb', line 41 def check_types(value, types:) validators = types.map { |type| OpenActive::Validators::BaseValidator.get_validator(type) }.compact return value if validators.empty? validators.each do |validator| if validator.run(value) # If validation passes for the given type # We coerce the type to mitigate PHP loose types return validator.coerce(value) end end val = value.to_s val += " (#{val.to_h})" if val.respond_to?(:to_h) # If validation does not pass for any of the provided types, # type invalid raise StandardError, "The first argument type does not match any of the declared parameter types "\ "(#{types.join(',')}) for #{val}." end |