Class: StrongJSON::Type::Object
- Inherits:
-
Object
- Object
- StrongJSON::Type::Object
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#ignored_attributes ⇒ Object
readonly
Returns the value of attribute ignored_attributes.
-
#prohibited_attributes ⇒ Object
readonly
Returns the value of attribute prohibited_attributes.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(object, path: ErrorPath.root(self)) ⇒ Object
- #ignore(attrs) ⇒ Object
- #ignore!(attrs) ⇒ Object
-
#initialize(fields, ignored_attributes:, prohibited_attributes:) ⇒ Object
constructor
A new instance of Object.
- #prohibit(attrs) ⇒ Object
- #prohibit!(attrs) ⇒ Object
- #to_s ⇒ Object
- #update_fields ⇒ Object
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(fields, ignored_attributes:, prohibited_attributes:) ⇒ Object
Returns a new instance of Object.
198 199 200 201 202 |
# File 'lib/strong_json/type.rb', line 198 def initialize(fields, ignored_attributes:, prohibited_attributes:) @fields = fields @ignored_attributes = ignored_attributes @prohibited_attributes = prohibited_attributes end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
196 197 198 |
# File 'lib/strong_json/type.rb', line 196 def fields @fields end |
#ignored_attributes ⇒ Object (readonly)
Returns the value of attribute ignored_attributes.
196 197 198 |
# File 'lib/strong_json/type.rb', line 196 def ignored_attributes @ignored_attributes end |
#prohibited_attributes ⇒ Object (readonly)
Returns the value of attribute prohibited_attributes.
196 197 198 |
# File 'lib/strong_json/type.rb', line 196 def prohibited_attributes @prohibited_attributes end |
Instance Method Details
#==(other) ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/strong_json/type.rb', line 277 def ==(other) if other.is_a?(Object) # @type var other: Object<any> other.fields == fields && other.ignored_attributes == ignored_attributes && other.prohibited_attributes == prohibited_attributes 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 240 241 |
# 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 unless (intersection = Set.new(object.keys).intersection(prohibited_attributes)).empty? raise UnexpectedAttributeError.new(path: path, attribute: intersection.to_a.first) end case attrs = ignored_attributes when :any object = object.dup extra_keys = Set.new(object.keys) - Set.new(fields.keys) extra_keys.each do |key| object.delete(key) end when Set object = object.dup attrs.each do |key| object.delete(key) end end # @type var result: ::Hash<Symbol, any> result = {} object.each do |key, _| unless fields.key?(key) raise UnexpectedAttributeError.new(path: path, attribute: key) end end fields.each do |key, type| result[key] = type.coerce(object[key], path: path.dig(key: key, type: type)) end _ = result end |
#ignore(attrs) ⇒ Object
243 244 245 |
# File 'lib/strong_json/type.rb', line 243 def ignore(attrs) Object.new(fields, ignored_attributes: attrs, prohibited_attributes: prohibited_attributes) end |
#ignore!(attrs) ⇒ Object
247 248 249 250 |
# File 'lib/strong_json/type.rb', line 247 def ignore!(attrs) @ignored_attributes = attrs self end |
#prohibit(attrs) ⇒ Object
252 253 254 |
# File 'lib/strong_json/type.rb', line 252 def prohibit(attrs) Object.new(fields, ignored_attributes: ignored_attributes, prohibited_attributes: attrs) end |
#prohibit!(attrs) ⇒ Object
256 257 258 259 |
# File 'lib/strong_json/type.rb', line 256 def prohibit!(attrs) @prohibited_attributes = attrs self end |
#to_s ⇒ Object
269 270 271 272 273 274 275 |
# File 'lib/strong_json/type.rb', line 269 def to_s fields = @fields.map do |name, type| "#{name}: #{type}" end self.alias&.to_s || "object(#{fields.join(', ')})" end |
#update_fields ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/strong_json/type.rb', line 261 def update_fields fields.dup.yield_self do |fields| yield fields Object.new(fields, ignored_attributes: ignored_attributes, prohibited_attributes: prohibited_attributes) end end |