class StrongJSON::Type::Object
Attributes
exceptions[R]
@dynamic fields, on_unknown
, exceptions
fields[R]
@dynamic fields, on_unknown
, exceptions
on_unknown[R]
@dynamic fields, on_unknown
, exceptions
Public Class Methods
new(fields, on_unknown:, exceptions:)
click to toggle source
# File lib/strong_json/type.rb, line 196 def initialize(fields, on_unknown:, exceptions:) @fields = fields @on_unknown = on_unknown @exceptions = exceptions end
Public Instance Methods
==(other)
click to toggle source
# 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
Also aliased as: eql?
coerce(object, path: ErrorPath.root(self))
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_s()
click to toggle source
# 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_fields() { |fields| ... }
click to toggle source
# 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