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.



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

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.



194
195
196
# File 'lib/strong_json/type.rb', line 194

def exceptions
  @exceptions
end

#fieldsObject (readonly)

Returns the value of attribute fields.



194
195
196
# File 'lib/strong_json/type.rb', line 194

def fields
  @fields
end

#on_unknownObject (readonly)

Returns the value of attribute on_unknown.



194
195
196
# File 'lib/strong_json/type.rb', line 194

def on_unknown
  @on_unknown
end

Instance Method Details

#==(other) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/strong_json/type.rb', line 279

def ==(other)
  if other.is_a?(Object)
    other.fields == fields &&
      other.on_unknown == on_unknown &&
      other.exceptions == exceptions
  end
end

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



202
203
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
# File 'lib/strong_json/type.rb', line 202

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, untyped]
  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



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/strong_json/type.rb', line 239

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



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/strong_json/type.rb', line 251

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



271
272
273
274
275
276
277
# File 'lib/strong_json/type.rb', line 271

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

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

#update_fieldsObject



263
264
265
266
267
268
269
# File 'lib/strong_json/type.rb', line 263

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

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