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
354 355 356 |
# File 'lib/strong_json/type.rb', line 354 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
352 353 354 |
# File 'lib/strong_json/type.rb', line 352 def type @type end |
Instance Method Details
#==(other) ⇒ Object
370 371 372 373 374 375 |
# File 'lib/strong_json/type.rb', line 370 def ==(other) if other.is_a?(Hash) # @type var other: Hash<any> other.type == type end end |
#coerce(value, path: ErrorPath.root(self)) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/strong_json/type.rb', line 358 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 |