Class: StrongJSON::Type::Enum
- Inherits:
-
Object
- Object
- StrongJSON::Type::Enum
- Includes:
- Match
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #coerce(value, path: []) ⇒ Object
-
#initialize(types) ⇒ Enum
constructor
A new instance of Enum.
- #to_s ⇒ Object
Methods included from Match
Constructor Details
#initialize(types) ⇒ Enum
Returns a new instance of Enum.
208 209 210 |
# File 'lib/strong_json/type.rb', line 208 def initialize(types) @types = types end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
206 207 208 |
# File 'lib/strong_json/type.rb', line 206 def types @types end |
Instance Method Details
#coerce(value, path: []) ⇒ Object
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/strong_json/type.rb', line 216 def coerce(value, path: []) types.each do |ty| begin return ty.coerce(value, path: path) rescue UnexpectedFieldError, IllegalTypeError, Error # rubocop:disable Lint/HandleExceptions end end raise Error.new(path: path, type: self, value: value) end |
#to_s ⇒ Object
212 213 214 |
# File 'lib/strong_json/type.rb', line 212 def to_s "enum(#{types.map(&:to_s).join(", ")})" end |