Class: StrongJSON::Type::Hash
- Inherits:
-
Object
- Object
- StrongJSON::Type::Hash
- Defined in:
- lib/strong_json/type.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coerce(value, path: ErrorPath.root(self)) ⇒ Object
-
#initialize(type) ⇒ Hash
constructor
A new instance of Hash.
Methods included from WithAlias
Methods included from Match
Constructor Details
#initialize(type) ⇒ Hash
Returns a new instance of Hash.
352 353 354 |
# File 'lib/strong_json/type.rb', line 352 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
350 351 352 |
# File 'lib/strong_json/type.rb', line 350 def type @type end |
Instance Method Details
#==(other) ⇒ Object
368 369 370 371 372 373 |
# File 'lib/strong_json/type.rb', line 368 def ==(other) if other.is_a?(Hash) # @type var other: Hash<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/strong_json/type.rb', line 356 def coerce(value, path: ErrorPath.root(self)) if value.is_a?(::Hash) (_ = {}).tap do |result| value.each do |k, v| result[k] = type.coerce(v, path: path.dig(key: k, type: type)) end end else raise TypeError.new(path: path, value: value) end end |