Class: StrongJSON::Type::Enum

Inherits:
Object
  • Object
show all
Includes:
Match
Defined in:
lib/strong_json/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#typesObject (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

Raises:



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_sObject



212
213
214
# File 'lib/strong_json/type.rb', line 212

def to_s
  "enum(#{types.map(&:to_s).join(", ")})"
end