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.
300 301 302 303 |
# File 'lib/strong_json/type.rb', line 300 def initialize(types, detector = nil) @types = types @detector = detector end |
Instance Attribute Details
#detector ⇒ Object (readonly)
Returns the value of attribute detector.
298 299 300 |
# File 'lib/strong_json/type.rb', line 298 def detector @detector end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
297 298 299 |
# File 'lib/strong_json/type.rb', line 297 def types @types end |
Instance Method Details
#==(other) ⇒ Object
327 328 329 330 331 332 |
# File 'lib/strong_json/type.rb', line 327 def ==(other) if other.is_a?(Enum) other.types == types && other.detector == detector end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/strong_json/type.rb', line 309 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
305 306 307 |
# File 'lib/strong_json/type.rb', line 305 def to_s self.alias&.to_s || "enum(#{types.map(&:to_s).join(", ")})" end |