Class: StrongJSON::Type::Object
- Inherits:
-
Object
- Object
- StrongJSON::Type::Object
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#exceptions ⇒ Object
readonly
Returns the value of attribute exceptions.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#on_unknown ⇒ Object
readonly
Returns the value of attribute on_unknown.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(object, path: ErrorPath.root(self)) ⇒ Object
- #ignore(*ignores, except: nil) ⇒ Object
-
#initialize(fields, on_unknown:, exceptions:) ⇒ Object
constructor
A new instance of Object.
- #reject(*rejecteds, except: nil) ⇒ Object
- #to_s ⇒ Object
- #update_fields ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(fields, on_unknown:, exceptions:) ⇒ Object
Returns a new instance of Object.
200 201 202 203 204 |
# File 'lib/strong_json/type.rb', line 200 def initialize(fields, on_unknown:, exceptions:) @fields = fields @on_unknown = on_unknown @exceptions = exceptions end |
Instance Attribute Details
#exceptions ⇒ Object (readonly)
Returns the value of attribute exceptions.
198 199 200 |
# File 'lib/strong_json/type.rb', line 198 def exceptions @exceptions end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
198 199 200 |
# File 'lib/strong_json/type.rb', line 198 def fields @fields end |
#on_unknown ⇒ Object (readonly)
Returns the value of attribute on_unknown.
198 199 200 |
# File 'lib/strong_json/type.rb', line 198 def on_unknown @on_unknown end |
Instance Method Details
#==(other) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/strong_json/type.rb', line 285 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
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 240 241 |
# File 'lib/strong_json/type.rb', line 206 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
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/strong_json/type.rb', line 244 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
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/strong_json/type.rb', line 257 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 ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/strong_json/type.rb', line 277 def to_s fields = @fields.map do |name, type| "#{name}: #{type}" end self.alias&.to_s || "object(#{fields.join(', ')})" end |