Class: StrongJSON::Type::Enum
- Inherits:
-
Object
- Object
- StrongJSON::Type::Enum
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#detector ⇒ Object
readonly
Returns the value of attribute detector.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(value, path: ErrorPath.root(self)) ⇒ Object
-
#initialize(types, detector = nil) ⇒ Enum
constructor
A new instance of Enum.
- #to_s ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(types, detector = nil) ⇒ Enum
Returns a new instance of Enum.
307 308 309 310 |
# File 'lib/strong_json/type.rb', line 307 def initialize(types, detector = nil) @types = types @detector = detector end |
Instance Attribute Details
#detector ⇒ Object (readonly)
Returns the value of attribute detector.
305 306 307 |
# File 'lib/strong_json/type.rb', line 305 def detector @detector end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
304 305 306 |
# File 'lib/strong_json/type.rb', line 304 def types @types end |
Instance Method Details
#==(other) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/strong_json/type.rb', line 334 def ==(other) if other.is_a?(Enum) # @type var other: Enum<any> other.types == types && other.detector == detector end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/strong_json/type.rb', line 316 def coerce(value, path: ErrorPath.root(self)) if d = detector type = d[value] if type && types.include?(type) return type.coerce(value, path: path.(type: type)) end end types.each do |ty| begin return ty.coerce(value, path: path.(type: ty)) rescue UnexpectedAttributeError, TypeError # rubocop:disable Lint/HandleExceptions end end raise TypeError.new(path: path, value: value) end |
#to_s ⇒ Object
312 313 314 |
# File 'lib/strong_json/type.rb', line 312 def to_s self.alias&.to_s || "enum(#{types.map(&:to_s).join(", ")})" end |