Class: Rtype::Behavior::TypedHash

Inherits:
Base show all
Defined in:
lib/rtype/behavior/typed_hash.rb

Overview

Typed hash behavior. empty hash allowed

Instance Method Summary collapse

Methods inherited from Base

[]

Constructor Details

#initialize(key_type, value_type) ⇒ TypedHash



5
6
7
8
9
10
# File 'lib/rtype/behavior/typed_hash.rb', line 5

def initialize(key_type, value_type)
  @ktype = key_type
  @vtype = value_type
  Rtype.assert_valid_argument_type_sig_element(@ktype)
  Rtype.assert_valid_argument_type_sig_element(@vtype)
end

Instance Method Details

#error_message(value) ⇒ Object



24
25
26
# File 'lib/rtype/behavior/typed_hash.rb', line 24

def error_message(value)
  "Expected #{value.inspect} to be a hash with key type #{@ktype.inspect} and value type #{@vtype.inspect}"
end

#valid?(value) ⇒ Boolean



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rtype/behavior/typed_hash.rb', line 12

def valid?(value)
  if value.is_a?(Hash)
    any = value.any? do |k, v|
      !Rtype::valid?(@ktype, k) ||
      !Rtype::valid?(@vtype, v)
    end
    !any
  else
    false
  end
end