Class: Thy::Types::Hash
- Inherits:
-
Object
- Object
- Thy::Types::Hash
- Defined in:
- lib/thy/types/hash.rb
Instance Method Summary collapse
- #check(value) ⇒ Object
-
#initialize(schema) ⇒ Hash
constructor
A new instance of Hash.
Constructor Details
#initialize(schema) ⇒ Hash
Returns a new instance of Hash.
6 7 8 9 10 |
# File 'lib/thy/types/hash.rb', line 6 def initialize(schema) @schema = schema @schema_length = schema.length @schema_keys = schema.keys end |
Instance Method Details
#check(value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/thy/types/hash.rb', line 12 def check(value) unless value.is_a?(::Hash) return Result::Failure.new("Expected a Hash, but got: #{value.inspect}") end if @schema_length != value.length return Result::Failure.new("Expected #{value.inspect} to contain #{@schema_length} keys") end if @schema_keys & value.keys != @schema_keys return Result::Failure.new( "Expected #{value.inspect} to contain keys: #{@schema_keys.inspect}", ) end @schema.each do |k, t| v = value.fetch(k) if t.check(v).failure? return Result::Failure.new("Expected #{v.inspect} to be of type #{t.inspect}") end end Result::Success end |