Class: StrongJSON::Type::Object

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WithAlias

#alias, #with_alias

Methods included from Match

#===, #=~

Constructor Details

#initialize(fields, on_unknown:, exceptions:) ⇒ Object

Returns a new instance of Object.



198
199
200
201
202
# File 'lib/strong_json/type.rb', line 198

def initialize(fields, on_unknown:, exceptions:)
  @fields = fields
  @on_unknown = on_unknown
  @exceptions = exceptions
end

Instance Attribute Details

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



196
197
198
# File 'lib/strong_json/type.rb', line 196

def exceptions
  @exceptions
end

#fieldsObject (readonly)

Returns the value of attribute fields.



196
197
198
# File 'lib/strong_json/type.rb', line 196

def fields
  @fields
end

#on_unknownObject (readonly)

Returns the value of attribute on_unknown.



196
197
198
# File 'lib/strong_json/type.rb', line 196

def on_unknown
  @on_unknown
end

Instance Method Details

#==(other) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/strong_json/type.rb', line 283

def ==(other)
  if other.is_a?(Object)
    # @type var other: Object<any>
    other.fields == fields &&
      other.on_unknown == on_unknown &&
      other.exceptions == exceptions
  end
end

#coerce(object, path: ErrorPath.root(self)) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/strong_json/type.rb', line 204

def coerce(object, path: ErrorPath.root(self))
  unless object.is_a?(::Hash)
    raise TypeError.new(path: path, value: object)
  end

  object = object.dup
  unknown_attributes = Set.new(object.keys) - fields.keys

  case on_unknown
  when :reject
    unknown_attributes.each do |attr|
      if exceptions.member?(attr)
        object.delete(attr)
      else
        raise UnexpectedAttributeError.new(path: path, attribute: attr)
      end
    end
  when :ignore
    unknown_attributes.each do |attr|
      if exceptions.member?(attr)
        raise UnexpectedAttributeError.new(path: path, attribute: attr)
      else
        object.delete(attr)
      end
    end
  end

  # @type var result: ::Hash<Symbol, any>
  result = {}

  fields.each do |key, type|
    result[key] = type.coerce(object[key], path: path.dig(key: key, type: type))
  end

  _ = result
end

#ignore(*ignores, except: nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/strong_json/type.rb', line 242

def ignore(*ignores, except: nil)
  if ignores.empty? && !except
    Object.new(fields, on_unknown: :ignore, exceptions: Set[])
  else
    if except
      Object.new(fields, on_unknown: :ignore, exceptions: except)
    else
      Object.new(fields, on_unknown: :reject, exceptions: Set.new(ignores))
    end
  end
end

#reject(*rejecteds, except: nil) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/strong_json/type.rb', line 255

def reject(*rejecteds, except: nil)
  if rejecteds.empty? && !except
    Object.new(fields, on_unknown: :reject, exceptions: Set[])
  else
    if except
      Object.new(fields, on_unknown: :reject, exceptions: except)
    else
      Object.new(fields, on_unknown: :ignore, exceptions: Set.new(rejecteds))
    end
  end
end

#to_sObject



275
276
277
278
279
280
281
# File 'lib/strong_json/type.rb', line 275

def to_s
  fields = @fields.map do |name, type|
    "#{name}: #{type}"
  end

  self.alias&.to_s || "object(#{fields.join(', ')})"
end

#update_fieldsObject



267
268
269
270
271
272
273
# File 'lib/strong_json/type.rb', line 267

def update_fields
  fields.dup.yield_self do |fields|
    yield fields

    Object.new(fields, on_unknown: on_unknown, exceptions: exceptions)
  end
end