Class: T::Types::TypedHash

Inherits:
TypedEnumerable show all
Defined in:
lib/types/types/typed_hash.rb

Direct Known Subclasses

Untyped

Defined Under Namespace

Classes: Untyped

Instance Method Summary collapse

Methods inherited from TypedEnumerable

#build_type, #describe_obj

Methods inherited from Base

#==, #build_type, #describe_obj, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(keys:, values:) ⇒ TypedHash

Returns a new instance of TypedHash.



10
11
12
13
# File 'lib/types/types/typed_hash.rb', line 10

def initialize(keys:, values:)
  @inner_keys = keys
  @inner_values = values
end

Instance Method Details

#keysObject

Technically we don’t need this, but it is a nice api



16
17
18
# File 'lib/types/types/typed_hash.rb', line 16

def keys
  @keys ||= T::Utils.coerce(@inner_keys)
end

#nameObject

overrides Base



30
31
32
# File 'lib/types/types/typed_hash.rb', line 30

def name
  "T::Hash[#{keys.name}, #{values.name}]"
end

#new(*args, &blk) ⇒ Object



44
45
46
# File 'lib/types/types/typed_hash.rb', line 44

def new(*args, &blk)
  Hash.new(*T.unsafe(args), &blk)
end

#recursively_valid?(obj) ⇒ Boolean

overrides Base

Returns:



35
36
37
# File 'lib/types/types/typed_hash.rb', line 35

def recursively_valid?(obj)
  obj.is_a?(Hash) && super
end

#typeObject



25
26
27
# File 'lib/types/types/typed_hash.rb', line 25

def type
  @type ||= T::Utils.coerce([keys, values])
end

#underlying_classObject



6
7
8
# File 'lib/types/types/typed_hash.rb', line 6

def underlying_class
  Hash
end

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



40
41
42
# File 'lib/types/types/typed_hash.rb', line 40

def valid?(obj)
  obj.is_a?(Hash)
end

#valuesObject

Technically we don’t need this, but it is a nice api



21
22
23
# File 'lib/types/types/typed_hash.rb', line 21

def values
  @values ||= T::Utils.coerce(@inner_values)
end